index.vue 3.7KB

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