2020-02-23 20:48:16 +08:00
|
|
|
/* eslint-disable react/no-array-index-key */
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-08-30 22:09:32 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import type { Breakpoint } from '../_util/responsiveObserver';
|
|
|
|
import { matchScreen } from '../_util/responsiveObserver';
|
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-08-30 22:09:32 +08:00
|
|
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
|
|
|
import DEFAULT_COLUMN_MAP from './constant';
|
2023-06-25 12:10:31 +08:00
|
|
|
import DescriptionsContext from './DescriptionsContext';
|
2023-08-30 22:09:32 +08:00
|
|
|
import useItems from './hooks/useItems';
|
|
|
|
import useRow from './hooks/useRow';
|
2023-07-17 14:48:03 +08:00
|
|
|
import type { DescriptionsItemProps } from './Item';
|
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
|
|
|
|
2023-07-09 18:26:06 +08:00
|
|
|
interface CompoundedComponent {
|
|
|
|
Item: typeof DescriptionsItem;
|
|
|
|
}
|
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
export interface InternalDescriptionsItemType extends DescriptionsItemProps {
|
2023-07-17 14:48:03 +08:00
|
|
|
key?: React.Key;
|
|
|
|
}
|
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
export interface DescriptionsItemType extends Omit<InternalDescriptionsItemType, 'span'> {
|
|
|
|
span?: number | { [key in Breakpoint]?: number };
|
|
|
|
}
|
|
|
|
|
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';
|
2023-07-17 14:48:03 +08:00
|
|
|
/**
|
|
|
|
* @deprecated use `items` instead
|
|
|
|
*/
|
2019-05-22 23:22:09 +08:00
|
|
|
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;
|
2023-07-17 14:48:03 +08:00
|
|
|
items?: DescriptionsItemType[];
|
2019-05-22 23:22:09 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 18:26:06 +08:00
|
|
|
const Descriptions: React.FC<DescriptionsProps> & CompoundedComponent = (props) => {
|
2023-07-09 13:34:59 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
title,
|
|
|
|
extra,
|
2023-08-30 22:09:32 +08:00
|
|
|
column,
|
2023-07-09 13:34:59 +08:00
|
|
|
colon = true,
|
|
|
|
bordered,
|
|
|
|
layout,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
style,
|
|
|
|
size: customizeSize,
|
|
|
|
labelStyle,
|
|
|
|
contentStyle,
|
2023-07-17 14:48:03 +08:00
|
|
|
items,
|
2023-07-09 13:34:59 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
2023-06-26 14:38:02 +08:00
|
|
|
const { getPrefixCls, direction, descriptions } = React.useContext(ConfigContext);
|
2020-02-23 20:48:16 +08:00
|
|
|
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
|
2023-08-30 22:09:32 +08:00
|
|
|
const screens = useBreakpoint();
|
2020-02-23 20:48:16 +08:00
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
// Column count
|
|
|
|
const mergedColumn = React.useMemo(() => {
|
|
|
|
if (typeof column === 'number') {
|
|
|
|
return column;
|
|
|
|
}
|
2023-05-10 14:09:25 +08:00
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
return (
|
|
|
|
matchScreen(screens, {
|
|
|
|
...DEFAULT_COLUMN_MAP,
|
|
|
|
...column,
|
|
|
|
}) ?? 3
|
|
|
|
);
|
|
|
|
}, [screens, column]);
|
|
|
|
|
|
|
|
// Items with responsive
|
|
|
|
const mergedItems = useItems(screens, items, children);
|
2022-03-31 17:11:44 +08:00
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
const mergedSize = useSize(customizeSize);
|
|
|
|
const rows = useRow(mergedColumn, mergedItems);
|
2019-05-22 23:22:09 +08:00
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2019-05-22 23:22:09 +08:00
|
|
|
|
2023-07-17 14:48:03 +08:00
|
|
|
// ======================== Render ========================
|
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-06-26 14:38:02 +08:00
|
|
|
descriptions?.className,
|
2020-12-30 17:08:43 +08:00
|
|
|
{
|
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
|
|
|
)}
|
2023-06-26 14:38:02 +08:00
|
|
|
style={{ ...descriptions?.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
|
|
|
);
|
2023-07-09 13:34:59 +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;
|