From 9c6ae2c28b8e5bc228dee765449c3ded3c305da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Mon, 16 Dec 2019 11:45:28 +0800 Subject: [PATCH] chore(Cell): improve jsx segment --- src/cell/index.tsx | 95 ++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/src/cell/index.tsx b/src/cell/index.tsx index 01d660391..78a512e7d 100644 --- a/src/cell/index.tsx +++ b/src/cell/index.tsx @@ -31,44 +31,72 @@ function Cell( slots: CellSlots, ctx: RenderContext ) { - const { icon, size, title, label, value, isLink, arrowDirection } = props; - + const { icon, size, title, label, value, isLink } = props; const showTitle = slots.title || isDef(title); - const showValue = slots.default || isDef(value); - const showLabel = slots.label || isDef(label); - const Label = showLabel && ( -
- {slots.label ? slots.label() : label} -
- ); + function Label() { + const showLabel = slots.label || isDef(label); - const Title = showTitle && ( -
- {slots.title ? slots.title() : {title}} - {Label} -
- ); + if (showLabel) { + return ( +
+ {slots.label ? slots.label() : label} +
+ ); + } + } - const Value = showValue && ( -
- {slots.default ? slots.default() : {value}} -
- ); + function Title() { + if (showTitle) { + return ( +
+ {slots.title ? slots.title() : {title}} + {Label()} +
+ ); + } + } - const LeftIcon = slots.icon - ? slots.icon() - : icon && ; + function Value() { + const showValue = slots.default || isDef(value); - const rightIconSlot = slots['right-icon']; - const RightIcon = rightIconSlot - ? rightIconSlot() - : isLink && ( + if (showValue) { + return ( +
+ {slots.default ? slots.default() : {value}} +
+ ); + } + } + + function LeftIcon() { + if (slots.icon) { + return slots.icon(); + } + + if (icon) { + return ; + } + } + + function RightIcon() { + const rightIconSlot = slots['right-icon']; + + if (rightIconSlot) { + return rightIconSlot(); + } + + if (isLink) { + const { arrowDirection } = props; + + return ( - ); + ); + } + } function onClick(event: Event) { emit(ctx, 'click', event); @@ -76,6 +104,7 @@ function Cell( } const clickable = isLink || props.clickable; + const classes: Mods = { clickable, center: props.center, @@ -95,11 +124,11 @@ function Cell( onClick={onClick} {...inherit(ctx)} > - {LeftIcon} - {Title} - {Value} - {RightIcon} - {slots.extra && slots.extra()} + {LeftIcon()} + {Title()} + {Value()} + {RightIcon()} + {slots.extra?.()} ); }