sui-search.vue 3.3 KB

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