UNPKG

2.51 kBTypeScriptView Raw
1/// <reference types="react" />
2import type { CreateNewItem } from "./listItemsUtils";
3/**
4 * An object describing how to render the list of items.
5 * An `itemListRenderer` receives this object as its sole argument.
6 */
7export interface ItemListRendererProps<T> {
8 /**
9 * The currently focused item (for keyboard interactions), or `null` to
10 * indicate that no item is active.
11 */
12 activeItem: T | CreateNewItem | null;
13 /**
14 * Array of items filtered by `itemListPredicate` or `itemPredicate`.
15 * See `items` for the full list of items.
16 *
17 * Use `renderFilteredItems()` utility function from this library to
18 * map each item in this array through `renderItem`, with support for
19 * optional `noResults` and `initialContent` states.
20 */
21 filteredItems: T[];
22 /**
23 * Array of all items in the list.
24 * See `filteredItems` for a filtered array based on `query` and predicate props.
25 */
26 items: T[];
27 /**
28 * The current query string.
29 */
30 query: string;
31 /**
32 * A ref handler that should be attached to the parent HTML element of the menu items.
33 * This is required for the active item to scroll into view automatically.
34 */
35 itemsParentRef: React.Ref<HTMLUListElement>;
36 /**
37 * Props to apply to the `Menu` created within the `itemListRenderer`
38 */
39 menuProps?: React.HTMLAttributes<HTMLUListElement>;
40 /**
41 * Call this function to render an item.
42 * This retrieves the modifiers for the item and delegates actual rendering
43 * to the owner component's `itemRenderer` prop.
44 */
45 renderItem: (item: T, index: number) => React.JSX.Element | null;
46 /**
47 * Call this function to render the "create new item" view component.
48 *
49 * @returns null when creating a new item is not available, and undefined if the createNewItemRenderer returns undefined
50 */
51 renderCreateItem: () => React.JSX.Element | null | undefined;
52}
53/** Type alias for a function that renders the list of items. */
54export type ItemListRenderer<T> = (itemListProps: ItemListRendererProps<T>) => React.JSX.Element | null;
55/**
56 * `ItemListRenderer` helper method for rendering each item in `filteredItems`,
57 * with optional support for `noResults` (when filtered items is empty)
58 * and `initialContent` (when query is empty).
59 */
60export declare function renderFilteredItems(props: ItemListRendererProps<any>, noResults?: React.ReactNode, initialContent?: React.ReactNode | null): React.ReactNode;