| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import { Button, Icon, Search } from 'vant';
- import { PropType, defineComponent, reactive, watch } from 'vue';
- import styles from './index.module.less';
- import iconSearch from '@/common/images/icon-search.png';
- type inputBackground = 'default' | 'white' | 'transparent';
- export default defineComponent({
- name: 'm-search',
- props: {
- modelValue: {
- type: String,
- default: ''
- },
- shape: {
- type: String as PropType<'round' | 'square'>,
- default: 'round'
- },
- disabled: {
- type: Boolean,
- default: false
- },
- autofocus: {
- // ios系统不支持
- type: Boolean,
- default: false
- },
- placeholder: {
- type: String,
- default: '请输入搜索关键词'
- },
- background: {
- type: String,
- default: '#fff'
- },
- inputBackground: {
- type: String as PropType<inputBackground>,
- default: 'default'
- }
- },
- emits: ['search'],
- setup(props, { slots, emit }) {
- const forms = reactive({
- search: props.modelValue || ''
- });
- watch(
- () => props.modelValue,
- () => {
- forms.search = props.modelValue;
- }
- );
- return () => (
- <Search
- class={[styles['m-search'], styles[props.inputBackground]]}
- shape={props.shape}
- background={props.background}
- placeholder={props.placeholder}
- disabled={props.disabled}
- autofocus={props.autofocus}
- autocomplete="off"
- v-model={forms.search}
- clearTrigger="always"
- onClear={() => {
- console.log('clear');
- forms.search = '';
- emit('search', forms.search);
- }}
- onSearch={() => emit('search', forms.search)}>
- {{
- left: () => slots.left && slots.left(),
- 'left-icon': () => <Icon name={iconSearch} class={styles.leftIcon} />,
- 'right-icon': () => (
- <Button
- disabled={props.disabled}
- class={styles.searchBtn}
- round
- type="primary"
- size="mini"
- onClick={() => emit('search', forms.search)}>
- 搜索
- </Button>
- )
- }}
- </Search>
- );
- }
- });
|