UNPKG

linkmore-design

Version:

🌈 πŸš€lmη»„δ»ΆεΊ“γ€‚πŸš€

223 lines (210 loc) β€’ 7.47 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertChildrenToColumns = convertChildrenToColumns; exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _warning = _interopRequireDefault(require("rc-util/lib/warning")); var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray")); var _legacyUtil = require("../utils/legacyUtil"); var _constant = require("../constant"); function convertChildrenToColumns(children) { return (0, _toArray.default)(children).filter(node => /*#__PURE__*/React.isValidElement(node)).map(({ key, props }) => { const { children: nodeChildren, ...restProps } = props; const column = { key, ...restProps }; if (nodeChildren) { column.children = convertChildrenToColumns(nodeChildren); } return column; }); } function flatColumns(columns) { return columns.reduce((list, column) => { const { fixed } = column; // Convert `fixed='true'` to `fixed='left'` instead const parsedFixed = fixed === true ? 'left' : fixed; const subColumns = column.children; if (subColumns && subColumns.length > 0) { return [...list, ...flatColumns(subColumns).map(subColum => ({ fixed: parsedFixed, ...subColum }))]; } return [...list, { ...column, fixed: parsedFixed }]; }, []); } function warningFixed(flattenColumns) { let allFixLeft = true; for (let i = 0; i < flattenColumns.length; i += 1) { const col = flattenColumns[i]; if (allFixLeft && col.fixed !== 'left') { allFixLeft = false; } else if (!allFixLeft && col.fixed === 'left') { (0, _warning.default)(false, `Index ${i - 1} of \`columns\` missing \`fixed='left'\` prop.`); break; } } let allFixRight = true; for (let i = flattenColumns.length - 1; i >= 0; i -= 1) { const col = flattenColumns[i]; if (allFixRight && col.fixed !== 'right') { allFixRight = false; } else if (!allFixRight && col.fixed === 'right') { (0, _warning.default)(false, `Index ${i + 1} of \`columns\` missing \`fixed='right'\` prop.`); break; } } } function revertForRtl(columns) { return columns.map(column => { const { fixed, ...restProps } = column; // Convert `fixed='left'` to `fixed='right'` instead let parsedFixed = fixed; if (fixed === 'left') { parsedFixed = 'right'; } else if (fixed === 'right') { parsedFixed = 'left'; } return { fixed: parsedFixed, ...restProps }; }); } /** * Parse `columns` & `children` into `columns`. */ function useColumns({ prefixCls, columns, children, expandable, expandedKeys, columnTitle, getRowKey, onTriggerExpand, expandIcon, rowExpandable, expandIconColumnIndex, direction, expandRowByClick, columnWidth, fixed }, transformColumns) { const baseColumns = React.useMemo(() => columns || convertChildrenToColumns(children), [columns, children]); // ========================== Expand ========================== const withExpandColumns = React.useMemo(() => { if (expandable) { let cloneColumns = baseColumns.slice(); // >>> Warning if use `expandIconColumnIndex` if (process.env.NODE_ENV !== 'production' && expandIconColumnIndex >= 0) { (0, _warning.default)(false, '`expandIconColumnIndex` is deprecated. Please use `Table.EXPAND_COLUMN` in `columns` instead.'); } // >>> Insert expand column if not exist if (!cloneColumns.includes(_constant.EXPAND_COLUMN)) { const expandColIndex = expandIconColumnIndex || 0; if (expandColIndex >= 0) { cloneColumns.splice(expandColIndex, 0, _constant.EXPAND_COLUMN); } } // >>> Deduplicate additional expand column if (process.env.NODE_ENV !== 'production' && cloneColumns.filter(c => c === _constant.EXPAND_COLUMN).length > 1) { (0, _warning.default)(false, 'There exist more than one `EXPAND_COLUMN` in `columns`.'); } const expandColumnIndex = cloneColumns.indexOf(_constant.EXPAND_COLUMN); cloneColumns = cloneColumns.filter((column, index) => column !== _constant.EXPAND_COLUMN || index === expandColumnIndex); // >>> Check if expand column need to fixed const prevColumn = baseColumns[expandColumnIndex]; let fixedColumn; if ((fixed === 'left' || fixed) && !expandIconColumnIndex) { fixedColumn = 'left'; } else if ((fixed === 'right' || fixed) && expandIconColumnIndex === baseColumns.length) { fixedColumn = 'right'; } else { fixedColumn = prevColumn ? prevColumn.fixed : null; } // >>> Create expandable column const expandColumn = { [_legacyUtil.INTERNAL_COL_DEFINE]: { className: `${prefixCls}-expand-icon-col`, columnType: 'EXPAND_COLUMN' }, title: columnTitle, fixed: fixedColumn, className: `${prefixCls}-row-expand-icon-cell`, width: columnWidth, render: (_, record, index) => { const rowKey = getRowKey(record, index); const expanded = expandedKeys.has(rowKey); const recordExpandable = rowExpandable ? rowExpandable(record) : true; const icon = expandIcon({ prefixCls, expanded, expandable: recordExpandable, record, onExpand: onTriggerExpand }); if (expandRowByClick) { return /*#__PURE__*/React.createElement("span", { onClick: e => e.stopPropagation() }, icon); } return icon; } }; return cloneColumns.map(col => col === _constant.EXPAND_COLUMN ? expandColumn : col); } if (process.env.NODE_ENV !== 'production' && baseColumns.includes(_constant.EXPAND_COLUMN)) { (0, _warning.default)(false, '`expandable` is not config but there exist `EXPAND_COLUMN` in `columns`.'); } return baseColumns.filter(col => col !== _constant.EXPAND_COLUMN); }, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon, direction]); // ========================= Transform ======================== const mergedColumns = React.useMemo(() => { let finalColumns = withExpandColumns; if (transformColumns) { finalColumns = transformColumns(finalColumns); } // Always provides at least one column for table display if (!finalColumns.length) { finalColumns = [{ render: () => null }]; } return finalColumns; }, [transformColumns, withExpandColumns, direction]); // ========================== Flatten ========================= const flattenColumns = React.useMemo(() => { if (direction === 'rtl') { return revertForRtl(flatColumns(mergedColumns)); } return flatColumns(mergedColumns); }, [mergedColumns, direction]); // Only check out of production since it's waste for each render if (process.env.NODE_ENV !== 'production') { warningFixed(direction === 'rtl' ? flattenColumns.slice().reverse() : flattenColumns); } return [mergedColumns, flattenColumns]; } var _default = useColumns; exports.default = _default;