index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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" :clear-trigger="clearTrigger" :clear-icon="clearIcon" :maxlength="maxlength" :input-align="inputAlign" input-class="input-class" :placeholder="placeholder" :placeholder-style="placeholderStyle" :cursor-spacing="cursorSpacing" 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="van-search__action-button 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. value: {
  29. type: String,
  30. value: '',
  31. },
  32. label: String,
  33. focus: Boolean,
  34. error: Boolean,
  35. disabled: Boolean,
  36. readonly: Boolean,
  37. inputAlign: String,
  38. showAction: Boolean,
  39. useActionSlot: Boolean,
  40. useLeftIconSlot: Boolean,
  41. useRightIconSlot: Boolean,
  42. leftIcon: {
  43. type: String,
  44. value: 'search',
  45. },
  46. rightIcon: String,
  47. placeholder: String,
  48. placeholderStyle: String,
  49. actionText: {
  50. type: String,
  51. value: '取消',
  52. },
  53. background: {
  54. type: String,
  55. value: '#ffffff',
  56. },
  57. maxlength: {
  58. type: Number,
  59. value: -1,
  60. },
  61. shape: {
  62. type: String,
  63. value: 'square',
  64. },
  65. clearable: {
  66. type: Boolean,
  67. value: true,
  68. },
  69. clearTrigger: {
  70. type: String,
  71. value: 'focus',
  72. },
  73. clearIcon: {
  74. type: String,
  75. value: 'clear',
  76. },
  77. cursorSpacing: {
  78. type: Number,
  79. value: 0,
  80. },
  81. },
  82. methods: {
  83. onChange(event) {
  84. if (canIUseModel()) {
  85. this.setData({ value: event.detail });
  86. }
  87. this.$emit('change', event.detail);
  88. },
  89. onCancel() {
  90. /**
  91. * 修复修改输入框值时,输入框失焦和赋值同时触发,赋值失效
  92. * https://github.com/youzan/vant-weapp/issues/1768
  93. */
  94. setTimeout(() => {
  95. if (canIUseModel()) {
  96. this.setData({ value: '' });
  97. }
  98. this.$emit('cancel');
  99. this.$emit('change', '');
  100. }, 200);
  101. },
  102. onSearch(event) {
  103. this.$emit('search', event.detail);
  104. },
  105. onFocus(event) {
  106. this.$emit('focus', event.detail);
  107. },
  108. onBlur(event) {
  109. this.$emit('blur', event.detail);
  110. },
  111. onClear(event) {
  112. this.$emit('clear', event.detail);
  113. },
  114. onClickInput(event) {
  115. this.$emit('click-input', event.detail);
  116. },
  117. },
  118. });
  119. export default global['__wxComponents']['vant/search/index']
  120. </script>
  121. <style platform="mp-weixin">
  122. @import '../common/index.css';.van-search{align-items:center;box-sizing:border-box;display:flex;padding:var(--search-padding,10px 12px)}.van-search__content{background-color:var(--search-background-color,#f7f8fa);border-radius:2px;display:flex;flex:1;padding-left:var(--padding-sm,12px)}.van-search__content--round{border-radius:999px}.van-search__label{color:var(--search-label-color,#323233);font-size:var(--search-label-font-size,14px);line-height:var(--search-input-height,34px);padding:var(--search-label-padding,0 5px)}.van-search__field{flex:1}.van-search__field__left-icon{color:var(--search-left-icon-color,#969799)}.van-search--withaction{padding-right:0}.van-search__action{color:var(--search-action-text-color,#323233);font-size:var(--search-action-font-size,14px);line-height:var(--search-input-height,34px)}.van-search__action--hover{background-color:#f2f3f5}.van-search__action-button{padding:var(--search-action-padding,0 8px)}
  123. </style>