From 810c8e6f895e9a3fec080b4a4f70d907c0fe4b75 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Sat, 19 Jun 2021 21:51:29 +0800 Subject: [PATCH] refactor: descriptions --- components/descriptions/Row.tsx | 33 ++++++++++++------------ components/descriptions/index.tsx | 22 ++++++++++------ components/descriptions/style/index.less | 4 +-- v2-doc | 2 +- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/components/descriptions/Row.tsx b/components/descriptions/Row.tsx index f6f39e341..c20caaf6c 100644 --- a/components/descriptions/Row.tsx +++ b/components/descriptions/Row.tsx @@ -1,6 +1,6 @@ import Cell from './Cell'; -import { getOptionProps, getSlot, getClass, getStyle, getComponent } from '../_util/props-util'; -import { FunctionalComponent, VNode, inject } from 'vue'; +import { getSlot, getClass, getStyle } from '../_util/props-util'; +import { FunctionalComponent, VNode, inject, ref } from 'vue'; import { descriptionsContext, DescriptionsContextProp } from './index'; interface CellConfig { @@ -38,9 +38,8 @@ const Row: FunctionalComponent = props => { span = 1, labelStyle, contentStyle, - } = getOptionProps(item); - const label = getComponent(item, 'label'); - + label = (item.children as any)?.label?.(), + } = item.props || {}; const children = getSlot(item); const className = getClass(item); const style = getStyle(item); @@ -52,8 +51,8 @@ const Row: FunctionalComponent = props => { key={`${type}-${key || index}`} class={className} style={style} - labelStyle={{ ...rootLabelStyle, ...labelStyle }} - contentStyle={{ ...rootContentStyle, ...contentStyle }} + labelStyle={{ ...rootLabelStyle.value, ...labelStyle }} + contentStyle={{ ...rootContentStyle.value, ...contentStyle }} span={span} colon={colon} component={component} @@ -69,7 +68,7 @@ const Row: FunctionalComponent = props => { = props => { = props => { const { prefixCls, vertical, row, index, bordered } = props; const { labelStyle, contentStyle } = inject(descriptionsContext, { - labelStyle: undefined, - contentStyle: undefined, + labelStyle: ref({}), + contentStyle: ref({}), }); if (vertical) { return ( @@ -104,8 +103,8 @@ const Row: FunctionalComponent = props => { component: 'th', type: 'label', showLabel: true, - labelStyle: labelStyle.value, - contentStyle: contentStyle.value, + labelStyle, + contentStyle, })} @@ -113,8 +112,8 @@ const Row: FunctionalComponent = props => { component: 'td', type: 'content', showContent: true, - labelStyle: labelStyle.value, - contentStyle: contentStyle.value, + labelStyle, + contentStyle, })} @@ -128,8 +127,8 @@ const Row: FunctionalComponent = props => { type: 'item', showLabel: true, showContent: true, - labelStyle: labelStyle.value, - contentStyle: contentStyle.value, + labelStyle, + contentStyle, })} ); diff --git a/components/descriptions/index.tsx b/components/descriptions/index.tsx index a462363f9..c995726eb 100644 --- a/components/descriptions/index.tsx +++ b/components/descriptions/index.tsx @@ -13,6 +13,8 @@ import { CSSProperties, provide, toRef, + InjectionKey, + computed, } from 'vue'; import warning from '../_util/warning'; import ResponsiveObserve, { @@ -24,7 +26,7 @@ import Row from './Row'; import PropTypes from '../_util/vue-types'; import { tuple } from '../_util/type'; import { cloneElement } from '../_util/vnode'; -import { filterEmpty } from '../_util/props-util'; +import { flattenChildren } from '../_util/props-util'; import useConfigInject from '../_util/hooks/useConfigInject'; export const DescriptionsItemProps = { @@ -46,8 +48,9 @@ export type DescriptionsItemProp = Partial null; + slots: ['label'], + setup(_, { slots }) { + return () => slots.default?.(); }, }); @@ -96,7 +99,7 @@ function getFilledItem(node: VNode, span: number | undefined, rowRestCol: number } function getRows(children: VNode[], column: number) { - const childNodes = filterEmpty(children); + const childNodes = flattenChildren(children); const rows: VNode[][] = []; let tmpRow: VNode[] = []; @@ -151,11 +154,14 @@ export interface DescriptionsContextProp { contentStyle?: Ref; } -export const descriptionsContext = Symbol('descriptionsContext'); +export const descriptionsContext: InjectionKey = Symbol( + 'descriptionsContext', +); const Descriptions = defineComponent({ name: 'ADescriptions', props: descriptionsProps, + slots: ['title', 'extra'], Item: DescriptionsItem, setup(props, { slots }) { const { prefixCls, direction } = useConfigInject('descriptions', props); @@ -183,9 +189,10 @@ const Descriptions = defineComponent({ contentStyle: toRef(props, 'contentStyle'), }); + const mergeColumn = computed(() => getColumn(props.column, screens.value)); + return () => { const { - column, size, bordered = false, layout = 'horizontal', @@ -194,9 +201,8 @@ const Descriptions = defineComponent({ extra = slots.extra?.(), } = props; - const mergeColumn = getColumn(column, screens.value); const children = slots.default?.(); - const rows = getRows(children, mergeColumn); + const rows = getRows(children, mergeColumn.value); return (
th, > td { - padding-bottom: 12px; + padding-bottom: @padding-sm; } } } diff --git a/v2-doc b/v2-doc index 0f6d531d0..4c2982755 160000 --- a/v2-doc +++ b/v2-doc @@ -1 +1 @@ -Subproject commit 0f6d531d088d5283250c8cec1c7e8be0e0d36a36 +Subproject commit 4c298275518d5790a58d26f2ed9b83ee5ba1dba4