antd
Version:
An enterprise-class UI design language and React components implementation
162 lines (161 loc) • 5.66 kB
JavaScript
'use client';
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
/* eslint-disable react/no-array-index-key */
import classNames from 'classnames';
import toArray from "rc-util/es/Children/toArray";
import * as React from 'react';
import { cloneElement } from '../_util/reactNode';
import useResponsiveObserver, { responsiveArray } from '../_util/responsiveObserver';
import warning from '../_util/warning';
import { ConfigContext } from '../config-provider';
import useSize from '../config-provider/hooks/useSize';
import DescriptionsContext from './DescriptionsContext';
import DescriptionsItem from './Item';
import Row from './Row';
import useStyle from './style';
const DEFAULT_COLUMN_MAP = {
xxl: 3,
xl: 3,
lg: 3,
md: 3,
sm: 2,
xs: 1
};
function getColumn(column, screens) {
if (typeof column === 'number') {
return column;
}
if (typeof column === 'object') {
for (let i = 0; i < responsiveArray.length; i++) {
const breakpoint = responsiveArray[i];
if (screens[breakpoint] && column[breakpoint] !== undefined) {
return column[breakpoint] || DEFAULT_COLUMN_MAP[breakpoint];
}
}
}
return 3;
}
function getFilledItem(node, rowRestCol, span) {
let clone = node;
if (span === undefined || span > rowRestCol) {
clone = cloneElement(node, {
span: rowRestCol
});
process.env.NODE_ENV !== "production" ? warning(span === undefined, 'Descriptions', 'Sum of column `span` in a line not match `column` of Descriptions.') : void 0;
}
return clone;
}
function getRows(children, column) {
const childNodes = toArray(children).filter(n => n);
const rows = [];
let tmpRow = [];
let rowRestCol = column;
childNodes.forEach((node, index) => {
var _a;
const span = (_a = node.props) === null || _a === void 0 ? void 0 : _a.span;
const mergedSpan = span || 1;
// Additional handle last one
if (index === childNodes.length - 1) {
tmpRow.push(getFilledItem(node, rowRestCol, span));
rows.push(tmpRow);
return;
}
if (mergedSpan < rowRestCol) {
rowRestCol -= mergedSpan;
tmpRow.push(node);
} else {
tmpRow.push(getFilledItem(node, rowRestCol, mergedSpan));
rows.push(tmpRow);
rowRestCol = column;
tmpRow = [];
}
});
return rows;
}
const Descriptions = props => {
const {
prefixCls: customizePrefixCls,
title,
extra,
column = DEFAULT_COLUMN_MAP,
colon = true,
bordered,
layout,
children,
className,
rootClassName,
style,
size: customizeSize,
labelStyle,
contentStyle
} = props,
restProps = __rest(props, ["prefixCls", "title", "extra", "column", "colon", "bordered", "layout", "children", "className", "rootClassName", "style", "size", "labelStyle", "contentStyle"]);
const {
getPrefixCls,
direction,
descriptions
} = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
const [screens, setScreens] = React.useState({});
const mergedColumn = getColumn(column, screens);
const mergedSize = useSize(customizeSize);
const [wrapSSR, hashId] = useStyle(prefixCls);
const responsiveObserver = useResponsiveObserver();
// Responsive
React.useEffect(() => {
const token = responsiveObserver.subscribe(newScreens => {
if (typeof column !== 'object') {
return;
}
setScreens(newScreens);
});
return () => {
responsiveObserver.unsubscribe(token);
};
}, []);
// Children
const rows = getRows(children, mergedColumn);
const contextValue = React.useMemo(() => ({
labelStyle,
contentStyle
}), [labelStyle, contentStyle]);
return wrapSSR( /*#__PURE__*/React.createElement(DescriptionsContext.Provider, {
value: contextValue
}, /*#__PURE__*/React.createElement("div", Object.assign({
className: classNames(prefixCls, descriptions === null || descriptions === void 0 ? void 0 : descriptions.className, {
[`${prefixCls}-${mergedSize}`]: mergedSize && mergedSize !== 'default',
[`${prefixCls}-bordered`]: !!bordered,
[`${prefixCls}-rtl`]: direction === 'rtl'
}, className, rootClassName, hashId),
style: Object.assign(Object.assign({}, descriptions === null || descriptions === void 0 ? void 0 : descriptions.style), style)
}, restProps), (title || extra) && /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-header`
}, title && /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-title`
}, title), extra && /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-extra`
}, extra)), /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-view`
}, /*#__PURE__*/React.createElement("table", null, /*#__PURE__*/React.createElement("tbody", null, rows.map((row, index) => /*#__PURE__*/React.createElement(Row, {
key: index,
index: index,
colon: colon,
prefixCls: prefixCls,
vertical: layout === 'vertical',
bordered: bordered,
row: row
}))))))));
};
if (process.env.NODE_ENV !== 'production') {
Descriptions.displayName = 'Descriptions';
}
export { DescriptionsContext };
Descriptions.Item = DescriptionsItem;
export default Descriptions;