Skip to content

Commit

Permalink
fix: selectedKeys are not present in the collection warning in async …
Browse files Browse the repository at this point in the history
…case (#2648)

* fix(hook): add AsyncLoadable & show warning after data is loaded

* fix(hooks): change isLoading default state to true

* refactor(hooks): move the logic to missingKeys

* refactor(changeset): rephrase changeset description
  • Loading branch information
wingkwong committed Apr 14, 2024
1 parent e458432 commit abf532b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/rare-plums-speak.md
@@ -0,0 +1,6 @@
---
"@nextui-org/use-aria-multiselect": patch
"@nextui-org/stories-utils": patch
---

Fixed an issue where a warning was triggered in the Select component when `defaultSelectedKeys` were used and items were still loading (#2605).
@@ -1,8 +1,11 @@
import {ListState, useListState} from "@react-stately/list";
import {CollectionBase, MultipleSelection, Node} from "@react-types/shared";
import {CollectionBase, MultipleSelection, AsyncLoadable, Node} from "@react-types/shared";
import {Key, useMemo} from "react";

export interface MultiSelectListProps<T> extends CollectionBase<T>, MultipleSelection {}
export interface MultiSelectListProps<T>
extends CollectionBase<T>,
AsyncLoadable,
MultipleSelection {}

export interface MultiSelectListState<T> extends ListState<T> {
/** The keys for the currently selected items. */
Expand All @@ -26,7 +29,7 @@ export function useMultiSelectListState<T extends object>(
} = useListState<T>(props);

const missingKeys: Key[] = useMemo(() => {
if (selectedKeys.size !== 0) {
if (!props.isLoading && selectedKeys.size !== 0) {
return Array.from(selectedKeys)
.filter(Boolean)
.filter((key) => !collection.getItem(`${key}`));
Expand Down
Expand Up @@ -14,7 +14,7 @@ export type UsePokemonListProps = {
export function usePokemonList({fetchDelay = 0}: UsePokemonListProps = {}) {
const [items, setItems] = useState<Pokemon[]>([]);
const [hasMore, setHasMore] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [offset, setOffset] = useState(0);
const limit = 20; // Number of items per page, adjust as necessary

Expand Down

0 comments on commit abf532b

Please sign in to comment.