index.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.png';
  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. autofocus: {
  22. // ios系统不支持
  23. type: Boolean,
  24. default: false
  25. },
  26. placeholder: {
  27. type: String,
  28. default: '请输入搜索关键词'
  29. },
  30. background: {
  31. type: String,
  32. default: '#fff'
  33. },
  34. inputBackground: {
  35. type: String as PropType<inputBackground>,
  36. default: 'default'
  37. }
  38. },
  39. emits: ['search'],
  40. setup(props, { slots, emit }) {
  41. const forms = reactive({
  42. search: props.modelValue || ''
  43. });
  44. watch(
  45. () => props.modelValue,
  46. () => {
  47. forms.search = props.modelValue;
  48. }
  49. );
  50. return () => (
  51. <Search
  52. class={[styles['m-search'], styles[props.inputBackground]]}
  53. shape={props.shape}
  54. background={props.background}
  55. placeholder={props.placeholder}
  56. disabled={props.disabled}
  57. autofocus={props.autofocus}
  58. autocomplete="off"
  59. v-model={forms.search}
  60. clearTrigger="always"
  61. onClear={() => {
  62. console.log('clear');
  63. forms.search = '';
  64. emit('search', forms.search);
  65. }}
  66. onSearch={() => emit('search', forms.search)}>
  67. {{
  68. left: () => slots.left && slots.left(),
  69. 'left-icon': () => <Icon name={iconSearch} class={styles.leftIcon} />,
  70. 'right-icon': () => (
  71. <Button
  72. disabled={props.disabled}
  73. class={styles.searchBtn}
  74. round
  75. type="primary"
  76. size="mini"
  77. onClick={() => emit('search', forms.search)}>
  78. 搜索
  79. </Button>
  80. )
  81. }}
  82. </Search>
  83. );
  84. }
  85. });