refactor: descriptions

pull/4232/head
tangjinzhou 3 years ago
parent 8e078d1e1c
commit 810c8e6f89

@ -1,6 +1,6 @@
import Cell from './Cell'; import Cell from './Cell';
import { getOptionProps, getSlot, getClass, getStyle, getComponent } from '../_util/props-util'; import { getSlot, getClass, getStyle } from '../_util/props-util';
import { FunctionalComponent, VNode, inject } from 'vue'; import { FunctionalComponent, VNode, inject, ref } from 'vue';
import { descriptionsContext, DescriptionsContextProp } from './index'; import { descriptionsContext, DescriptionsContextProp } from './index';
interface CellConfig { interface CellConfig {
@ -38,9 +38,8 @@ const Row: FunctionalComponent<RowProps> = props => {
span = 1, span = 1,
labelStyle, labelStyle,
contentStyle, contentStyle,
} = getOptionProps(item); label = (item.children as any)?.label?.(),
const label = getComponent(item, 'label'); } = item.props || {};
const children = getSlot(item); const children = getSlot(item);
const className = getClass(item); const className = getClass(item);
const style = getStyle(item); const style = getStyle(item);
@ -52,8 +51,8 @@ const Row: FunctionalComponent<RowProps> = props => {
key={`${type}-${key || index}`} key={`${type}-${key || index}`}
class={className} class={className}
style={style} style={style}
labelStyle={{ ...rootLabelStyle, ...labelStyle }} labelStyle={{ ...rootLabelStyle.value, ...labelStyle }}
contentStyle={{ ...rootContentStyle, ...contentStyle }} contentStyle={{ ...rootContentStyle.value, ...contentStyle }}
span={span} span={span}
colon={colon} colon={colon}
component={component} component={component}
@ -69,7 +68,7 @@ const Row: FunctionalComponent<RowProps> = props => {
<Cell <Cell
key={`label-${key || index}`} key={`label-${key || index}`}
class={className} class={className}
style={{ ...rootLabelStyle, ...style, ...labelStyle }} style={{ ...rootLabelStyle.value, ...style, ...labelStyle }}
span={1} span={1}
colon={colon} colon={colon}
component={component[0]} component={component[0]}
@ -80,7 +79,7 @@ const Row: FunctionalComponent<RowProps> = props => {
<Cell <Cell
key={`content-${key || index}`} key={`content-${key || index}`}
class={className} class={className}
style={{ ...rootContentStyle, ...style, ...contentStyle }} style={{ ...rootContentStyle.value, ...style, ...contentStyle }}
span={span * 2 - 1} span={span * 2 - 1}
component={component[1]} component={component[1]}
itemPrefixCls={itemPrefixCls} itemPrefixCls={itemPrefixCls}
@ -93,8 +92,8 @@ const Row: FunctionalComponent<RowProps> = props => {
const { prefixCls, vertical, row, index, bordered } = props; const { prefixCls, vertical, row, index, bordered } = props;
const { labelStyle, contentStyle } = inject(descriptionsContext, { const { labelStyle, contentStyle } = inject(descriptionsContext, {
labelStyle: undefined, labelStyle: ref({}),
contentStyle: undefined, contentStyle: ref({}),
}); });
if (vertical) { if (vertical) {
return ( return (
@ -104,8 +103,8 @@ const Row: FunctionalComponent<RowProps> = props => {
component: 'th', component: 'th',
type: 'label', type: 'label',
showLabel: true, showLabel: true,
labelStyle: labelStyle.value, labelStyle,
contentStyle: contentStyle.value, contentStyle,
})} })}
</tr> </tr>
<tr key={`content-${index}`} class={`${prefixCls}-row`}> <tr key={`content-${index}`} class={`${prefixCls}-row`}>
@ -113,8 +112,8 @@ const Row: FunctionalComponent<RowProps> = props => {
component: 'td', component: 'td',
type: 'content', type: 'content',
showContent: true, showContent: true,
labelStyle: labelStyle.value, labelStyle,
contentStyle: contentStyle.value, contentStyle,
})} })}
</tr> </tr>
</> </>
@ -128,8 +127,8 @@ const Row: FunctionalComponent<RowProps> = props => {
type: 'item', type: 'item',
showLabel: true, showLabel: true,
showContent: true, showContent: true,
labelStyle: labelStyle.value, labelStyle,
contentStyle: contentStyle.value, contentStyle,
})} })}
</tr> </tr>
); );

