Skip to content

Commit 8e2609f

Browse files
authored
fix: force update to keep disable state work (#612)
* fix: force update to keep disable state work * refactor: replace disabled ref cache with state * perf: remove unnecessary setState and init map
1 parent 2943745 commit 8e2609f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/OptionList.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,18 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
160160
}, [searchValue]);
161161

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

165-
// Clear cache if `leftMaxCount` changed
166166
React.useEffect(() => {
167167
if (leftMaxCount) {
168-
disabledCacheRef.current.clear();
168+
setDisabledCache(new Map());
169169
}
170170
}, [leftMaxCount]);
171171

172172
function getDisabledWithCache(node: DataNode) {
173173
const value = node[fieldNames.value];
174-
if (!disabledCacheRef.current.has(value)) {
174+
if (!disabledCache.has(value)) {
175175
const entity = valueEntities.get(value);
176176
const isLeaf = (entity.children || []).length === 0;
177177

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

186186
const checkableChildrenCount = checkableChildren.length;
187-
disabledCacheRef.current.set(value, checkableChildrenCount > leftMaxCount);
187+
disabledCache.set(value, checkableChildrenCount > leftMaxCount);
188188
} else {
189-
disabledCacheRef.current.set(value, false);
189+
disabledCache.set(value, false);
190190
}
191191
}
192-
return disabledCacheRef.current.get(value);
192+
return disabledCache.get(value);
193193
}
194194

195195
const nodeDisabled = useEvent((node: DataNode) => {

0 commit comments

Comments
 (0)