import { ref, PropType, defineComponent } from 'vue'; // Utils import { pick, extend, truthProp, createNamespace, preventDefault, ComponentInstance, } from '../utils'; import { fieldProps } from '../field/Field'; // Composables import { useExpose } from '../composables/use-expose'; // Components import { Field } from '../field'; const [name, bem, t] = createNamespace('search'); export type SearchShape = 'square' | 'round'; export default defineComponent({ name, props: extend({}, fieldProps, { label: String, clearable: truthProp, actionText: String, background: String, showAction: Boolean, shape: { type: String as PropType, default: 'square', }, leftIcon: { type: String, default: 'search', }, }), emits: ['search', 'cancel', 'update:modelValue'], setup(props, { emit, slots, attrs }) { const filedRef = ref(); const onCancel = () => { if (!slots.action) { emit('update:modelValue', ''); emit('cancel'); } }; const onKeypress = (event: KeyboardEvent) => { const ENTER_CODE = 13; if (event.keyCode === ENTER_CODE) { preventDefault(event); emit('search', props.modelValue); } }; const renderLabel = () => { if (slots.label || props.label) { return (
{slots.label ? slots.label() : props.label}
); } }; const renderAction = () => { if (props.showAction) { const text = props.actionText || t('cancel'); return (
{slots.action ? slots.action() : text}
); } }; const blur = () => filedRef.value?.blur(); const focus = () => filedRef.value?.focus(); const fieldPropNames = Object.keys(fieldProps) as Array< keyof typeof fieldProps >; const renderField = () => { const fieldAttrs = extend({}, attrs, pick(props, fieldPropNames)); const onInput = (value: string) => emit('update:modelValue', value); return ( ); }; useExpose({ focus, blur }); return () => (
{slots.left?.()}
{renderLabel()} {renderField()}
{renderAction()}
); }, });