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

29 lines
1016 B
JavaScript

import * as React from 'react';
export default 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];
}