index.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Button, Icon, Search } from 'vant';
  2. import { PropType, defineComponent, reactive, 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'],
  48. setup(props, { slots, emit }) {
  49. const forms = reactive({
  50. search: props.modelValue || ''
  51. });
  52. watch(
  53. () => props.modelValue,
  54. () => {
  55. forms.search = props.modelValue;
  56. }
  57. );
  58. return () => (
  59. <Search
  60. class={[styles['m-search'], styles[props.inputBackground]]}
  61. shape={props.shape}
  62. background={props.background}
  63. placeholder={props.placeholder}
  64. disabled={props.disabled}
  65. autofocus={props.autofocus}
  66. autocomplete="off"
  67. clearable={props.clearable}
  68. v-model={forms.search}
  69. clearTrigger="always"
  70. onClear={() => {
  71. console.log('clear');
  72. forms.search = '';
  73. emit('search', forms.search);
  74. }}
  75. onFocus={() => emit('focus')}
  76. onBlur={() => emit('blur', forms.search)}
  77. onSearch={() => emit('search', forms.search)}>
  78. {{
  79. left: () => slots.left && slots.left(),
  80. 'left-icon': () => (
  81. <Icon name={props.searchIcon} class={styles.leftIcon} />
  82. ),
  83. 'right-icon': () => (
  84. <Button
  85. disabled={props.disabled}
  86. class={styles.searchBtn}
  87. round
  88. type="primary"
  89. size="mini"
  90. onClick={() => emit('search', forms.search)}>
  91. 搜索
  92. </Button>
  93. )
  94. }}
  95. </Search>
  96. );
  97. }
  98. });