UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4var _typeof3 = require("@babel/runtime/helpers/typeof");
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = useExpand;
9var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
12var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
13var React = _interopRequireWildcard(require("react"));
14var _constant = require("../constant");
15var _expandUtil = require("../utils/expandUtil");
16var _legacyUtil = require("../utils/legacyUtil");
17function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
18function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof3(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
19function useExpand(props, mergedData, getRowKey) {
20 var expandableConfig = (0, _legacyUtil.getExpandableProps)(props);
21 var expandIcon = expandableConfig.expandIcon,
22 expandedRowKeys = expandableConfig.expandedRowKeys,
23 defaultExpandedRowKeys = expandableConfig.defaultExpandedRowKeys,
24 defaultExpandAllRows = expandableConfig.defaultExpandAllRows,
25 expandedRowRender = expandableConfig.expandedRowRender,
26 onExpand = expandableConfig.onExpand,
27 onExpandedRowsChange = expandableConfig.onExpandedRowsChange,
28 childrenColumnName = expandableConfig.childrenColumnName;
29 var mergedExpandIcon = expandIcon || _expandUtil.renderExpandIcon;
30 var mergedChildrenColumnName = childrenColumnName || 'children';
31 var expandableType = React.useMemo(function () {
32 if (expandedRowRender) {
33 return 'row';
34 }
35 /* eslint-disable no-underscore-dangle */
36 /**
37 * Fix https://github.com/ant-design/ant-design/issues/21154
38 * This is a workaround to not to break current behavior.
39 * We can remove follow code after final release.
40 *
41 * To other developer:
42 * Do not use `__PARENT_RENDER_ICON__` in prod since we will remove this when refactor
43 */
44 if (props.expandable && props.internalHooks === _constant.INTERNAL_HOOKS && props.expandable.__PARENT_RENDER_ICON__ || mergedData.some(function (record) {
45 return record && (0, _typeof2.default)(record) === 'object' && record[mergedChildrenColumnName];
46 })) {
47 return 'nest';
48 }
49 /* eslint-enable */
50 return false;
51 }, [!!expandedRowRender, mergedData]);
52 var _React$useState = React.useState(function () {
53 if (defaultExpandedRowKeys) {
54 return defaultExpandedRowKeys;
55 }
56 if (defaultExpandAllRows) {
57 return (0, _expandUtil.findAllChildrenKeys)(mergedData, getRowKey, mergedChildrenColumnName);
58 }
59 return [];
60 }),
61 _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
62 innerExpandedKeys = _React$useState2[0],
63 setInnerExpandedKeys = _React$useState2[1];
64 var mergedExpandedKeys = React.useMemo(function () {
65 return new Set(expandedRowKeys || innerExpandedKeys || []);
66 }, [expandedRowKeys, innerExpandedKeys]);
67 var onTriggerExpand = React.useCallback(function (record) {
68 var key = getRowKey(record, mergedData.indexOf(record));
69 var newExpandedKeys;
70 var hasKey = mergedExpandedKeys.has(key);
71 if (hasKey) {
72 mergedExpandedKeys.delete(key);
73 newExpandedKeys = (0, _toConsumableArray2.default)(mergedExpandedKeys);
74 } else {
75 newExpandedKeys = [].concat((0, _toConsumableArray2.default)(mergedExpandedKeys), [key]);
76 }
77 setInnerExpandedKeys(newExpandedKeys);
78 if (onExpand) {
79 onExpand(!hasKey, record);
80 }
81 if (onExpandedRowsChange) {
82 onExpandedRowsChange(newExpandedKeys);
83 }
84 }, [getRowKey, mergedExpandedKeys, mergedData, onExpand, onExpandedRowsChange]);
85
86 // Warning if use `expandedRowRender` and nest children in the same time
87 if (process.env.NODE_ENV !== 'production' && expandedRowRender && mergedData.some(function (record) {
88 return Array.isArray(record === null || record === void 0 ? void 0 : record[mergedChildrenColumnName]);
89 })) {
90 (0, _warning.default)(false, '`expandedRowRender` should not use with nested Table');
91 }
92 return [expandableConfig, expandableType, mergedExpandedKeys, mergedExpandIcon, mergedChildrenColumnName, onTriggerExpand];
93}
\No newline at end of file