antd
Version:
An enterprise-class UI design language and React components implementation
163 lines (134 loc) • 6.55 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/* eslint-disable react/no-array-index-key */
import * as React from 'react';
import classNames from 'classnames';
import toArray from "rc-util/es/Children/toArray";
import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve';
import devWarning from '../_util/devWarning';
import { ConfigContext } from '../config-provider';
import Row from './Row';
import DescriptionsItem from './Item';
import { cloneElement } from '../_util/reactNode';
var 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 (var i = 0; i < responsiveArray.length; i++) {
var breakpoint = responsiveArray[i];
if (screens[breakpoint] && column[breakpoint] !== undefined) {
return column[breakpoint] || DEFAULT_COLUMN_MAP[breakpoint];
}
}
}
return 3;
}
function getFilledItem(node, span, rowRestCol) {
var clone = node;
if (span === undefined || span > rowRestCol) {
clone = cloneElement(node, {
span: rowRestCol
});
devWarning(span === undefined, 'Descriptions', 'Sum of column `span` in a line not match `column` of Descriptions.');
}
return clone;
}
function getRows(children, column) {
var childNodes = toArray(children).filter(function (n) {
return n;
});
var rows = [];
var tmpRow = [];
var rowRestCol = column;
childNodes.forEach(function (node, index) {
var _a;
var span = (_a = node.props) === null || _a === void 0 ? void 0 : _a.span;
var mergedSpan = span || 1; // Additional handle last one
if (index === childNodes.length - 1) {
tmpRow.push(getFilledItem(node, span, rowRestCol));
rows.push(tmpRow);
return;
}
if (mergedSpan < rowRestCol) {
rowRestCol -= mergedSpan;
tmpRow.push(node);
} else {
tmpRow.push(getFilledItem(node, mergedSpan, rowRestCol));
rows.push(tmpRow);
rowRestCol = column;
tmpRow = [];
}
});
return rows;
}
function Descriptions(_ref) {
var _classNames;
var customizePrefixCls = _ref.prefixCls,
title = _ref.title,
_ref$column = _ref.column,
column = _ref$column === void 0 ? DEFAULT_COLUMN_MAP : _ref$column,
_ref$colon = _ref.colon,
colon = _ref$colon === void 0 ? true : _ref$colon,
bordered = _ref.bordered,
layout = _ref.layout,
children = _ref.children,
className = _ref.className,
style = _ref.style,
size = _ref.size;
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var prefixCls = getPrefixCls('descriptions', customizePrefixCls);
var _React$useState = React.useState({}),
_React$useState2 = _slicedToArray(_React$useState, 2),
screens = _React$useState2[0],
setScreens = _React$useState2[1];
var mergedColumn = getColumn(column, screens); // Responsive
React.useEffect(function () {
var token = ResponsiveObserve.subscribe(function (newScreens) {
if (_typeof(column) !== 'object') {
return;
}
setScreens(newScreens);
});
return function () {
ResponsiveObserve.unsubscribe(token);
};
}, []); // Children
var rows = getRows(children, mergedColumn);
return /*#__PURE__*/React.createElement("div", {
className: classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size && size !== 'default'), _defineProperty(_classNames, "".concat(prefixCls, "-bordered"), !!bordered), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames)),
style: style
}, title && /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-title")
}, title), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-view")
}, /*#__PURE__*/React.createElement("table", null, /*#__PURE__*/React.createElement("tbody", null, rows.map(function (row, index) {
return /*#__PURE__*/React.createElement(Row, {
key: index,
index: index,
colon: colon,
prefixCls: prefixCls,
vertical: layout === 'vertical',
bordered: bordered,
row: row
});
})))));
}
Descriptions.Item = DescriptionsItem;
export default Descriptions;