index.vue 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <uni-shadow-root class="vant-dist-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" :clear-trigger="clearTrigger" :clear-icon="clearIcon" :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" @click-input="onClickInput">
  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/dist/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. clearTrigger: {
  66. type: String,
  67. value: 'focus',
  68. },
  69. clearIcon: {
  70. type: String,
  71. value: 'clear',
  72. },
  73. },
  74. methods: {
  75. onChange(event) {
  76. if (canIUseModel()) {
  77. this.setData({ value: event.detail });
  78. }
  79. this.$emit('change', event.detail);
  80. },
  81. onCancel() {
  82. /**
  83. * 修复修改输入框值时,输入框失焦和赋值同时触发,赋值失效
  84. * https://github.com/youzan/@vant/weapp/issues/1768
  85. */
  86. setTimeout(() => {
  87. if (canIUseModel()) {
  88. this.setData({ value: '' });
  89. }
  90. this.$emit('cancel');
  91. this.$emit('change', '');
  92. }, 200);
  93. },
  94. onSearch(event) {
  95. this.$emit('search', event.detail);
  96. },
  97. onFocus(event) {
  98. this.$emit('focus', event.detail);
  99. },
  100. onBlur(event) {
  101. this.$emit('blur', event.detail);
  102. },
  103. onClear(event) {
  104. this.$emit('clear', event.detail);
  105. },
  106. onClickInput(event) {
  107. this.$emit('click-input', event.detail);
  108. },
  109. },
  110. });
  111. export default global['__wxComponents']['vant/dist/search/index']
  112. </script>
  113. <style platform="mp-weixin">
  114. @import '../common/index.css';.van-search{display:flex;align-items:center;box-sizing:border-box;padding:10px 12px;padding:var(--search-padding,10px 12px)}.van-search__content{display:flex;flex:1;padding-left:12px;padding-left:var(--padding-sm,12px);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:999px;border-radius:var(--border-radius-max,999px)}.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{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)}
  115. </style>