@ -13,6 +13,8 @@ import {
CSSProperties, CSSProperties,
provide, provide,
toRef, toRef,
InjectionKey,
computed,
} from 'vue'; } from 'vue';
import warning from '../_util/warning'; import warning from '../_util/warning';
import ResponsiveObserve, { import ResponsiveObserve, {
@ -24,7 +26,7 @@ import Row from './Row';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { tuple } from '../_util/type'; import { tuple } from '../_util/type';
import { cloneElement } from '../_util/vnode'; import { cloneElement } from '../_util/vnode';
import { filterEmpty } from '../_util/props-util'; import { flattenChildren } from '../_util/props-util';
import useConfigInject from '../_util/hooks/useConfigInject'; import useConfigInject from '../_util/hooks/useConfigInject';
export const DescriptionsItemProps = { export const DescriptionsItemProps = {
@ -46,8 +48,9 @@ export type DescriptionsItemProp = Partial<ExtractPropTypes<typeof descriptionsI
export const DescriptionsItem = defineComponent({ export const DescriptionsItem = defineComponent({
name: 'ADescriptionsItem', name: 'ADescriptionsItem',
props: descriptionsItemProp, props: descriptionsItemProp,
setup() { slots: ['label'],
return () => null; setup(_, { slots }) {
return () => slots.default?.();
}, },
}); });
@ -96,7 +99,7 @@ function getFilledItem(node: VNode, span: number | undefined, rowRestCol: number
} }
function getRows(children: VNode[], column: number) { function getRows(children: VNode[], column: number) {
const childNodes = filterEmpty(children); const childNodes = flattenChildren(children);
const rows: VNode[][] = []; const rows: VNode[][] = [];
let tmpRow: VNode[] = []; let tmpRow: VNode[] = [];
@ -151,11 +154,14 @@ export interface DescriptionsContextProp {
contentStyle?: Ref<CSSProperties>; contentStyle?: Ref<CSSProperties>;
} }
export const descriptionsContext = Symbol('descriptionsContext'); export const descriptionsContext: InjectionKey<DescriptionsContextProp> = Symbol(
'descriptionsContext',
);
const Descriptions = defineComponent({ const Descriptions = defineComponent({
name: 'ADescriptions', name: 'ADescriptions',
props: descriptionsProps, props: descriptionsProps,
slots: ['title', 'extra'],
Item: DescriptionsItem, Item: DescriptionsItem,
setup(props, { slots }) { setup(props, { slots }) {
const { prefixCls, direction } = useConfigInject('descriptions', props); const { prefixCls, direction } = useConfigInject('descriptions', props);
@ -183,9 +189,10 @@ const Descriptions = defineComponent({
contentStyle: toRef(props, 'contentStyle'), contentStyle: toRef(props, 'contentStyle'),
}); });
const mergeColumn = computed(() => getColumn(props.column, screens.value));
return () => { return () => {
const { const {
column,
size, size,
bordered = false, bordered = false,
layout = 'horizontal', layout = 'horizontal',
@ -194,9 +201,8 @@ const Descriptions = defineComponent({
extra = slots.extra?.(), extra = slots.extra?.(),
} = props; } = props;
const mergeColumn = getColumn(column, screens.value);
const children = slots.default?.(); const children = slots.default?.();
const rows = getRows(children, mergeColumn); const rows = getRows(children, mergeColumn.value);
return ( return (
<div <div

@ -1,4 +1,4 @@
@import '../../style/themes/default'; @import '../../style/themes/index';
@import '../../style/mixins/index'; @import '../../style/mixins/index';
@descriptions-prefix-cls: ~'@{ant-prefix}-descriptions'; @descriptions-prefix-cls: ~'@{ant-prefix}-descriptions';
@ -109,7 +109,7 @@
.@{descriptions-prefix-cls}-row { .@{descriptions-prefix-cls}-row {
> th, > th,
> td { > td {
padding-bottom: 12px; padding-bottom: @padding-sm;
} }
} }
} }

@ -1 +1 @@
Subproject commit 0f6d531d088d5283250c8cec1c7e8be0e0d36a36 Subproject commit 4c298275518d5790a58d26f2ed9b83ee5ba1dba4
Loading…
Cancel
Save