amis-rpc-design/node_modules/antd/lib/table/hooks/useLazyKVMap.js
2023-10-07 19:42:30 +08:00

36 lines
1.2 KiB
JavaScript

"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useLazyKVMap;
var React = _interopRequireWildcard(require("react"));
function useLazyKVMap(data, childrenColumnName, getRowKey) {
const mapCacheRef = React.useRef({});
function getRecordByKey(key) {
if (!mapCacheRef.current || mapCacheRef.current.data !== data || mapCacheRef.current.childrenColumnName !== childrenColumnName || mapCacheRef.current.getRowKey !== getRowKey) {
const kvMap = new Map();
/* eslint-disable no-inner-declarations */
function dig(records) {
records.forEach((record, index) => {
const rowKey = getRowKey(record, index);
kvMap.set(rowKey, record);
if (record && typeof record === 'object' && childrenColumnName in record) {
dig(record[childrenColumnName] || []);
}
});
}
/* eslint-enable */
dig(data);
mapCacheRef.current = {
data,
childrenColumnName,
kvMap,
getRowKey
};
}
return mapCacheRef.current.kvMap.get(key);
}
return [getRecordByKey];
}