My_search.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view class="search-box">
  3. <view class="search" :style="{ background: background || '#F2F2F2' }">
  4. <icon class="icon" type="search" size="3.733vw" />
  5. <input :focus="inputFocus" v-model="value" class="input" :disabled="disabled" confirm-type="search"
  6. placeholder-style="font-size:3.733vw;" :placeholder="placeholder" type="text"
  7. @input="isInput ? onConfirm($event) : ''" @confirm="isInput ? '' : onConfirm($event)">
  8. <icon v-if="value" class="icon" type="clear" size="3.733vw" @click="onClear" />
  9. </view>
  10. <slot />
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. props: {
  16. placeholder: {
  17. type: String,
  18. default: "搜索关键字"
  19. },
  20. focus: {
  21. type: Boolean,
  22. default: true
  23. },
  24. onSearch: {
  25. type: Function
  26. },
  27. delay: {
  28. type: [String, Number],
  29. default: 350
  30. },
  31. disabled: {
  32. type: Boolean,
  33. default: false
  34. },
  35. background: {
  36. type: String
  37. },
  38. isInput: {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. watch: {
  44. focus: function (newVal) {
  45. setTimeout(() => {
  46. this.inputFocus = newVal;
  47. }, this.delay)
  48. }
  49. },
  50. data() {
  51. return {
  52. value: "",
  53. inputFocus: false,
  54. }
  55. },
  56. methods: {
  57. onConfirm() {
  58. this.$emit("onSearch", this.value)
  59. },
  60. onClear() {
  61. this.value = '';
  62. this.$emit("onSearch", this.value)
  63. }
  64. },
  65. }
  66. </script>
  67. <style lang="scss">
  68. .search-box {
  69. display: flex;
  70. width: 100%;
  71. box-sizing: border-box;
  72. .search {
  73. display: flex;
  74. align-items: center;
  75. flex: 1;
  76. height: 30px;
  77. border-radius: 50px;
  78. flex-shrink: 0;
  79. margin: 0 !important;
  80. .icon {
  81. padding: 10px;
  82. }
  83. .input {
  84. flex: 1;
  85. margin: 0 !important;
  86. }
  87. }
  88. }
  89. </style>