UNPKG

@chakra-ui/react

Version:

Responsive and accessible React UI components built with React and Emotion

153 lines (152 loc) • 4.71 kB
import { type CollectionOptions, type ListCollection } from "@ark-ui/react/collection"; import { type AsyncListLoadResult } from "../../hooks"; import type { CommandItem, CommandSelectionDetails } from "./command"; export interface CommandLoadDetails<T extends CommandItem = CommandItem> { /** * The query to load items for. */ query: string; /** * The open page, or `null` at the top level. */ page: T | null; /** * Aborted when a newer load starts or the component unmounts. */ signal: AbortSignal; /** * Where the page to load starts. `undefined` for the first page. */ cursor?: string | undefined; } export type CommandLoadResult<T> = T[] | AsyncListLoadResult<T>; export interface UseCommandCollectionProps<T extends CommandItem> extends Omit<CollectionOptions<T>, "items"> { /** * The items to search locally. An item with a `parent` lives on that item's * page. */ initialItems?: T[] | readonly T[] | undefined; /** * Scores an item from 0 (hidden) to 1. Defaults to a fuzzy match on the * label and `keywords`. Pass `null` to show the items as given, e.g. when a * server already filtered them. */ filter?: ((item: T, query: string) => number) | null | undefined; /** * The maximum number of items to show. */ limit?: number | undefined; /** * Loads items for the query. They show after the local items. Runs again * when the page changes. Return `{ items, cursor }` to load more pages with * `loadMore`. */ load?: ((details: CommandLoadDetails<T>) => Promise<CommandLoadResult<T>>) | undefined; /** * The controlled query. */ query?: string | undefined; /** * Called when the query changes. */ onQueryChange?: ((query: string) => void) | undefined; /** * The query to rank with. Defaults to `query`. Pass a deferred or debounced * copy to keep typing responsive on large lists. */ filterQuery?: string | undefined; /** * Whether a query also matches items in nested pages. * @default false */ deepSearch?: boolean | undefined; /** * Called when the current page changes. */ onPageChange?: ((page: T | null) => void) | undefined; } export interface UseCommandCollectionReturn<T extends CommandItem> { /** * The ranked items to show. */ collection: ListCollection<T>; /** * The current query, as shown in the input. */ query: string; /** * The query the collection is ranked with. */ filterQuery: string; /** * Ranks the items on the open page for a query right away and returns the * top one. */ getFirstMatch: (query: string) => T | undefined; /** * Updates the query. */ search: (query: string) => void; /** * Whether `load` is waiting for results. */ loading: boolean; /** * The error from the last `load`, if any. */ error: Error | undefined; /** * Runs `load` again for the current query. */ reload: VoidFunction; /** * Whether `load` has more pages. */ hasMore: boolean; /** * Loads the next page and appends its items. */ loadMore: VoidFunction; /** * The open page, or `null` at the top level. */ page: T | null; /** * The pages from the top level to the open page. */ pages: T[]; /** * Opens a page by item value, or the top level for `null`. */ setPage: (value: string | null) => void; /** * Goes back to the previous page. Returns `false` when there's none. */ popPage: () => boolean; /** * Whether the item has children or sets `page: true`. */ isPage: (item: T) => boolean; /** * The item's ancestors below the open page. */ getPath: (item: T) => T[]; /** * Clears the query and starts over at the top level, or at the given page. */ reset: (page?: string | null) => void; /** * The item to highlight after going back: the one the user came from. */ highlightedValue: string | null; /** * Opens the page of a selected item that is a page. Returns `true` when it * did, so the command stays open. */ onSelect: (details: CommandSelectionDetails) => boolean; /** * Handles Backspace (up a page) and Escape (back). `Command.Input` calls it. */ onInputKeyDown: (event: React.KeyboardEvent<HTMLInputElement>) => void; } export declare function useCommandCollection<T extends CommandItem>(props: UseCommandCollectionProps<T>): UseCommandCollectionReturn<T>;