UNPKG

8.64 kBTypeScriptView Raw
1import * as React from "react";
2import { AbstractComponent2, Props } from "@blueprintjs/core";
3import { CreateNewItem, ListItemsProps } from "../../common";
4export declare type QueryListProps<T> = IQueryListProps<T>;
5/** @deprecated use QueryListProps */
6export interface IQueryListProps<T> extends ListItemsProps<T> {
7 /**
8 * Initial active item, useful if the parent component is controlling its selectedItem but
9 * not activeItem.
10 */
11 initialActiveItem?: T;
12 /**
13 * Additional props to apply to the `Menu` that is created within the `QueryList`
14 */
15 menuProps?: React.HTMLAttributes<HTMLUListElement>;
16 /**
17 * Callback invoked when user presses a key, after processing `QueryList`'s own key events
18 * (up/down to navigate active item). This callback is passed to `renderer` and (along with
19 * `onKeyUp`) can be attached to arbitrary content elements to support keyboard selection.
20 */
21 onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
22 /**
23 * Callback invoked when user releases a key, after processing `QueryList`'s own key events
24 * (enter to select active item). This callback is passed to `renderer` and (along with
25 * `onKeyDown`) can be attached to arbitrary content elements to support keyboard selection.
26 */
27 onKeyUp?: React.KeyboardEventHandler<HTMLElement>;
28 /**
29 * Customize rendering of the component.
30 * Receives an object with props that should be applied to elements as necessary.
31 */
32 renderer: (listProps: QueryListRendererProps<T>) => JSX.Element;
33 /**
34 * Whether the list is disabled.
35 *
36 * @default false
37 */
38 disabled?: boolean;
39}
40export declare type QueryListRendererProps<T> = IQueryListRendererProps<T>;
41/**
42 * An object describing how to render a `QueryList`.
43 * A `QueryList` `renderer` receives this object as its sole argument.
44 *
45 * @deprecated use QueryListRendererProps
46 */
47export interface IQueryListRendererProps<T>// Omit `createNewItem`, because it's used strictly for internal tracking.
48 extends Pick<IQueryListState<T>, "activeItem" | "filteredItems" | "query">, Props {
49 /**
50 * Selection handler that should be invoked when a new item has been chosen,
51 * perhaps because the user clicked it.
52 */
53 handleItemSelect: (item: T, event?: React.SyntheticEvent<HTMLElement>) => void;
54 /**
55 * Handler that should be invoked when the user pastes one or more values.
56 *
57 * This callback will use `itemPredicate` with `exactMatch=true` to find a
58 * subset of `items` exactly matching the pasted `values` provided, then it
59 * will invoke `onItemsPaste` with those found items. Each pasted value that
60 * does not exactly match an item will be ignored.
61 *
62 * If creating items is enabled (by providing both `createNewItemFromQuery`
63 * and `createNewItemRenderer`), then pasted values that do not exactly
64 * match an existing item will emit a new item as created via
65 * `createNewItemFromQuery`.
66 *
67 * If `itemPredicate` returns multiple matching items for a particular query
68 * in `queries`, then only the first matching item will be emitted.
69 */
70 handlePaste: (queries: string[]) => void;
71 /**
72 * Keyboard handler for up/down arrow keys to shift the active item.
73 * Attach this handler to any element that should support this interaction.
74 */
75 handleKeyDown: React.KeyboardEventHandler<HTMLElement>;
76 /**
77 * Keyboard handler for enter key to select the active item.
78 * Attach this handler to any element that should support this interaction.
79 */
80 handleKeyUp: React.KeyboardEventHandler<HTMLElement>;
81 /**
82 * Change handler for query string. Attach this to an input element to allow
83 * `QueryList` to control the query.
84 */
85 handleQueryChange: React.ChangeEventHandler<HTMLInputElement>;
86 /** Rendered elements returned from `itemListRenderer` prop. */
87 itemList: React.ReactNode;
88}
89export interface IQueryListState<T> {
90 /** The currently focused item (for keyboard interactions). */
91 activeItem: T | CreateNewItem | null;
92 /**
93 * The item returned from `createNewItemFromQuery(this.state.query)`, cached
94 * to avoid continuous reinstantions within `isCreateItemRendered`, where
95 * this element will be used to hide the "Create Item" option if its value
96 * matches the current `query`.
97 */
98 createNewItem: T | T[] | undefined;
99 /** The original `items` array filtered by `itemListPredicate` or `itemPredicate`. */
100 filteredItems: T[];
101 /** The current query string. */
102 query: string;
103}
104/**
105 * Query list component.
106 *
107 * @see https://blueprintjs.com/docs/#select/query-list
108 */
109export declare class QueryList<T> extends AbstractComponent2<QueryListProps<T>, IQueryListState<T>> {
110 static displayName: string;
111 static defaultProps: {
112 disabled: boolean;
113 resetOnQuery: boolean;
114 };
115 /** @deprecated no longer necessary now that the TypeScript parser supports type arguments on JSX element tags */
116 static ofType<U>(): new (props: QueryListProps<U>) => QueryList<U>;
117 private itemsParentRef?;
118 private itemRefs;
119 private refHandlers;
120 /**
121 * Flag indicating that we should check whether selected item is in viewport
122 * after rendering, typically because of keyboard change. Set to `true` when
123 * manipulating state in a way that may cause active item to scroll away.
124 */
125 private shouldCheckActiveItemInViewport;
126 /**
127 * The item that we expect to be the next selected active item (based on click
128 * or key interactions). When scrollToActiveItem = false, used to detect if
129 * an unexpected external change to the active item has been made.
130 */
131 private expectedNextActiveItem;
132 /**
133 * Flag which is set to true while in between an ENTER "keydown" event and its
134 * corresponding "keyup" event.
135 *
136 * When entering text via an IME (https://en.wikipedia.org/wiki/Input_method),
137 * the ENTER key is pressed to confirm the character(s) to be input from a list
138 * of options. The operating system intercepts the ENTER "keydown" event and
139 * prevents it from propagating to the application, but "keyup" is still
140 * fired, triggering a spurious event which this component does not expect.
141 *
142 * To work around this quirk, we keep track of "real" key presses by setting
143 * this flag in handleKeyDown.
144 */
145 private isEnterKeyPressed;
146 constructor(props: QueryListProps<T>, context?: any);
147 render(): JSX.Element;
148 componentDidUpdate(prevProps: QueryListProps<T>): void;
149 scrollActiveItemIntoView(): void;
150 setQuery(query: string, resetActiveItem?: boolean | undefined, props?: Readonly<QueryListProps<T>> & Readonly<{
151 children?: React.ReactNode;
152 }>): void;
153 setActiveItem(activeItem: T | CreateNewItem | null): void;
154 /** default `itemListRenderer` implementation */
155 private renderItemList;
156 /** wrapper around `itemRenderer` to inject props */
157 private renderItem;
158 private renderCreateItemMenuItem;
159 private getActiveElement;
160 private getActiveIndex;
161 private getItemsParentPadding;
162 private handleItemCreate;
163 private handleItemSelect;
164 private handlePaste;
165 private handleKeyDown;
166 private handleKeyUp;
167 private handleInputQueryChange;
168 /**
169 * Get the next enabled item, moving in the given direction from the start
170 * index. A `null` return value means no suitable item was found.
171 *
172 * @param direction amount to move in each iteration, typically +/-1
173 * @param startIndex item to start iteration
174 */
175 private getNextActiveItem;
176 /**
177 * @param createNewItem Checks if this item would match the current query. Cannot check this.state.createNewItem
178 * every time since state may not have been updated yet.
179 */
180 private isCreateItemRendered;
181 private isCreateItemFirst;
182 private canCreateItems;
183 private wouldCreatedItemMatchSomeExistingItem;
184 private maybeResetQuery;
185}
186/**
187 * Get the next enabled item, moving in the given direction from the start
188 * index. A `null` return value means no suitable item was found.
189 *
190 * @param items the list of items
191 * @param itemDisabled callback to determine if a given item is disabled
192 * @param direction amount to move in each iteration, typically +/-1
193 * @param startIndex which index to begin moving from
194 */
195export declare function getFirstEnabledItem<T>(items: T[], itemDisabled?: keyof T | ((item: T, index: number) => boolean), direction?: number, startIndex?: number): T | CreateNewItem | null;