index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import { canIUseModel } from '../common/version';
  24. VantComponent({
  25. field: true,
  26. classes: ['field-class', 'input-class', 'cancel-class'],
  27. props: {
  28. label: String,
  29. focus: Boolean,
  30. error: Boolean,
  31. disabled: Boolean,
  32. readonly: Boolean,
  33. inputAlign: String,
  34. showAction: Boolean,
  35. useActionSlot: Boolean,
  36. useLeftIconSlot: Boolean,
  37. useRightIconSlot: Boolean,
  38. leftIcon: {
  39. type: String,
  40. value: 'search',
  41. },
  42. rightIcon: String,
  43. placeholder: String,
  44. placeholderStyle: String,
  45. actionText: {
  46. type: String,
  47. value: '取消',
  48. },
  49. background: {
  50. type: String,
  51. value: '#ffffff',
  52. },
  53. maxlength: {
  54. type: Number,
  55. value: -1,
  56. },
  57. shape: {
  58. type: String,
  59. value: 'square',
  60. },
  61. clearable: {
  62. type: Boolean,
  63. value: true,
  64. },
  65. },
  66. methods: {
  67. onChange(event) {
  68. if (canIUseModel()) {
  69. this.setData({ value: event.detail });
  70. }
  71. this.$emit('change', event.detail);
  72. },
  73. onCancel() {
  74. /**
  75. * 修复修改输入框值时,输入框失焦和赋值同时触发,赋值失效
  76. * https://github.com/youzan/@vant/weapp/issues/1768
  77. */
  78. setTimeout(() => {
  79. if (canIUseModel()) {
  80. this.setData({ value: '' });
  81. }
  82. this.$emit('cancel');
  83. this.$emit('change', '');
  84. }, 200);
  85. },
  86. onSearch(event) {
  87. this.$emit('search', event.detail);
  88. },
  89. onFocus(event) {
  90. this.$emit('focus', event.detail);
  91. },
  92. onBlur(event) {
  93. this.$emit('blur', event.detail);
  94. },
  95. onClear(event) {
  96. this.$emit('clear', event.detail);
  97. },
  98. },
  99. });
  100. export default global['__wxComponents']['vant/search/index']
  101. </script>
  102. <style platform="mp-weixin">
  103. @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)}
  104. </style>