2019-05-22 23:22:09 +08:00
|
|
|
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';
|
2019-07-10 13:21:56 +08:00
|
|
|
import Col from './Col';
|
2019-05-22 23:22:09 +08:00
|
|
|
|
|
|
|
export interface DescriptionsItemProps {
|
|
|
|
prefixCls?: string;
|
2019-06-26 16:05:46 +08:00
|
|
|
className?: string;
|
2019-06-16 20:30:59 +08:00
|
|
|
label?: React.ReactNode;
|
|
|
|
children: React.ReactNode;
|
2019-05-22 23:22:09 +08:00
|
|
|
span?: number;
|
|
|
|
}
|
|
|
|
|
2019-06-16 20:30:59 +08:00
|
|
|
const DescriptionsItem: React.SFC<DescriptionsItemProps> = ({ children }) =>
|
|
|
|
children as JSX.Element;
|
2019-05-22 23:22:09 +08:00
|
|
|
|
|
|
|
export interface DescriptionsProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
bordered?: boolean;
|
|
|
|
size?: 'middle' | 'small' | 'default';
|
|
|
|
children?: React.ReactNode;
|
2019-07-10 09:56:46 +08:00
|
|
|
title?: 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;
|
2019-05-22 23:22:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2019-05-27 16:41:56 +08:00
|
|
|
totalRowSpan <= column,
|
2019-05-22 23:22:09 +08:00
|
|
|
'Descriptions',
|
|
|
|
'Sum of column `span` in a line exceeds `column` of Descriptions.',
|
|
|
|
);
|
2019-05-27 16:41:56 +08:00
|
|
|
|
|
|
|
childrenArray.push(columnArray);
|
|
|
|
columnArray = [];
|
|
|
|
totalRowSpan = 0;
|
2019-05-22 23:22:09 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
if (columnArray.length > 0) {
|
|
|
|
childrenArray.push(columnArray);
|
|
|
|
columnArray = [];
|
|
|
|
}
|
|
|
|
return childrenArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderRow = (
|
|
|
|
children: React.ReactElement<DescriptionsItemProps>[],
|
|
|
|
index: number,
|
|
|
|
{ prefixCls, column, isLast }: { prefixCls: string; column: number; isLast: boolean },
|
|
|
|
bordered: boolean,
|
2019-07-10 13:21:56 +08:00
|
|
|
layout: 'horizontal' | 'vertical',
|
2019-07-10 10:32:02 +08:00
|
|
|
colon: boolean,
|
2019-05-22 23:22:09 +08:00
|
|
|
) => {
|
|
|
|
// 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,
|
|
|
|
});
|
|
|
|
}
|
2019-07-10 13:21:56 +08:00
|
|
|
childrenArray.push(lastChildren);
|
|
|
|
|
|
|
|
const renderCol = (
|
|
|
|
childrenItem: React.ReactElement<DescriptionsItemProps>,
|
|
|
|
type: 'label' | 'content',
|
|
|
|
idx: number,
|
|
|
|
) => (
|
|
|
|
<Col
|
|
|
|
child={childrenItem}
|
|
|
|
bordered={bordered}
|
|
|
|
colon={colon}
|
|
|
|
type={type}
|
|
|
|
key={`${type}-${idx}`}
|
|
|
|
layout={layout}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const cloneChildren: JSX.Element[] = [];
|
|
|
|
const cloneContentChildren: JSX.Element[] = [];
|
|
|
|
React.Children.forEach(
|
|
|
|
childrenArray,
|
|
|
|
(childrenItem: React.ReactElement<DescriptionsItemProps>, idx: number) => {
|
|
|
|
cloneChildren.push(renderCol(childrenItem, 'label', idx));
|
|
|
|
if (layout === 'vertical') {
|
|
|
|
cloneContentChildren.push(renderCol(childrenItem, 'content', idx));
|
|
|
|
} else if (bordered) {
|
|
|
|
cloneChildren.push(renderCol(childrenItem, 'content', idx));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-06-27 11:29:33 +08:00
|
|
|
|
|
|
|
if (layout === 'vertical') {
|
|
|
|
return [
|
|
|
|
<tr className={`${prefixCls}-row`} key={`label-${index}`}>
|
2019-07-10 13:21:56 +08:00
|
|
|
{cloneChildren}
|
2019-06-27 11:29:33 +08:00
|
|
|
</tr>,
|
|
|
|
<tr className={`${prefixCls}-row`} key={`content-${index}`}>
|
|
|
|
{cloneContentChildren}
|
|
|
|
</tr>,
|
|
|
|
];
|
|
|
|
}
|
2019-07-10 13:21:56 +08:00
|
|
|
|
2019-05-22 23:22:09 +08:00
|
|
|
return (
|
|
|
|
<tr className={`${prefixCls}-row`} key={index}>
|
|
|
|
{cloneChildren}
|
|
|
|
</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,
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-06-16 17:15:52 +08:00
|
|
|
static Item: typeof DescriptionsItem = DescriptionsItem;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-05-22 23:22:09 +08:00
|
|
|
state: {
|
|
|
|
screens: BreakpointMap;
|
|
|
|
} = {
|
|
|
|
screens: {},
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-05-22 23:22:09 +08:00
|
|
|
token: string;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-05-22 23:22:09 +08:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
// If the configuration is not an object, it is a number, return number
|
2019-05-22 23:22:09 +08:00
|
|
|
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,
|
2019-06-27 11:29:33 +08:00
|
|
|
layout = 'horizontal',
|
2019-07-10 10:32:02 +08:00
|
|
|
colon = true,
|
2019-07-10 10:01:31 +08:00
|
|
|
style,
|
2019-05-22 23:22:09 +08:00
|
|
|
} = this.props;
|
|
|
|
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
|
|
|
|
|
|
|
|
const column = this.getColumn();
|
|
|
|
const cloneChildren = React.Children.map(
|
|
|
|
children,
|
|
|
|
(child: React.ReactElement<DescriptionsItemProps>) => {
|
2019-06-21 13:49:01 +08:00
|
|
|
if (React.isValidElement(child)) {
|
|
|
|
return React.cloneElement(child, {
|
|
|
|
prefixCls,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return child;
|
2019-05-22 23:22:09 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const childrenArray: Array<
|
|
|
|
React.ReactElement<DescriptionsItemProps>[]
|
|
|
|
> = generateChildrenRows(cloneChildren, column);
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(prefixCls, className, {
|
2019-06-26 16:05:46 +08:00
|
|
|
[`${prefixCls}-${size}`]: size !== 'default',
|
|
|
|
[`${prefixCls}-bordered`]: !!bordered,
|
2019-05-22 23:22:09 +08:00
|
|
|
})}
|
2019-07-10 10:01:31 +08:00
|
|
|
style={style}
|
2019-05-22 23:22:09 +08:00
|
|
|
>
|
|
|
|
{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,
|
2019-06-27 11:29:33 +08:00
|
|
|
layout,
|
2019-07-10 10:32:02 +08:00
|
|
|
colon,
|
2019-05-22 23:22:09 +08:00
|
|
|
),
|
|
|
|
)}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Descriptions;
|