木邻有你前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
4.5 KiB

  1. <template>
  2. <!-- 自定义搜索框组件 -->
  3. <view class="search-container" >
  4. <view class="search-input" :style="{ backgroundColor: bgColor }">
  5. <!-- 搜索图标 -->
  6. <view v-if="searchIconAlign === 'left' && showIcon" class="search-icon left" @click="clickIcon">
  7. <text class="iconfont">🔍</text>
  8. </view>
  9. <view v-if="searchIconAlign === 'center' && showIcon" class="search-icon center" @click="clickIcon">
  10. <text class="iconfont">🔍</text>
  11. </view>
  12. <input
  13. :value="value"
  14. :placeholder="placeholder"
  15. type="text"
  16. :disabled="disabled"
  17. :maxlength="maxLength"
  18. @input="input"
  19. @confirm="search"
  20. @keyup.enter="search"
  21. class="input-field"
  22. :style="{
  23. textAlign: textAlign,
  24. borderRadius: borderRadius,
  25. height: height,
  26. width: width }"
  27. />
  28. <!-- 清除按钮 -->
  29. <view v-if="value && !disabled" class="clear-icon" @click="clear">
  30. <text class="iconfont"></text>
  31. </view>
  32. <!-- 搜索图标在右侧 -->
  33. <view v-if="searchIconAlign === 'right' && showIcon" class="search-icon right" @click="clickIcon">
  34. <text class="iconfont">🔍</text>
  35. </view>
  36. </view>
  37. <!-- 取消按钮 -->
  38. <view v-if="showCancel" class="cancel-btn" @click="cancel">
  39. <text>取消</text>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. export default{
  45. name:'Search',
  46. props:{
  47. placeholder:{
  48. type:String,
  49. default:'搜索'
  50. },
  51. // 是否展示搜索图标
  52. showIcon:{
  53. type:Boolean,
  54. default:true
  55. },
  56. // 是否展示右侧的取消按钮
  57. showCancel:{
  58. type:Boolean,
  59. default:true
  60. },
  61. // 搜索图标对齐位置
  62. searchIconAlign:{
  63. type:String,
  64. default:'left'
  65. },
  66. // 搜索框内容对齐方式
  67. textAlign:{
  68. type:String,
  69. default:'left'
  70. },
  71. // v-model传入的内容怎么用
  72. value:{
  73. type:String,
  74. default:''
  75. },
  76. // 搜索框内容改变时触发的事件
  77. height:{
  78. type:String,
  79. default:'60rpx'
  80. },
  81. width:{
  82. type:String,
  83. default:'100%'
  84. },
  85. bgColor:{
  86. type:String,
  87. default:'#f3f7f8'
  88. },
  89. disabled:{
  90. type:Boolean,
  91. default:false
  92. },
  93. maxLength:{
  94. type:Number,
  95. default:100
  96. },
  97. borderRadius:{
  98. type:String,
  99. default:'30rpx'
  100. },
  101. },
  102. methods:{
  103. clickIcon(){
  104. console.log('clickIcon');
  105. this.search()
  106. },
  107. clear(){
  108. // console.log('clear');
  109. this.$emit('clear', '')
  110. this.$emit('input', '')
  111. },
  112. search(){
  113. console.log('search', this.value);
  114. this.$emit('search', this.value)
  115. },
  116. cancel(){
  117. // console.log('cancel');
  118. this.$emit('cancel')
  119. this.$emit('input', '')
  120. },
  121. input(e){
  122. const value = e.detail.value
  123. console.log('input', value)
  124. this.$emit('input', value)
  125. // this.onChange(value)
  126. }
  127. },
  128. watch:{
  129. value(newVal){
  130. console.log(newVal)
  131. this.$emit('change', newVal)
  132. }
  133. }
  134. }
  135. </script>
  136. <style lang="scss" scoped>
  137. .search-container {
  138. display: flex;
  139. align-items: center;
  140. padding: 20rpx;
  141. // background-color: #fff;
  142. .search-input {
  143. flex: 1;
  144. display: flex;
  145. align-items: center;
  146. // height: 60rpx;
  147. border-radius: 30rpx;
  148. padding: 0 20rpx;
  149. position: relative;
  150. .search-icon {
  151. // flex: 0.1;
  152. display: flex;
  153. align-items: center;
  154. justify-content: center;
  155. width: 40rpx;
  156. height: 40rpx;
  157. &.left {
  158. margin-right: 10rpx;
  159. }
  160. &.right {
  161. margin-left: 10rpx;
  162. }
  163. .iconfont {
  164. font-size: 28rpx;
  165. color: #999;
  166. }
  167. }
  168. .input-field {
  169. flex: 1;
  170. padding-left: 4rpx;
  171. border: none;
  172. outline: none;
  173. background: transparent;
  174. font-size: 28rpx;
  175. color: #333;
  176. // background-color: #f1f6ff;
  177. &::placeholder {
  178. color: #999;
  179. }
  180. }
  181. .clear-icon {
  182. display: flex;
  183. // align-items: center;
  184. line-height: 40rpx;
  185. justify-content: center;
  186. width: 40rpx;
  187. height: 40rpx;
  188. margin-left: 10rpx;
  189. border-radius: 50%;
  190. background: #f0f0f0;
  191. .iconfont {
  192. font-size: 20rpx;
  193. color: #b2b2b2;
  194. }
  195. }
  196. }
  197. .cancel-btn {
  198. margin-left: 20rpx;
  199. padding: 0 20rpx;
  200. text {
  201. font-size: 28rpx;
  202. color: #007aff;
  203. }
  204. }
  205. }
  206. </style>