Skip to content

Commit faa432b

Browse files
committed
chore: cache cal
1 parent 3daaa8d commit faa432b

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

src/OptionList.tsx

+37-39
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,38 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
160160
// eslint-disable-next-line react-hooks/exhaustive-deps
161161
}, [searchValue]);
162162

163-
// const getSelectableKeys = (targetNode: DataNode, names: FieldNames): Key[] => {
164-
// const keys = [targetNode[names.value]];
165-
// if (!Array.isArray(targetNode.children)) {
166-
// return keys;
167-
// }
168-
169-
// return targetNode.children.reduce((acc, child) => {
170-
// if (!child.disabled) {
171-
// acc.push(...getSelectableKeys(child, names));
172-
// }
173-
// return acc;
174-
// }, keys);
175-
// };
163+
// ========================= Disabled =========================
164+
const disabledCacheRef = React.useRef<Map<string, boolean>>(new Map());
165+
166+
// Clear cache if `leftMaxCount` changed
167+
React.useEffect(() => {
168+
if (leftMaxCount) {
169+
disabledCacheRef.current.clear();
170+
}
171+
}, [leftMaxCount]);
172+
173+
function getDisabledWithCache(node: DataNode) {
174+
const value = node[fieldNames.value];
175+
if (!disabledCacheRef.current.has(value)) {
176+
const entity = valueEntities.get(value);
177+
const isLeaf = (entity.children || []).length === 0;
178+
179+
if (!isLeaf) {
180+
const checkableChildren = entity.children.filter(
181+
childTreeNode =>
182+
!childTreeNode.node.disabled &&
183+
!childTreeNode.node.disableCheckbox &&
184+
!checkedKeys.includes(childTreeNode.node[fieldNames.value]),
185+
);
186+
187+
const checkableChildrenCount = checkableChildren.length;
188+
disabledCacheRef.current.set(value, checkableChildrenCount > leftMaxCount);
189+
} else {
190+
disabledCacheRef.current.set(value, false);
191+
}
192+
}
193+
return disabledCacheRef.current.get(value);
194+
}
176195

177196
const nodeDisabled = useEvent((node: DataNode) => {
178197
const nodeValue = node[fieldNames.value];
@@ -181,39 +200,18 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
181200
return false;
182201
}
183202

184-
// console.log('--->', node);
185-
186203
if (leftMaxCount === null) {
187204
return false;
188205
}
189206

190-
if (!leafCountOnly && leftMaxCount <= 0) {
207+
if (leftMaxCount <= 0) {
191208
return true;
192209
}
193210

194-
// const cacheKey = `${nodeValue}-${checkedKeys.join(',')}-${maxCount}`;
195-
196-
// // check cache
197-
// if (disabledCacheRef.current.has(cacheKey)) {
198-
// return disabledCacheRef.current.get(cacheKey);
199-
// }
200-
201-
// // calculate disabled state
202-
// const selectableNodeKeys = getSelectableKeys(node, fieldNames);
203-
// const simulatedCheckedKeys = [...checkedKeys, ...selectableNodeKeys];
204-
// const simulatedDisplayValues = formatStrategyValues(
205-
// simulatedCheckedKeys as SafeKey[],
206-
// showCheckedStrategy,
207-
// keyEntities,
208-
// fieldNames,
209-
// );
210-
211-
// const isDisabled = simulatedDisplayValues.length > maxCount;
212-
213-
// // update cache
214-
// disabledCacheRef.current.set(cacheKey, isDisabled);
215-
216-
// return isDisabled;
211+
// This is a low performance calculation
212+
if (leafCountOnly && leftMaxCount) {
213+
return getDisabledWithCache(node);
214+
}
217215

218216
return false;
219217
});

src/TreeSelect.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
226226
const mergedTreeData = useTreeData(treeData, children, treeDataSimpleMode);
227227

228228
const { keyEntities, valueEntities } = useDataEntities(mergedTreeData, mergedFieldNames);
229-
console.log('-->', valueEntities);
230229

231230
/** Get `missingRawValues` which not exist in the tree yet */
232231
const splitRawValues = React.useCallback(

0 commit comments

Comments
 (0)