ut-search.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <!-- 搜索框 -->
  3. <view class="search-input d-flex a-c" :class="{ 'up-border': border }" :style="{ margin, background: bgColor, height, borderRadius }">
  4. <u-input v-model="value" ref="searchInputRef" cursor cursorColor="#333" clearable type="text" :focus="focused" border="none" @change="inputSearch" @clear="clear" @confirm="search" confirmType="search" :fontSize="fontSize" :maxlength="maxlength" :placeholder="placeholder">
  5. <template #suffix>
  6. <view @click.stop="search" class="d-flex j-c a-c" style="padding: 0 16rpx">
  7. <image class="search_icon" src="/static/images/plant/search_icon.png" mode="widthFix" />
  8. </view>
  9. </template>
  10. </u-input>
  11. </view>
  12. </template>
  13. <script setup name="search">
  14. import { defineProps, defineEmits, ref, watch } from 'vue';
  15. import { debounce } from 'uview-plus';
  16. const props = defineProps({
  17. modelValue: {
  18. type: String,
  19. default: '',
  20. },
  21. margin: {
  22. type: String,
  23. default: '24rpx',
  24. },
  25. placeholder: {
  26. type: String,
  27. default: '请输入搜索内容',
  28. },
  29. maxlength: {
  30. type: Number,
  31. default: 140,
  32. },
  33. fontSize: {
  34. type: String,
  35. default: '26rpx',
  36. },
  37. height: {
  38. type: String,
  39. default: '86rpx',
  40. },
  41. border: {
  42. type: Boolean,
  43. default: true,
  44. },
  45. // 背景颜色
  46. bgColor: {
  47. type: String,
  48. default: '#fff',
  49. },
  50. borderRadius: {
  51. type: String,
  52. default: '84rpx',
  53. },
  54. });
  55. const value = ref(props.modelValue);
  56. const emit = defineEmits(['search', 'update:modelValue', 'change']);
  57. const inputSearch = (e) => {
  58. debounce(() => {
  59. emit('update:modelValue', e);
  60. emit('change', e);
  61. }, 500);
  62. };
  63. const search = (e) => {
  64. debounce(() => {
  65. emit('update:modelValue', value.value);
  66. emit('search', value.value);
  67. }, 500);
  68. };
  69. const clear = () => {
  70. emit('update:modelValue', '');
  71. emit('change', '');
  72. emit('search', '');
  73. };
  74. const searchInputRef = ref(null);
  75. const focused = ref(false);
  76. const doFocus = () => {
  77. focused.value = true;
  78. // searchInputRef.value.doFocus();
  79. };
  80. // 监听输入框的输入事件,触发更新modelValue的值
  81. watch(
  82. () => props.modelValue,
  83. (val) => {
  84. value.value = val;
  85. }
  86. );
  87. defineExpose({
  88. doFocus,
  89. });
  90. </script>
  91. <style lang="scss" scoped>
  92. .search-input {
  93. height: 86rpx;
  94. background-color: #fff;
  95. padding-left: 20rpx;
  96. }
  97. .search_icon {
  98. width: 40rpx;
  99. height: 40rpx;
  100. }
  101. </style>