// Utils import { createNamespace } from '../utils'; import { preventDefault } from '../utils/dom/event'; // Components import Field from '../field'; const [createComponent, bem, t] = createNamespace('search'); export default createComponent({ inheritAttrs: false, props: { label: String, rightIcon: String, modelValue: String, actionText: String, background: String, showAction: Boolean, clearTrigger: String, shape: { type: String, default: 'square', }, clearable: { type: Boolean, default: true, }, leftIcon: { type: String, default: 'search', }, }, emits: ['search', 'cancel', 'keypress'], setup(props, { emit, slots }) { return function () { function Label() { if (slots.label || props.label) { return (
{slots.label ? slots.label() : props.label}
); } } function Action() { if (!props.showAction) { return; } function onCancel() { if (slots.action) { return; } emit('update:modelValue', ''); emit('cancel'); } return (
{slots.action ? slots.action() : props.actionText || t('cancel')}
); } const fieldData = { ...this.$attrs, style: null, class: null, onKeypress(event) { // press enter if (event.keyCode === 13) { preventDefault(event); emit('search', props.modelValue); } emit('keypress', event); }, }; return (
{slots.left?.()}
{Label()}
{Action()}
); }; }, });