refactor(Search): extract render fns

This commit is contained in:
chenjiahan 2020-08-25 16:46:02 +08:00
parent 7b4286b9d5
commit 32ae8a5fa3

View File

@ -1,5 +1,5 @@
// Utils // Utils
import { createNamespace } from '../utils'; import { createNamespace, pick } from '../utils';
import { preventDefault } from '../utils/dom/event'; import { preventDefault } from '../utils/dom/event';
// Components // Components
@ -32,34 +32,37 @@ export default createComponent({
}, },
}, },
emits: ['search', 'cancel', 'keypress'], emits: ['search', 'cancel'],
setup(props, { emit, slots }) { setup(props, { emit, slots, attrs }) {
return function () { const onCancel = () => {
function Label() { if (!slots.action) {
if (slots.label || props.label) { emit('update:modelValue', '');
return ( emit('cancel');
<div class={bem('label')}>
{slots.label ? slots.label() : props.label}
</div>
);
}
} }
};
function Action() { const onKeypress = (event) => {
if (!props.showAction) { const ENTER_CODE = 13;
return; if (event.keyCode === ENTER_CODE) {
} preventDefault(event);
emit('search', props.modelValue);
}
};
function onCancel() { const renderLabel = () => {
if (slots.action) { if (slots.label || props.label) {
return; return (
} <div class={bem('label')}>
{slots.label ? slots.label() : props.label}
emit('update:modelValue', ''); </div>
emit('cancel'); );
} }
};
const renderAction = () => {
if (props.showAction) {
const text = props.actionText || t('cancel');
return ( return (
<div <div
class={bem('action')} class={bem('action')}
@ -67,51 +70,51 @@ export default createComponent({
tabindex="0" tabindex="0"
onClick={onCancel} onClick={onCancel}
> >
{slots.action ? slots.action() : props.actionText || t('cancel')} {slots.action ? slots.action() : text}
</div> </div>
); );
} }
};
const fieldData = { const fieldPropNames = [
...this.$attrs, 'leftIcon',
'rightIcon',
'clearable',
'modelValue',
'clearTrigger',
];
const renderField = () => {
const fieldAttrs = {
...attrs,
...pick(props, fieldPropNames),
style: null, style: null,
class: null, class: null,
onKeypress(event) {
// press enter
if (event.keyCode === 13) {
preventDefault(event);
emit('search', props.modelValue);
}
emit('keypress', event);
},
}; };
return ( return (
<div <Field
class={[bem({ 'show-action': props.showAction }), this.$attrs.class]} v-slots={pick(slots, ['left-icon', 'right-icon'])}
style={{ background: props.background, ...this.$attrs.style }} type="search"
> border={false}
{slots.left?.()} onKeypress={onKeypress}
<div class={bem('content', props.shape)}> {...fieldAttrs}
{Label()} />
<Field
v-slots={{
'left-icon': slots['left-icon'],
'right-icon': slots['right-icon'],
}}
type="search"
border={false}
leftIcon={props.leftIcon}
rightIcon={props.rightIcon}
clearable={props.clearable}
modelValue={props.modelValue}
clearTrigger={props.clearTrigger}
{...fieldData}
/>
</div>
{Action()}
</div>
); );
}; };
return () => (
<div
class={[bem({ 'show-action': props.showAction }), attrs.class]}
style={{ background: props.background, ...attrs.style }}
>
{slots.left?.()}
<div class={bem('content', props.shape)}>
{renderLabel()}
{renderField()}
</div>
{renderAction()}
</div>
);
}, },
}); });