index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Button, Icon, Search } from 'vant';
  2. import { PropType, defineComponent, reactive, ref, watch } from 'vue';
  3. import styles from './index.module.less';
  4. import iconSearch from '@/common/images/icon-search.svg';
  5. type inputBackground = 'default' | 'white' | 'transparent';
  6. export default defineComponent({
  7. name: 'm-search',
  8. props: {
  9. modelValue: {
  10. type: String,
  11. default: ''
  12. },
  13. shape: {
  14. type: String as PropType<'round' | 'square'>,
  15. default: 'round'
  16. },
  17. disabled: {
  18. type: Boolean,
  19. default: false
  20. },
  21. clearable: {
  22. type: Boolean,
  23. default: true
  24. },
  25. autofocus: {
  26. // ios系统不支持
  27. type: Boolean,
  28. default: false
  29. },
  30. placeholder: {
  31. type: String,
  32. default: '请输入搜索关键词'
  33. },
  34. background: {
  35. type: String,
  36. default: '#fff'
  37. },
  38. searchIcon: {
  39. type: String,
  40. default: iconSearch
  41. },
  42. inputBackground: {
  43. type: String as PropType<inputBackground>,
  44. default: 'default'
  45. }
  46. },
  47. emits: ['search', 'focus', 'blur', 'searchUpdate'],
  48. setup(props, { slots, emit, expose }) {
  49. const forms = reactive({
  50. search: props.modelValue || '',
  51. });
  52. const searchRef = ref()
  53. watch(
  54. () => props.modelValue,
  55. () => {
  56. forms.search = props.modelValue;
  57. }
  58. );
  59. const searchBlur = () => {
  60. searchRef.value?.blur()
  61. }
  62. expose({
  63. searchBlur
  64. })
  65. return () => (
  66. <Search
  67. class={[styles['m-search'], styles[props.inputBackground]]}
  68. ref={searchRef}
  69. shape={props.shape}
  70. background={props.background}
  71. placeholder={props.placeholder}
  72. disabled={props.disabled}
  73. autofocus={props.autofocus}
  74. autocomplete="off"
  75. clearable={props.clearable}
  76. v-model={forms.search}
  77. clearTrigger="always"
  78. onClear={() => {
  79. console.log('clear');
  80. forms.search = '';
  81. emit('search', forms.search);
  82. }}
  83. onFocus={() => emit('focus')}
  84. onBlur={() => emit('blur', forms.search)}
  85. onSearch={() => emit('search', forms.search)}
  86. onUpdate:modelValue={(val: any) => {
  87. // 输入框内容变化时触发
  88. // console.log('搜索内容变化',val)
  89. emit('searchUpdate', val);
  90. }}>
  91. {{
  92. left: () => slots.left && slots.left(),
  93. 'left-icon': () => (
  94. <Icon name={props.searchIcon} class={styles.leftIcon} />
  95. ),
  96. 'right-icon': () => (
  97. <Button
  98. disabled={props.disabled}
  99. class={styles.searchBtn}
  100. round
  101. type="primary"
  102. size="mini"
  103. onClick={() => emit('search', forms.search)}>
  104. 搜索
  105. </Button>
  106. )
  107. }}
  108. </Search>
  109. );
  110. }
  111. });