diff --git a/src/search/index.js b/src/search/index.js index 05af496f8..9e4eefebc 100644 --- a/src/search/index.js +++ b/src/search/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace } from '../utils'; +import { createNamespace, pick } from '../utils'; import { preventDefault } from '../utils/dom/event'; // Components @@ -32,34 +32,37 @@ export default createComponent({ }, }, - emits: ['search', 'cancel', 'keypress'], + emits: ['search', 'cancel'], - setup(props, { emit, slots }) { - return function () { - function Label() { - if (slots.label || props.label) { - return ( -
- {slots.label ? slots.label() : props.label} -
- ); - } + setup(props, { emit, slots, attrs }) { + const onCancel = () => { + if (!slots.action) { + emit('update:modelValue', ''); + emit('cancel'); } + }; - function Action() { - if (!props.showAction) { - return; - } + const onKeypress = (event) => { + const ENTER_CODE = 13; + if (event.keyCode === ENTER_CODE) { + preventDefault(event); + emit('search', props.modelValue); + } + }; - function onCancel() { - if (slots.action) { - return; - } - - emit('update:modelValue', ''); - emit('cancel'); - } + 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() : props.actionText || t('cancel')} + {slots.action ? slots.action() : text}
); } + }; - const fieldData = { - ...this.$attrs, + const fieldPropNames = [ + 'leftIcon', + 'rightIcon', + 'clearable', + 'modelValue', + 'clearTrigger', + ]; + + const renderField = () => { + const fieldAttrs = { + ...attrs, + ...pick(props, fieldPropNames), 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()} -
+ ); }; + + return () => ( +
+ {slots.left?.()} +
+ {renderLabel()} + {renderField()} +
+ {renderAction()} +
+ ); }, });