Skip to content

Commit

Permalink
fix(useSelections): setSelected should support non-array value (ali…
Browse files Browse the repository at this point in the history
…baba#2559)

* fix(useSelections): `setSelected` should support `undefined`

* test: add case for `null`
  • Loading branch information
liuyib authored Jun 2, 2024
1 parent ccf14eb commit a75a982
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/hooks/src/useSelections/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ describe('useSelections', () => {
expect(result.current.noneSelected).toBe(false);
expect(result.current.allSelected).toBe(true);
expect(result.current.partiallySelected).toBe(false);

// Keep compatible with older versions.
act(() => {
expect(() => setSelected(undefined!)).not.toThrowError();
expect(() => setSelected(null!)).not.toThrowError();
});
};

runCaseCallback(caseCallback, caseCallback);
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useSelections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export default function useSelections<T>(items: T[], options?: T[] | Options<T>)
const selectedMap = useMemo(() => {
const keyToItemMap = new Map();

if (!Array.isArray(selected)) {
return keyToItemMap;
}

selected.forEach((item) => {
keyToItemMap.set(getKey(item), item);
});
Expand Down

0 comments on commit a75a982

Please sign in to comment.