index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <uni-shadow-root class="vant-search-index"><view :class="(utils.bem('search', { withaction: showAction || useActionSlot }))+' custom-class'" :style="'background: '+(background)">
  3. <view :class="utils.bem('search__content', [shape])">
  4. <view class="van-search__label" v-if="label">{{ label }}</view>
  5. <slot v-else name="label"></slot>
  6. <van-field type="search" :left-icon="(!useLeftIconSlot ? leftIcon : '')" :right-icon="(!useRightIconSlot ? rightIcon : '')" :focus="focus" :error="error" :border="false" confirm-type="search" class="van-search__field field-class" :value="value" :disabled="disabled" :readonly="readonly" :clearable="clearable" :maxlength="maxlength" :input-align="inputAlign" input-class="input-class" :placeholder="placeholder" :placeholder-style="placeholderStyle" custom-style="padding: 5px 10px 5px 0; background-color: transparent;" @blur="onBlur" @focus="onFocus" @change="onChange" @confirm="onSearch" @clear="onClear">
  7. <slot v-if="useLeftIconSlot" name="left-icon" slot="left-icon"></slot>
  8. <slot v-if="useRightIconSlot" name="right-icon" slot="right-icon"></slot>
  9. </van-field>
  10. </view>
  11. <view v-if="showAction || useActionSlot" class="van-search__action" hover-class="van-search__action--hover" hover-stay-time="70">
  12. <slot v-if="useActionSlot" name="action"></slot>
  13. <view v-else @click="onCancel" class="cancel-class">{{ actionText }}</view>
  14. </view>
  15. </view></uni-shadow-root>
  16. </template>
  17. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  18. <script>
  19. import VanField from '../field/index.vue'
  20. global['__wxVueOptions'] = {components:{'van-field': VanField}}
  21. global['__wxRoute'] = 'vant/search/index'
  22. import { VantComponent } from '../common/component';
  23. VantComponent({
  24. field: true,
  25. classes: ['field-class', 'input-class', 'cancel-class'],
  26. props: {
  27. label: String,
  28. focus: Boolean,
  29. error: Boolean,
  30. disabled: Boolean,
  31. readonly: Boolean,
  32. inputAlign: String,
  33. showAction: Boolean,
  34. useActionSlot: Boolean,
  35. useLeftIconSlot: Boolean,
  36. useRightIconSlot: Boolean,
  37. leftIcon: {
  38. type: String,
  39. value: 'search'
  40. },
  41. rightIcon: String,
  42. placeholder: String,
  43. placeholderStyle: String,
  44. actionText: {
  45. type: String,
  46. value: '取消'
  47. },
  48. background: {
  49. type: String,
  50. value: '#ffffff'
  51. },
  52. maxlength: {
  53. type: Number,
  54. value: -1
  55. },
  56. shape: {
  57. type: String,
  58. value: 'square'
  59. },
  60. clearable: {
  61. type: Boolean,
  62. value: true
  63. }
  64. },
  65. methods: {
  66. onChange(event) {
  67. this.setData({ value: event.detail });
  68. this.$emit('change', event.detail);
  69. },
  70. onCancel() {
  71. /**
  72. * 修复修改输入框值时,输入框失焦和赋值同时触发,赋值失效
  73. * https://github.com/youzan/@vant/weapp/issues/1768
  74. */
  75. setTimeout(() => {
  76. this.setData({ value: '' });
  77. this.$emit('cancel');
  78. this.$emit('change', '');
  79. }, 200);
  80. },
  81. onSearch() {
  82. this.$emit('search', this.data.value);
  83. },
  84. onFocus() {
  85. this.$emit('focus');
  86. },
  87. onBlur() {
  88. this.$emit('blur');
  89. },
  90. onClear() {
  91. this.$emit('clear');
  92. },
  93. }
  94. });
  95. export default global['__wxComponents']['vant/search/index']
  96. </script>
  97. <style platform="mp-weixin">
  98. @import '../common/index.css';.van-search{-webkit-align-items:center;align-items:center;box-sizing:border-box;padding:10px 12px;padding:var(--search-padding,10px 12px)}.van-search,.van-search__content{display:-webkit-flex;display:flex}.van-search__content{-webkit-flex:1;flex:1;padding-left:8px;padding-left:var(--padding-xs,8px);border-radius:2px;border-radius:var(--border-radius-sm,2px);background-color:#f7f8fa;background-color:var(--search-background-color,#f7f8fa)}.van-search__content--round{border-radius:17px;border-radius:calc(var(--search-input-height, 34px)/2)}.van-search__label{padding:0 5px;padding:var(--search-label-padding,0 5px);font-size:14px;font-size:var(--search-label-font-size,14px);line-height:34px;line-height:var(--search-input-height,34px);color:#323233;color:var(--search-label-color,#323233)}.van-search__field{-webkit-flex:1;flex:1}.van-search__field__left-icon{color:#969799;color:var(--search-left-icon-color,#969799)}.van-search--withaction{padding-right:0}.van-search__action{padding:0 8px;padding:var(--search-action-padding,0 8px);font-size:14px;font-size:var(--search-action-font-size,14px);line-height:34px;line-height:var(--search-input-height,34px);color:#323233;color:var(--search-action-text-color,#323233)}.van-search__action--hover{background-color:#f2f3f5;background-color:var(--active-color,#f2f3f5)}
  99. </style>