Skip to content

Commit a5b1e9b

Browse files
committed
refactor: replace disabled ref cache with state
1 parent c098a17 commit a5b1e9b

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/OptionList.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +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-
// Force update is needed since clearing cache alone doesn't trigger
166-
// a recalculation of node disabled states
167-
const [, setForceUpdate] = React.useState({});
168-
169-
// When leftMaxCount changes, we need to:
170-
// 1. Clear the disabled state cache
171-
// 2. Trigger a re-render to recalculate all node disabled states
172-
// This ensures parent nodes are properly disabled when max count is reached
173166
React.useEffect(() => {
174167
if (leftMaxCount) {
175-
disabledCacheRef.current.clear();
176-
setForceUpdate({});
168+
setDisabledCache(new Map());
177169
}
178170
}, [leftMaxCount]);
179171

180172
function getDisabledWithCache(node: DataNode) {
181173
const value = node[fieldNames.value];
182-
if (!disabledCacheRef.current.has(value)) {
174+
if (!disabledCache.has(value)) {
183175
const entity = valueEntities.get(value);
184176
const isLeaf = (entity.children || []).length === 0;
185177

@@ -192,12 +184,16 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
192184
);
193185

194186
const checkableChildrenCount = checkableChildren.length;
195-
disabledCacheRef.current.set(value, checkableChildrenCount > leftMaxCount);
187+
const newCache = new Map(disabledCache);
188+
newCache.set(value, checkableChildrenCount > leftMaxCount);
189+
setDisabledCache(newCache);
196190
} else {
197-
disabledCacheRef.current.set(value, false);
191+
const newCache = new Map(disabledCache);
192+
newCache.set(value, false);
193+
setDisabledCache(newCache);
198194
}
199195
}
200-
return disabledCacheRef.current.get(value);
196+
return disabledCache.get(value);
201197
}
202198

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

0 commit comments

Comments
 (0)