2020-02-23 20:48:16 +08:00
|
|
|
/* eslint-disable react/no-array-index-key */
|
2019-05-22 23:22:09 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-30 12:31:10 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-12-22 10:11:08 +08:00
|
|
|
import type { Breakpoint, ScreenMap } from '../_util/responsiveObserver';
|
|
|
|
import useResponsiveObserver, { responsiveArray } from '../_util/responsiveObserver';
|
2022-05-10 15:43:29 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-05-11 15:24:26 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2023-06-25 12:10:31 +08:00
|
|
|
import DescriptionsContext from './DescriptionsContext';
|
2020-02-23 20:48:16 +08:00
|
|
|
import DescriptionsItem from './Item';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Row from './Row';
|
2022-03-31 17:11:44 +08:00
|
|
|
import useStyle from './style';
|
2019-05-22 23:22:09 +08:00
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
const DEFAULT_COLUMN_MAP: Record<Breakpoint, number> = {
|
|
|
|
xxl: 3,
|
|
|
|
xl: 3,
|
|
|
|
lg: 3,
|
|
|
|
md: 3,
|
|
|
|
sm: 2,
|
|
|
|
xs: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
function getColumn(column: DescriptionsProps['column'], screens: ScreenMap): number {
|
|
|
|
if (typeof column === 'number') {
|
|
|
|
return column;
|
2019-11-30 18:10:56 +08:00
|
|
|
}
|
2020-02-23 20:48:16 +08:00
|
|
|
|
|
|
|
if (typeof column === 'object') {
|
|
|
|
for (let i = 0; i < responsiveArray.length; i++) {
|
|
|
|
const breakpoint: Breakpoint = responsiveArray[i];
|
|
|
|
if (screens[breakpoint] && column[breakpoint] !== undefined) {
|
|
|
|
return column[breakpoint] || DEFAULT_COLUMN_MAP[breakpoint];
|
|
|
|
}
|
2019-11-30 18:10:56 +08:00
|
|
|
}
|
2020-02-23 20:48:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 3;
|
2019-11-30 18:10:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
function getFilledItem(
|
|
|
|
node: React.ReactElement,
|
|
|
|
rowRestCol: number,
|
2023-01-16 16:45:14 +08:00
|
|
|
span?: number,
|
2020-02-23 20:48:16 +08:00
|
|
|
): React.ReactElement {
|
|
|
|
let clone = node;
|
|
|
|
|
|
|
|
if (span === undefined || span > rowRestCol) {
|
2020-05-14 20:54:49 +08:00
|
|
|
clone = cloneElement(node, {
|
2020-02-23 20:48:16 +08:00
|
|
|
span: rowRestCol,
|
|
|
|
});
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2020-02-23 20:48:16 +08:00
|
|
|
span === undefined,
|
|
|
|
'Descriptions',
|
|
|
|
'Sum of column `span` in a line not match `column` of Descriptions.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
2019-05-22 23:22:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
function getRows(children: React.ReactNode, column: number) {
|
2022-11-19 13:47:33 +08:00
|
|
|
const childNodes = toArray(children).filter((n) => n);
|
2020-02-23 20:48:16 +08:00
|
|
|
const rows: React.ReactElement[][] = [];
|
|
|
|
|
|
|
|
let tmpRow: React.ReactElement[] = [];
|
|
|
|
let rowRestCol = column;
|
|
|
|
|
|
|
|
childNodes.forEach((node, index) => {
|
2023-01-16 09:55:52 +08:00
|
|
|
const span: number = node.props?.span;
|
2020-02-23 20:48:16 +08:00
|
|
|
const mergedSpan = span || 1;
|
|
|
|
|
|
|
|
// Additional handle last one
|
|
|
|
if (index === childNodes.length - 1) {
|
2023-01-16 16:45:14 +08:00
|
|
|
tmpRow.push(getFilledItem(node, rowRestCol, span));
|
2020-02-23 20:48:16 +08:00
|
|
|
rows.push(tmpRow);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mergedSpan < rowRestCol) {
|
|
|
|
rowRestCol -= mergedSpan;
|
|
|
|
tmpRow.push(node);
|
|
|
|
} else {
|
2023-01-16 16:45:14 +08:00
|
|
|
tmpRow.push(getFilledItem(node, rowRestCol, mergedSpan));
|
2020-02-23 20:48:16 +08:00
|
|
|
rows.push(tmpRow);
|
|
|
|
rowRestCol = column;
|
|
|
|
tmpRow = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
2019-05-22 23:22:09 +08:00
|
|
|
|
|
|
|
export interface DescriptionsProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2019-05-22 23:22:09 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
bordered?: boolean;
|
|
|
|
size?: 'middle' | 'small' | 'default';
|
|
|
|
children?: React.ReactNode;
|
2019-07-10 09:56:46 +08:00
|
|
|
title?: React.ReactNode;
|
2020-07-14 20:37:21 +08:00
|
|
|
extra?: React.ReactNode;
|
2019-05-22 23:22:09 +08:00
|
|
|
column?: number | Partial<Record<Breakpoint, number>>;
|
2019-06-27 11:29:33 +08:00
|
|
|
layout?: 'horizontal' | 'vertical';
|
2019-07-10 10:32:02 +08:00
|
|
|
colon?: boolean;
|
2020-12-30 17:08:43 +08:00
|
|
|
labelStyle?: React.CSSProperties;
|
|
|
|
contentStyle?: React.CSSProperties;
|
2019-05-22 23:22:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
function Descriptions({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
title,
|
2020-07-14 20:37:21 +08:00
|
|
|
extra,
|
2020-02-23 20:48:16 +08:00
|
|
|
column = DEFAULT_COLUMN_MAP,
|
|
|
|
colon = true,
|
|
|
|
bordered,
|
|
|
|
layout,
|
|
|
|
children,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-02-23 20:48:16 +08:00
|
|
|
style,
|
2023-05-10 14:09:25 +08:00
|
|
|
size: customizeSize,
|
2020-12-30 17:08:43 +08:00
|
|
|
labelStyle,
|
|
|
|
contentStyle,
|
2023-02-22 16:17:21 +08:00
|
|
|
...restProps
|
2020-02-23 20:48:16 +08:00
|
|
|
}: DescriptionsProps) {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
|
|
|
|
const [screens, setScreens] = React.useState<ScreenMap>({});
|
|
|
|
const mergedColumn = getColumn(column, screens);
|
|
|
|
|
2023-05-11 15:24:26 +08:00
|
|
|
const mergedSize = useSize(customizeSize);
|
2023-05-10 14:09:25 +08:00
|
|
|
|
2022-03-31 17:11:44 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-12-22 10:11:08 +08:00
|
|
|
const responsiveObserver = useResponsiveObserver();
|
2022-03-31 17:11:44 +08:00
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
// Responsive
|
|
|
|
React.useEffect(() => {
|
2022-12-22 10:11:08 +08:00
|
|
|
const token = responsiveObserver.subscribe((newScreens) => {
|
2019-05-22 23:22:09 +08:00
|
|
|
if (typeof column !== 'object') {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-23 20:48:16 +08:00
|
|
|
setScreens(newScreens);
|
2019-05-22 23:22:09 +08:00
|
|
|
});
|
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
return () => {
|
2022-12-22 10:11:08 +08:00
|
|
|
responsiveObserver.unsubscribe(token);
|
2020-02-23 20:48:16 +08:00
|
|
|
};
|
|
|
|
}, []);
|
2019-05-22 23:22:09 +08:00
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
// Children
|
|
|
|
const rows = getRows(children, mergedColumn);
|
2021-11-26 15:19:31 +08:00
|
|
|
const contextValue = React.useMemo(
|
|
|
|
() => ({ labelStyle, contentStyle }),
|
|
|
|
[labelStyle, contentStyle],
|
|
|
|
);
|
2019-05-22 23:22:09 +08:00
|
|
|
|
2022-03-31 17:11:44 +08:00
|
|
|
return wrapSSR(
|
2021-11-26 12:18:21 +08:00
|
|
|
<DescriptionsContext.Provider value={contextValue}>
|
2020-12-30 17:08:43 +08:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
2023-05-10 14:09:25 +08:00
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize && mergedSize !== 'default',
|
2020-12-30 17:08:43 +08:00
|
|
|
[`${prefixCls}-bordered`]: !!bordered,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-31 17:11:44 +08:00
|
|
|
hashId,
|
2020-12-30 17:08:43 +08:00
|
|
|
)}
|
|
|
|
style={style}
|
2023-02-22 16:17:21 +08:00
|
|
|
{...restProps}
|
2020-12-30 17:08:43 +08:00
|
|
|
>
|
|
|
|
{(title || extra) && (
|
|
|
|
<div className={`${prefixCls}-header`}>
|
|
|
|
{title && <div className={`${prefixCls}-title`}>{title}</div>}
|
|
|
|
{extra && <div className={`${prefixCls}-extra`}>{extra}</div>}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className={`${prefixCls}-view`}>
|
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
{rows.map((row, index) => (
|
|
|
|
<Row
|
|
|
|
key={index}
|
|
|
|
index={index}
|
|
|
|
colon={colon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
vertical={layout === 'vertical'}
|
|
|
|
bordered={bordered}
|
|
|
|
row={row}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2020-07-14 20:37:21 +08:00
|
|
|
</div>
|
2020-02-23 20:48:16 +08:00
|
|
|
</div>
|
2022-03-31 17:11:44 +08:00
|
|
|
</DescriptionsContext.Provider>,
|
2020-02-23 20:48:16 +08:00
|
|
|
);
|
2019-05-22 23:22:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Descriptions.displayName = 'Descriptions';
|
|
|
|
}
|
|
|
|
|
2023-06-25 12:10:31 +08:00
|
|
|
export type { DescriptionsContextProps } from './DescriptionsContext';
|
|
|
|
export { DescriptionsContext };
|
|
|
|
|
2020-02-23 20:48:16 +08:00
|
|
|
Descriptions.Item = DescriptionsItem;
|
|
|
|
|
2019-05-22 23:22:09 +08:00
|
|
|
export default Descriptions;
|