UNPKG

9.12 kBTypeScriptView Raw
1/// <reference types="react" />
2import { Props } from "@blueprintjs/core";
3import { ItemListRenderer } from "./itemListRenderer";
4import { ItemRenderer } from "./itemRenderer";
5import { CreateNewItem } from "./listItemsUtils";
6import { ItemListPredicate, ItemPredicate } from "./predicate";
7/**
8 * Equality test comparator to determine if two {@link ListItemsProps} items are equivalent.
9 *
10 * @return `true` if the two items are equivalent.
11 */
12export declare type ItemsEqualComparator<T> = (itemA: T, itemB: T) => boolean;
13/**
14 * Union of all possible types for {@link ListItemsProps#itemsEqual}.
15 */
16export declare type ItemsEqualProp<T> = ItemsEqualComparator<T> | keyof T;
17/** @deprecated use ListItemsProps */
18export declare type IListItemsProps<T> = ListItemsProps<T>;
19/** Reusable generic props for a component that operates on a filterable, selectable list of `items`. */
20export interface ListItemsProps<T> extends Props {
21 /**
22 * The currently focused item for keyboard interactions, or `null` to
23 * indicate that no item is active. If omitted or `undefined`, this prop will be
24 * uncontrolled (managed by the component's state). Use `onActiveItemChange`
25 * to listen for updates.
26 */
27 activeItem?: T | CreateNewItem | null;
28 /** Array of items in the list. */
29 items: T[];
30 /**
31 * Specifies how to test if two items are equal. By default, simple strict
32 * equality (`===`) is used to compare two items.
33 *
34 * If your items have a unique identifier field, simply provide the name of
35 * a property on the item that can be compared with strict equality to
36 * determine equivalence: `itemsEqual="id"` will check `a.id === b.id`.
37 *
38 * If more complex comparison logic is required, provide an equality
39 * comparator function that returns `true` if the two items are equal. The
40 * arguments to this function will never be `null` or `undefined`, as those
41 * values are handled before calling the function.
42 */
43 itemsEqual?: ItemsEqualProp<T>;
44 /**
45 * Determine if the given item is disabled. Provide a callback function, or
46 * simply provide the name of a boolean property on the item that exposes
47 * its disabled state.
48 */
49 itemDisabled?: keyof T | ((item: T, index: number) => boolean);
50 /**
51 * Customize querying of entire `items` array. Return new list of items.
52 * This method can reorder, add, or remove items at will.
53 * (Supports filter algorithms that operate on the entire set, rather than individual items.)
54 *
55 * If `itemPredicate` is also defined, this prop takes priority and the other will be ignored.
56 */
57 itemListPredicate?: ItemListPredicate<T>;
58 /**
59 * Customize querying of individual items.
60 *
61 * __Filtering a list of items.__ This function is invoked to filter the
62 * list of items as a query is typed. Return `true` to keep the item, or
63 * `false` to hide. This method is invoked once for each item, so it should
64 * be performant. For more complex queries, use `itemListPredicate` to
65 * operate once on the entire array. For the purposes of filtering the list,
66 * this prop is ignored if `itemListPredicate` is also defined.
67 *
68 * __Matching a pasted value to an item.__ This function is also invoked to
69 * match a pasted value to an existing item if possible. In this case, the
70 * function will receive `exactMatch=true`, and the function should return
71 * true only if the item _exactly_ matches the query. For the purposes of
72 * matching pasted values, this prop will be invoked even if
73 * `itemListPredicate` is defined.
74 */
75 itemPredicate?: ItemPredicate<T>;
76 /**
77 * Custom renderer for an item in the dropdown list. Receives a boolean indicating whether
78 * this item is active (selected by keyboard arrows) and an `onClick` event handler that
79 * should be attached to the returned element.
80 */
81 itemRenderer: ItemRenderer<T>;
82 /**
83 * Custom renderer for the contents of the dropdown.
84 *
85 * The default implementation invokes `itemRenderer` for each item that passes the predicate
86 * and wraps them all in a `Menu` element. If the query is empty then `initialContent` is returned,
87 * and if there are no items that match the predicate then `noResults` is returned.
88 */
89 itemListRenderer?: ItemListRenderer<T>;
90 /**
91 * React content to render when query is empty.
92 * If omitted, all items will be rendered (or result of `itemListPredicate` with empty query).
93 * If explicit `null`, nothing will be rendered when query is empty.
94 *
95 * This prop is ignored if a custom `itemListRenderer` is supplied.
96 */
97 initialContent?: React.ReactNode | null;
98 /**
99 * React content to render when filtering items returns zero results.
100 * If omitted, nothing will be rendered in this case.
101 *
102 * This prop is ignored if a custom `itemListRenderer` is supplied.
103 *
104 * NOTE: if passing a `MenuItem`, ensure it has `roleStructure="listoption"` prop.
105 */
106 noResults?: React.ReactNode;
107 /**
108 * Invoked when user interaction should change the active item: arrow keys
109 * move it up/down in the list, selecting an item makes it active, and
110 * changing the query may reset it to the first item in the list if it no
111 * longer matches the filter.
112 *
113 * If the "Create Item" option is displayed and currently active, then
114 * `isCreateNewItem` will be `true` and `activeItem` will be `null`. In this
115 * case, you should provide a valid `CreateNewItem` object to the
116 * `activeItem` _prop_ in order for the "Create Item" option to appear as
117 * active.
118 *
119 * __Note:__ You can instantiate a `CreateNewItem` object using the
120 * `getCreateNewItem()` utility exported from this package.
121 */
122 onActiveItemChange?: (activeItem: T | null, isCreateNewItem: boolean) => void;
123 /**
124 * Callback invoked when an item from the list is selected,
125 * typically by clicking or pressing `enter` key.
126 */
127 onItemSelect: (item: T, event?: React.SyntheticEvent<HTMLElement>) => void;
128 /**
129 * Callback invoked when multiple items are selected at once via pasting.
130 */
131 onItemsPaste?: (items: T[]) => void;
132 /**
133 * Callback invoked when the query string changes.
134 */
135 onQueryChange?: (query: string, event?: React.ChangeEvent<HTMLInputElement>) => void;
136 /**
137 * If provided, allows new items to be created using the current query
138 * string. This is invoked when user interaction causes one or many items to be
139 * created, either by pressing the `Enter` key or by clicking on the "Create
140 * Item" option. It transforms a query string into one or many items type.
141 */
142 createNewItemFromQuery?: (query: string) => T | T[];
143 /**
144 * Custom renderer to transform the current query string into a selectable
145 * "Create Item" option. If this function is provided, a "Create Item"
146 * option will be rendered at the end of the list of items. If this function
147 * is not provided, a "Create Item" option will not be displayed.
148 */
149 createNewItemRenderer?: (query: string, active: boolean, handleClick: React.MouseEventHandler<HTMLElement>) => JSX.Element | undefined;
150 /**
151 * Determines the position of the `createNewItem` within the list: first or
152 * last. Only relevant when `createNewItemRenderer` is defined.
153 *
154 * @default 'last'
155 */
156 createNewItemPosition?: "first" | "last";
157 /**
158 * Whether the active item should be reset to the first matching item _every
159 * time the query changes_ (via prop or by user input).
160 *
161 * @default true
162 */
163 resetOnQuery?: boolean;
164 /**
165 * Whether the active item should be reset to the first matching item _when
166 * an item is selected_. The query will also be reset to the empty string.
167 *
168 * @default false
169 */
170 resetOnSelect?: boolean;
171 /**
172 * When `activeItem` is controlled, whether the active item should _always_
173 * be scrolled into view when the prop changes. If `false`, only changes
174 * that result from built-in interactions (clicking, querying, or using
175 * arrow keys) will scroll the active item into view. Ignored if the
176 * `activeItem` prop is omitted (uncontrolled behavior).
177 *
178 * @default true
179 */
180 scrollToActiveItem?: boolean;
181 /**
182 * Query string passed to `itemListPredicate` or `itemPredicate` to filter items.
183 * This value is controlled: its state must be managed externally by attaching an `onChange`
184 * handler to the relevant element in your `renderer` implementation.
185 */
186 query?: string;
187}
188/**
189 * Utility function for executing the {@link ListItemsProps#itemsEqual} prop to test
190 * for equality between two items.
191 *
192 * @return `true` if the two items are equivalent according to `itemsEqualProp`.
193 */
194export declare function executeItemsEqual<T>(itemsEqualProp: ItemsEqualProp<T> | undefined, itemA: T | null | undefined, itemB: T | null | undefined): boolean;