UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = useRowInfo;
8var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9var _context = require("@rc-component/context");
10var _TableContext = _interopRequireDefault(require("../context/TableContext"));
11var _valueUtil = require("../utils/valueUtil");
12var _rcUtil = require("rc-util");
13var _classnames = _interopRequireDefault(require("classnames"));
14function useRowInfo(record, rowKey, recordIndex, indent) {
15 var context = (0, _context.useContext)(_TableContext.default, ['prefixCls', 'fixedInfoList', 'flattenColumns', 'expandableType', 'expandRowByClick', 'onTriggerExpand', 'rowClassName', 'expandedRowClassName', 'indentSize', 'expandIcon', 'expandedRowRender', 'expandIconColumnIndex', 'expandedKeys', 'childrenColumnName', 'rowExpandable', 'onRow']);
16 var flattenColumns = context.flattenColumns,
17 expandableType = context.expandableType,
18 expandedKeys = context.expandedKeys,
19 childrenColumnName = context.childrenColumnName,
20 onTriggerExpand = context.onTriggerExpand,
21 rowExpandable = context.rowExpandable,
22 onRow = context.onRow,
23 expandRowByClick = context.expandRowByClick,
24 rowClassName = context.rowClassName;
25
26 // ======================= Expandable =======================
27 // Only when row is not expandable and `children` exist in record
28 var nestExpandable = expandableType === 'nest';
29 var rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
30 var mergedExpandable = rowSupportExpand || nestExpandable;
31 var expanded = expandedKeys && expandedKeys.has(rowKey);
32 var hasNestChildren = childrenColumnName && record && record[childrenColumnName];
33 var onInternalTriggerExpand = (0, _rcUtil.useEvent)(onTriggerExpand);
34
35 // ========================= onRow ==========================
36 var rowProps = onRow === null || onRow === void 0 ? void 0 : onRow(record, recordIndex);
37 var onRowClick = rowProps === null || rowProps === void 0 ? void 0 : rowProps.onClick;
38 var onClick = function onClick(event) {
39 if (expandRowByClick && mergedExpandable) {
40 onTriggerExpand(record, event);
41 }
42 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
43 args[_key - 1] = arguments[_key];
44 }
45 onRowClick === null || onRowClick === void 0 || onRowClick.apply(void 0, [event].concat(args));
46 };
47
48 // ====================== RowClassName ======================
49 var computeRowClassName;
50 if (typeof rowClassName === 'string') {
51 computeRowClassName = rowClassName;
52 } else if (typeof rowClassName === 'function') {
53 computeRowClassName = rowClassName(record, recordIndex, indent);
54 }
55
56 // ========================= Column =========================
57 var columnsKey = (0, _valueUtil.getColumnsKey)(flattenColumns);
58 return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, context), {}, {
59 columnsKey: columnsKey,
60 nestExpandable: nestExpandable,
61 expanded: expanded,
62 hasNestChildren: hasNestChildren,
63 record: record,
64 onTriggerExpand: onInternalTriggerExpand,
65 rowSupportExpand: rowSupportExpand,
66 expandable: mergedExpandable,
67 rowProps: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, rowProps), {}, {
68 className: (0, _classnames.default)(computeRowClassName, rowProps === null || rowProps === void 0 ? void 0 : rowProps.className),
69 onClick: onClick
70 })
71 });
72}
\No newline at end of file