sui-search.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <!-- 搜索框 -->
  3. <view class="search-input-box dis-flex" :style="{ background: bgColor }">
  4. <view
  5. class="search-input dis-flex flex-y-center"
  6. @tap="gosearch"
  7. :style="{ background: background, borderRadius: radius }"
  8. >
  9. <text class="iconfont icon-sousuo col-9"></text>
  10. <input
  11. :disabled="disabled"
  12. v-model="keyword"
  13. @blur="search"
  14. :focus="focus"
  15. :placeholder="placeholder"
  16. :placeholderStyle="placeStyle"
  17. type="text"
  18. />
  19. <text class="iconfont icon-iconfontguanbi clear" v-if="showClose" @click="clearKeyword"></text>
  20. <slot name="right"></slot>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. name: 'search',
  27. props: {
  28. // 是否禁止输入
  29. disabled: {
  30. type: Boolean,
  31. default: false
  32. },
  33. // 是否自动聚焦
  34. focus: {
  35. type: Boolean,
  36. default: false
  37. },
  38. // 输入框值
  39. value: {
  40. type: [Number, String],
  41. default: ''
  42. },
  43. // 搜索栏Placeholder
  44. placeholder: {
  45. type: String,
  46. default: ''
  47. },
  48. // 搜索栏Placeholder样式
  49. placeStyle: {
  50. type: String,
  51. default: 'color:#999;font-size:24rpx;'
  52. },
  53. // 输入框背景颜色
  54. background: {
  55. type: String,
  56. default: '#f7f7f7'
  57. },
  58. // 搜索栏圆角
  59. radius: {
  60. type: [Number, String]
  61. },
  62. bgColor: {
  63. type: String,
  64. default: '#fff'
  65. },
  66. clearable: {
  67. type: Boolean,
  68. default: true
  69. }
  70. },
  71. watch: {
  72. value: {
  73. immediate: true,
  74. handler(newVal) {
  75. this.keyword = newVal
  76. }
  77. }
  78. },
  79. computed: {
  80. showClose() {
  81. return this.clearable && this.keyword
  82. }
  83. },
  84. data() {
  85. return {
  86. keyword: ''
  87. }
  88. },
  89. methods: {
  90. // input是否禁止输入时触发事件
  91. gosearch() {
  92. this.$emit('gosearch', this.keyword)
  93. },
  94. // input失去焦点时触发事件
  95. search() {
  96. this.$emit('search', this.keyword)
  97. },
  98. clearKeyword() {
  99. this.keyword = ''
  100. this.$emit('clear')
  101. }
  102. }
  103. }
  104. </script>
  105. <style lang="scss">
  106. /* 搜索框 */
  107. $searchbar-height: 60rpx;
  108. .dis-flex {
  109. display: flex;
  110. }
  111. .flex-y-center {
  112. align-items: center;
  113. }
  114. .col-9 {
  115. color: #999;
  116. margin-right: 8px;
  117. }
  118. .search-input-box {
  119. height: $searchbar-height;
  120. padding: 32rpx 24rpx;
  121. overflow: hidden;
  122. }
  123. .search-input {
  124. width: 100%;
  125. background: #f5f5f5;
  126. border-radius: 30rpx;
  127. padding: 0 26rpx;
  128. text-align: left;
  129. box-sizing: border-box;
  130. overflow: hidden;
  131. .icon {
  132. margin-right: 24rpx;
  133. }
  134. }
  135. .search-input input {
  136. flex: 1;
  137. font-size: 24rpx;
  138. height: $searchbar-height;
  139. line-height: $searchbar-height;
  140. }
  141. .clear {
  142. width: 48rpx;
  143. height: 48rpx;
  144. color: #999;
  145. font-size: 24rpx;
  146. line-height: 48rpx;
  147. text-align: center;
  148. }
  149. </style>