refactor(Cell): extract render function

This commit is contained in:
chenjiahan 2020-08-24 19:41:18 +08:00
parent be5f161799
commit 53109895a9

View File

@ -14,107 +14,93 @@ export default createComponent({
...routeProps, ...routeProps,
}, },
emits: ['click'], setup(props, { slots }) {
const renderLabel = () => {
const showLabel = slots.label || isDef(props.label);
setup(props, { slots, emit }) { if (showLabel) {
return function () { return (
const { icon, size, title, label, value, isLink } = props; <div class={[bem('label'), props.labelClass]}>
const titleSlot = slots.title?.(); {slots.label ? slots.label() : props.label}
const showTitle = titleSlot || isDef(title); </div>
);
}
};
function Label() { const renderTitle = () => {
const showLabel = slots.label || isDef(label); if (slots.title || isDef(props.title)) {
return (
<div
class={[bem('title'), props.titleClass]}
style={props.titleStyle}
>
{slots.title ? slots.title() : <span>{props.title}</span>}
{renderLabel()}
</div>
);
}
};
if (showLabel) { const renderValue = () => {
return ( const hasTitle = slots.title || isDef(props.title);
<div class={[bem('label'), props.labelClass]}> const hasValue = slots.default || isDef(props.value);
{slots.label ? slots.label() : label}
</div> if (hasValue) {
); return (
} <div class={[bem('value', { alone: !hasTitle }), props.valueClass]}>
{slots.default ? slots.default() : <span>{props.value}</span>}
</div>
);
}
};
const renderLeftIcon = () => {
if (slots.icon) {
return slots.icon();
} }
function Title() { if (props.icon) {
if (showTitle) { return (
return ( <Icon
<div name={props.icon}
class={[bem('title'), props.titleClass]} class={bem('left-icon')}
style={props.titleStyle} classPrefix={props.iconPrefix}
> />
{slots.title ? titleSlot : <span>{title}</span>} );
{Label()} }
</div> };
);
} const renderRightIcon = () => {
if (slots['right-icon']) {
return slots['right-icon']();
} }
function Value() { if (props.isLink) {
const showValue = slots.default || isDef(value); const name = props.arrowDirection
? `arrow-${props.arrowDirection}`
if (showValue) { : 'arrow';
return ( return <Icon name={name} class={bem('right-icon')} />;
<div
class={[bem('value', { alone: !showTitle }), props.valueClass]}
>
{slots.default ? slots.default() : <span>{value}</span>}
</div>
);
}
} }
};
function LeftIcon() { return (vm) => {
if (slots.icon) { const { size, center, border, isLink, required } = props;
return slots.icon();
}
if (icon) {
return (
<Icon
class={bem('left-icon')}
name={icon}
classPrefix={props.iconPrefix}
/>
);
}
}
function RightIcon() {
const rightIconSlot = slots['right-icon'];
if (rightIconSlot) {
return rightIconSlot();
}
if (isLink) {
const { arrowDirection } = props;
return (
<Icon
class={bem('right-icon')}
name={arrowDirection ? `arrow-${arrowDirection}` : 'arrow'}
/>
);
}
}
const onClick = (event) => {
emit('click', event);
route(this.$router, this);
};
const clickable = isLink || props.clickable; const clickable = isLink || props.clickable;
const classes = { const classes = {
center,
required,
clickable, clickable,
center: props.center, borderless: !border,
required: props.required,
borderless: !props.border,
}; };
if (size) { if (size) {
classes[size] = size; classes[size] = size;
} }
const onClick = () => {
route(vm.$router, vm);
};
return ( return (
<div <div
class={bem(classes)} class={bem(classes)}
@ -122,10 +108,10 @@ export default createComponent({
tabindex={clickable ? 0 : null} tabindex={clickable ? 0 : null}
onClick={onClick} onClick={onClick}
> >
{LeftIcon()} {renderLeftIcon()}
{Title()} {renderTitle()}
{Value()} {renderValue()}
{RightIcon()} {renderRightIcon()}
{slots.extra?.()} {slots.extra?.()}
</div> </div>
); );