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 = () => {
setup(props, { slots, emit }) { const showLabel = slots.label || isDef(props.label);
return function () {
const { icon, size, title, label, value, isLink } = props;
const titleSlot = slots.title?.();
const showTitle = titleSlot || isDef(title);
function Label() {
const showLabel = slots.label || isDef(label);
if (showLabel) { if (showLabel) {
return ( return (
<div class={[bem('label'), props.labelClass]}> <div class={[bem('label'), props.labelClass]}>
{slots.label ? slots.label() : label} {slots.label ? slots.label() : props.label}
</div> </div>
); );
} }
} };
function Title() { const renderTitle = () => {
if (showTitle) { if (slots.title || isDef(props.title)) {
return ( return (
<div <div
class={[bem('title'), props.titleClass]} class={[bem('title'), props.titleClass]}
style={props.titleStyle} style={props.titleStyle}
> >
{slots.title ? titleSlot : <span>{title}</span>} {slots.title ? slots.title() : <span>{props.title}</span>}
{Label()} {renderLabel()}
</div> </div>
); );
} }
} };
function Value() { const renderValue = () => {
const showValue = slots.default || isDef(value); const hasTitle = slots.title || isDef(props.title);
const hasValue = slots.default || isDef(props.value);
if (showValue) { if (hasValue) {
return ( return (
<div <div class={[bem('value', { alone: !hasTitle }), props.valueClass]}>
class={[bem('value', { alone: !showTitle }), props.valueClass]} {slots.default ? slots.default() : <span>{props.value}</span>}
>
{slots.default ? slots.default() : <span>{value}</span>}
</div> </div>
); );
} }
} };
function LeftIcon() { const renderLeftIcon = () => {
if (slots.icon) { if (slots.icon) {
return slots.icon(); return slots.icon();
} }
if (icon) { if (props.icon) {
return ( return (
<Icon <Icon
name={props.icon}
class={bem('left-icon')} class={bem('left-icon')}
name={icon}
classPrefix={props.iconPrefix} 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 renderRightIcon = () => {
if (slots['right-icon']) {
return slots['right-icon']();
}
if (props.isLink) {
const name = props.arrowDirection
? `arrow-${props.arrowDirection}`
: 'arrow';
return <Icon name={name} class={bem('right-icon')} />;
}
};
return (vm) => {
const { size, center, border, isLink, required } = props;
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>
); );