Skip to content

Commit

Permalink
fix: force update to keep disable state work (#612)
Browse files Browse the repository at this point in the history
* fix: force update to keep disable state work

* refactor: replace disabled ref cache with state

* perf: remove unnecessary setState and init map
  • Loading branch information
aojunhao123 authored Dec 25, 2024
1 parent 2943745 commit 8e2609f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,18 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
}, [searchValue]);

// ========================= Disabled =========================
const disabledCacheRef = React.useRef<Map<string, boolean>>(new Map());
// Cache disabled states in React state to ensure re-render when cache updates
const [disabledCache, setDisabledCache] = React.useState<Map<string, boolean>>(() => new Map());

// Clear cache if `leftMaxCount` changed
React.useEffect(() => {
if (leftMaxCount) {
disabledCacheRef.current.clear();
setDisabledCache(new Map());
}
}, [leftMaxCount]);

function getDisabledWithCache(node: DataNode) {
const value = node[fieldNames.value];
if (!disabledCacheRef.current.has(value)) {
if (!disabledCache.has(value)) {
const entity = valueEntities.get(value);
const isLeaf = (entity.children || []).length === 0;

Expand All @@ -184,12 +184,12 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
);

const checkableChildrenCount = checkableChildren.length;
disabledCacheRef.current.set(value, checkableChildrenCount > leftMaxCount);
disabledCache.set(value, checkableChildrenCount > leftMaxCount);
} else {
disabledCacheRef.current.set(value, false);
disabledCache.set(value, false);
}
}
return disabledCacheRef.current.get(value);
return disabledCache.get(value);
}

const nodeDisabled = useEvent((node: DataNode) => {
Expand Down

0 comments on commit 8e2609f

Please sign in to comment.