/
index.tsx
273 lines (258 loc) · 7.33 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import * as React from 'react';
import classNames from 'classnames';
import warning from '../_util/warning';
import ResponsiveObserve, {
Breakpoint,
BreakpointMap,
responsiveArray,
} from '../_util/responsiveObserve';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
export interface DescriptionsItemProps {
prefixCls?: string;
className?: string;
label?: React.ReactNode;
children: React.ReactNode;
span?: number;
}
const DescriptionsItem: React.SFC<DescriptionsItemProps> = ({ children }) =>
children as JSX.Element;
export interface DescriptionsProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
bordered?: boolean;
size?: 'middle' | 'small' | 'default';
children?: React.ReactNode;
title?: string;
column?: number | Partial<Record<Breakpoint, number>>;
}
/**
* Convert children into `column` groups.
* @param cloneChildren: DescriptionsItem
* @param column: number
*/
const generateChildrenRows = (
cloneChildren: React.ReactNode,
column: number,
): React.ReactElement<DescriptionsItemProps>[][] => {
const childrenArray: React.ReactElement<DescriptionsItemProps>[][] = [];
let columnArray: React.ReactElement<DescriptionsItemProps>[] = [];
let totalRowSpan = 0;
React.Children.forEach(cloneChildren, (node: React.ReactElement<DescriptionsItemProps>) => {
columnArray.push(node);
if (node.props.span) {
totalRowSpan += node.props.span;
} else {
totalRowSpan += 1;
}
if (totalRowSpan >= column) {
warning(
totalRowSpan <= column,
'Descriptions',
'Sum of column `span` in a line exceeds `column` of Descriptions.',
);
childrenArray.push(columnArray);
columnArray = [];
totalRowSpan = 0;
}
});
if (columnArray.length > 0) {
childrenArray.push(columnArray);
columnArray = [];
}
return childrenArray;
};
/**
* This code is for handling react15 does not support returning an array,
* It can convert a children into th and td
* @param child DescriptionsItem
* @returns
* <>
* <th>{DescriptionsItem.label}</th>
* <td>{DescriptionsItem.children}</td>
* </>
*/
const renderCol = (child: React.ReactElement<DescriptionsItemProps>, bordered: boolean) => {
const { prefixCls, label, className, children, span = 1 } = child.props;
if (bordered) {
return [
<th
className={classNames(`${prefixCls}-item-label`, className, {
[`${prefixCls}-item-no-label`]: !label,
})}
key="label"
>
{label}
</th>,
<td
className={classNames(`${prefixCls}-item-content`, className)}
key="content"
colSpan={span * 2 - 1}
>
{children}
</td>,
];
}
return (
<td colSpan={span} className={classNames(`${prefixCls}-item`, className)}>
<span
className={classNames(`${prefixCls}-item-label`, {
[`${prefixCls}-item-no-label`]: !label,
})}
key="label"
>
{label}
</span>
<span className={`${prefixCls}-item-content`} key="content">
{children}
</span>
</td>
);
};
const renderRow = (
children: React.ReactElement<DescriptionsItemProps>[],
index: number,
{ prefixCls, column, isLast }: { prefixCls: string; column: number; isLast: boolean },
bordered: boolean,
) => {
// copy children,prevent changes to incoming parameters
const childrenArray = [...children];
let lastChildren = childrenArray.pop() as React.ReactElement<DescriptionsItemProps>;
const span = column - childrenArray.length;
if (isLast) {
lastChildren = React.cloneElement(lastChildren as React.ReactElement<DescriptionsItemProps>, {
span,
});
}
const cloneChildren = React.Children.map(
childrenArray,
(childrenItem: React.ReactElement<DescriptionsItemProps>) => {
return renderCol(childrenItem, bordered);
},
);
return (
<tr className={`${prefixCls}-row`} key={index}>
{cloneChildren}
{renderCol(lastChildren, bordered)}
</tr>
);
};
const defaultColumnMap = {
xxl: 3,
xl: 3,
lg: 3,
md: 3,
sm: 2,
xs: 1,
};
class Descriptions extends React.Component<
DescriptionsProps,
{
screens: BreakpointMap;
}
> {
static defaultProps: DescriptionsProps = {
size: 'default',
column: defaultColumnMap,
};
static Item: typeof DescriptionsItem = DescriptionsItem;
state: {
screens: BreakpointMap;
} = {
screens: {},
};
token: string;
componentDidMount() {
const { column } = this.props;
this.token = ResponsiveObserve.subscribe(screens => {
if (typeof column !== 'object') {
return;
}
this.setState({
screens,
});
});
}
componentWillUnmount() {
ResponsiveObserve.unsubscribe(this.token);
}
getColumn(): number {
const { column } = this.props;
if (typeof column === 'object') {
for (let i = 0; i < responsiveArray.length; i++) {
const breakpoint: Breakpoint = responsiveArray[i];
if (this.state.screens[breakpoint] && column[breakpoint] !== undefined) {
return column[breakpoint] || defaultColumnMap[breakpoint];
}
}
}
//If the configuration is not an object, it is a number, return number
if (typeof column === 'number') {
return column as number;
}
// If it is an object, but no response is found, this happens only in the test.
// Maybe there are some strange environments
return 3;
}
render() {
return (
<ConfigConsumer>
{({ getPrefixCls }: ConfigConsumerProps) => {
const {
className,
prefixCls: customizePrefixCls,
title,
size,
children,
bordered = false,
} = this.props;
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
const column = this.getColumn();
const cloneChildren = React.Children.map(
children,
(child: React.ReactElement<DescriptionsItemProps>) => {
if (React.isValidElement(child)) {
return React.cloneElement(child, {
prefixCls,
});
}
return child;
},
);
const childrenArray: Array<
React.ReactElement<DescriptionsItemProps>[]
> = generateChildrenRows(cloneChildren, column);
return (
<div
className={classNames(prefixCls, className, {
[`${prefixCls}-${size}`]: size !== 'default',
[`${prefixCls}-bordered`]: !!bordered,
})}
>
{title && <div className={`${prefixCls}-title`}>{title}</div>}
<div className={`${prefixCls}-view`}>
<table>
<tbody>
{childrenArray.map((child, index) =>
renderRow(
child,
index,
{
prefixCls,
column,
isLast: index + 1 === childrenArray.length,
},
bordered,
),
)}
</tbody>
</table>
</div>
</div>
);
}}
</ConfigConsumer>
);
}
}
export default Descriptions;