fix: won't display warning in when placeholder (#2346) (#2377)

* fix: won't display warning in when placeholder (#2346)

* chore: update use-multiselect-list-state.ts

* chore: add change log
This commit is contained in:
Frozen FIsh 2024-03-03 15:09:21 -06:00 committed by GitHub
parent e83595e06f
commit d8b0ef528b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 9 deletions

View File

@ -0,0 +1,5 @@
---
"@nextui-org/use-aria-multiselect": patch
---
fix won't display warning when placeholder (#2346)

View File

@ -1,6 +1,6 @@
import {ListState, useListState} from "@react-stately/list";
import {CollectionBase, MultipleSelection, Node} from "@react-types/shared";
import {Key} from "react";
import {Key, useMemo} from "react";
export interface MultiSelectListProps<T> extends CollectionBase<T>, MultipleSelection {}
@ -25,19 +25,21 @@ export function useMultiSelectListState<T extends object>(
selectionManager: {setSelectedKeys, selectedKeys, selectionMode},
} = useListState<T>(props);
const missingKeys: Key[] = [];
const missingKeys: Key[] = useMemo(() => {
if (selectedKeys.size !== 0) {
return Array.from(selectedKeys)
.filter(Boolean)
.filter((key) => !collection.getItem(key));
}
return [];
}, [selectedKeys, collection]);
const selectedItems = (
selectedKeys.size !== 0
? Array.from(selectedKeys)
.map((key) => {
const item = collection.getItem(key);
if (!item) {
missingKeys.push(key);
}
return item;
return collection.getItem(key);
})
// Remove undefined values when some keys are not present in the collection
.filter(Boolean)