Skip to content

Commit 810c8e6

Browse files
committed
refactor: descriptions
1 parent 8e078d1 commit 810c8e6

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

components/descriptions/Row.tsx

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Cell from './Cell';
2-
import { getOptionProps, getSlot, getClass, getStyle, getComponent } from '../_util/props-util';
3-
import { FunctionalComponent, VNode, inject } from 'vue';
2+
import { getSlot, getClass, getStyle } from '../_util/props-util';
3+
import { FunctionalComponent, VNode, inject, ref } from 'vue';
44
import { descriptionsContext, DescriptionsContextProp } from './index';
55

66
interface CellConfig {
@@ -38,9 +38,8 @@ const Row: FunctionalComponent<RowProps> = props => {
3838
span = 1,
3939
labelStyle,
4040
contentStyle,
41-
} = getOptionProps(item);
42-
const label = getComponent(item, 'label');
43-
41+
label = (item.children as any)?.label?.(),
42+
} = item.props || {};
4443
const children = getSlot(item);
4544
const className = getClass(item);
4645
const style = getStyle(item);
@@ -52,8 +51,8 @@ const Row: FunctionalComponent<RowProps> = props => {
5251
key={`${type}-${key || index}`}
5352
class={className}
5453
style={style}
55-
labelStyle={{ ...rootLabelStyle, ...labelStyle }}
56-
contentStyle={{ ...rootContentStyle, ...contentStyle }}
54+
labelStyle={{ ...rootLabelStyle.value, ...labelStyle }}
55+
contentStyle={{ ...rootContentStyle.value, ...contentStyle }}
5756
span={span}
5857
colon={colon}
5958
component={component}
@@ -69,7 +68,7 @@ const Row: FunctionalComponent<RowProps> = props => {
6968
<Cell
7069
key={`label-${key || index}`}
7170
class={className}
72-
style={{ ...rootLabelStyle, ...style, ...labelStyle }}
71+
style={{ ...rootLabelStyle.value, ...style, ...labelStyle }}
7372
span={1}
7473
colon={colon}
7574
component={component[0]}
@@ -80,7 +79,7 @@ const Row: FunctionalComponent<RowProps> = props => {
8079
<Cell
8180
key={`content-${key || index}`}
8281
class={className}
83-
style={{ ...rootContentStyle, ...style, ...contentStyle }}
82+
style={{ ...rootContentStyle.value, ...style, ...contentStyle }}
8483
span={span * 2 - 1}
8584
component={component[1]}
8685
itemPrefixCls={itemPrefixCls}
@@ -93,8 +92,8 @@ const Row: FunctionalComponent<RowProps> = props => {
9392

9493
const { prefixCls, vertical, row, index, bordered } = props;
9594
const { labelStyle, contentStyle } = inject(descriptionsContext, {
96-
labelStyle: undefined,
97-
contentStyle: undefined,
95+
labelStyle: ref({}),
96+
contentStyle: ref({}),
9897
});
9998
if (vertical) {
10099
return (
@@ -104,17 +103,17 @@ const Row: FunctionalComponent<RowProps> = props => {
104103
component: 'th',
105104
type: 'label',
106105
showLabel: true,
107-
labelStyle: labelStyle.value,
108-
contentStyle: contentStyle.value,
106+
labelStyle,
107+
contentStyle,
109108
})}
110109
</tr>
111110
<tr key={`content-${index}`} class={`${prefixCls}-row`}>
112111
{renderCells(row, props, {
113112
component: 'td',
114113
type: 'content',
115114
showContent: true,
116-
labelStyle: labelStyle.value,
117-
contentStyle: contentStyle.value,
115+
labelStyle,
116+
contentStyle,
118117
})}
119118
</tr>
120119
</>
@@ -128,8 +127,8 @@ const Row: FunctionalComponent<RowProps> = props => {
128127
type: 'item',
129128
showLabel: true,
130129
showContent: true,
131-
labelStyle: labelStyle.value,
132-
contentStyle: contentStyle.value,
130+
labelStyle,
131+
contentStyle,
133132
})}
134133
</tr>
135134
);

components/descriptions/index.tsx

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
CSSProperties,
1414
provide,
1515
toRef,
16+
InjectionKey,
17+
computed,
1618
} from 'vue';
1719
import warning from '../_util/warning';
1820
import ResponsiveObserve, {
@@ -24,7 +26,7 @@ import Row from './Row';
2426
import PropTypes from '../_util/vue-types';
2527
import { tuple } from '../_util/type';
2628
import { cloneElement } from '../_util/vnode';
27-
import { filterEmpty } from '../_util/props-util';
29+
import { flattenChildren } from '../_util/props-util';
2830
import useConfigInject from '../_util/hooks/useConfigInject';
2931

3032
export const DescriptionsItemProps = {
@@ -46,8 +48,9 @@ export type DescriptionsItemProp = Partial<ExtractPropTypes<typeof descriptionsI
4648
export const DescriptionsItem = defineComponent({
4749
name: 'ADescriptionsItem',
4850
props: descriptionsItemProp,
49-
setup() {
50-
return () => null;
51+
slots: ['label'],
52+
setup(_, { slots }) {
53+
return () => slots.default?.();
5154
},
5255
});
5356

@@ -96,7 +99,7 @@ function getFilledItem(node: VNode, span: number | undefined, rowRestCol: number
9699
}
97100

98101
function getRows(children: VNode[], column: number) {
99-
const childNodes = filterEmpty(children);
102+
const childNodes = flattenChildren(children);
100103
const rows: VNode[][] = [];
101104

102105
let tmpRow: VNode[] = [];
@@ -151,11 +154,14 @@ export interface DescriptionsContextProp {
151154
contentStyle?: Ref<CSSProperties>;
152155
}
153156

154-
export const descriptionsContext = Symbol('descriptionsContext');
157+
export const descriptionsContext: InjectionKey<DescriptionsContextProp> = Symbol(
158+
'descriptionsContext',
159+
);
155160

156161
const Descriptions = defineComponent({
157162
name: 'ADescriptions',
158163
props: descriptionsProps,
164+
slots: ['title', 'extra'],
159165
Item: DescriptionsItem,
160166
setup(props, { slots }) {
161167
const { prefixCls, direction } = useConfigInject('descriptions', props);
@@ -183,9 +189,10 @@ const Descriptions = defineComponent({
183189
contentStyle: toRef(props, 'contentStyle'),
184190
});
185191

192+
const mergeColumn = computed(() => getColumn(props.column, screens.value));
193+
186194
return () => {
187195
const {
188-
column,
189196
size,
190197
bordered = false,
191198
layout = 'horizontal',
@@ -194,9 +201,8 @@ const Descriptions = defineComponent({
194201
extra = slots.extra?.(),
195202
} = props;
196203

197-
const mergeColumn = getColumn(column, screens.value);
198204
const children = slots.default?.();
199-
const rows = getRows(children, mergeColumn);
205+
const rows = getRows(children, mergeColumn.value);
200206

201207
return (
202208
<div

components/descriptions/style/index.less

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../../style/themes/default';
1+
@import '../../style/themes/index';
22
@import '../../style/mixins/index';
33

44
@descriptions-prefix-cls: ~'@{ant-prefix}-descriptions';
@@ -109,7 +109,7 @@
109109
.@{descriptions-prefix-cls}-row {
110110
> th,
111111
> td {
112-
padding-bottom: 12px;
112+
padding-bottom: @padding-sm;
113113
}
114114
}
115115
}

0 commit comments

Comments
 (0)