-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
suggest2.tsx
405 lines (351 loc) · 15.7 KB
/
suggest2.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright 2022 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @fileoverview "V2" variant of Suggest which uses Popover2 instead of Popover2 */
import classNames from "classnames";
import * as React from "react";
import {
AbstractPureComponent2,
DISPLAYNAME_PREFIX,
InputGroup,
InputGroupProps2,
Keys,
mergeRefs,
refHandler,
setRef,
Utils,
} from "@blueprintjs/core";
import { Popover2, Popover2TargetProps, PopupKind } from "@blueprintjs/popover2";
import { Classes, ListItemsProps, SelectPopoverProps } from "../../common";
import { QueryList, QueryListRendererProps } from "../query-list/queryList";
export interface Suggest2Props<T> extends ListItemsProps<T>, Omit<SelectPopoverProps, "popoverTargetProps"> {
/**
* Whether the popover should close after selecting an item.
*
* @default true
*/
closeOnSelect?: boolean;
/** Whether the input field should be disabled. */
disabled?: boolean;
/**
* Whether the component should take up the full width of its container.
*/
fill?: boolean;
/**
* Props to pass to the query [InputGroup component](#core/components/text-inputs.input-group).
*
* Some properties are unavailable:
* - `inputProps.value`: use `query` instead
* - `inputProps.onChange`: use `onQueryChange` instead
* - `inputProps.disabled`: use `disabled` instead
* - `inputProps.fill`: use `fill` instead
*
* Other notes:
* - `inputProps.tagName` will override `popoverProps.targetTagName`
* - `inputProps.className` will work as expected, but this is redundant with the simpler `className` prop
*/
inputProps?: Partial<Omit<InputGroupProps2, "disabled" | "fill" | "value" | "onChange">>;
/** Custom renderer to transform an item into a string for the input value. */
inputValueRenderer: (item: T) => string;
/**
* The uncontrolled default selected item.
* This prop is ignored if `selectedItem` is used to control the state.
*/
defaultSelectedItem?: T;
/**
* The currently selected item, or `null` to indicate that no item is selected.
* If omitted or `undefined`, this prop will be uncontrolled (managed by the component's state).
* Use `onItemSelect` to listen for updates.
*/
selectedItem?: T | null;
/**
* HTML attributes to add to the `Menu` listbox containing the selectable options.
*/
menuProps?: React.HTMLAttributes<HTMLUListElement>;
/**
* If true, the component waits until a keydown event in the TagInput
* before opening its popover.
*
* If false, the popover opens immediately after a mouse click or TAB key
* interaction focuses the component's TagInput.
*
* @default false
*/
openOnKeyDown?: boolean;
/**
* Whether the active item should be reset to the first matching item _when
* the popover closes_. The query will also be reset to the empty string.
*
* @default false
*/
resetOnClose?: boolean;
}
export interface Suggest2State<T> {
isOpen: boolean;
selectedItem: T | null;
}
/**
* Suggest (v2) component.
*
* @see https://blueprintjs.com/docs/#select/suggest2
*/
export class Suggest2<T> extends AbstractPureComponent2<Suggest2Props<T>, Suggest2State<T>> {
public static displayName = `${DISPLAYNAME_PREFIX}.Suggest2`;
public static defaultProps: Partial<Suggest2Props<any>> = {
closeOnSelect: true,
fill: false,
openOnKeyDown: false,
resetOnClose: false,
};
/** @deprecated no longer necessary now that the TypeScript parser supports type arguments on JSX element tags */
public static ofType<U>() {
return Suggest2 as new (props: Suggest2Props<U>) => Suggest2<U>;
}
public state: Suggest2State<T> = {
isOpen: (this.props.popoverProps != null && this.props.popoverProps.isOpen) || false,
selectedItem: this.getInitialSelectedItem(),
};
public inputElement: HTMLInputElement | null = null;
private queryList: QueryList<T> | null = null;
private handleInputRef: React.Ref<HTMLInputElement> = refHandler(
this,
"inputElement",
this.props.inputProps?.inputRef,
);
private handleQueryListRef = (ref: QueryList<T> | null) => (this.queryList = ref);
private listboxId = Utils.uniqueId("listbox");
public render() {
// omit props specific to this component, spread the rest.
const { disabled, inputProps, menuProps, popoverProps, ...restProps } = this.props;
return (
<QueryList<T>
{...restProps}
menuProps={{ "aria-label": "selectable options", ...menuProps, id: this.listboxId }}
initialActiveItem={this.props.selectedItem ?? undefined}
onItemSelect={this.handleItemSelect}
ref={this.handleQueryListRef}
renderer={this.renderQueryList}
/>
);
}
public componentDidUpdate(prevProps: Suggest2Props<T>, prevState: Suggest2State<T>) {
if (prevProps.inputProps?.inputRef !== this.props.inputProps?.inputRef) {
setRef(prevProps.inputProps?.inputRef, null);
this.handleInputRef = refHandler(this, "inputElement", this.props.inputProps?.inputRef);
setRef(this.props.inputProps?.inputRef, this.inputElement);
}
// If the selected item prop changes, update the underlying state.
if (this.props.selectedItem !== undefined && this.props.selectedItem !== this.state.selectedItem) {
this.setState({ selectedItem: this.props.selectedItem });
}
if (this.state.isOpen === false && prevState.isOpen === true) {
// just closed, likely by keyboard interaction
// wait until the transition ends so there isn't a flash of content in the popover
/* eslint-disable-next-line deprecation/deprecation */
const timeout = this.props.popoverProps?.transitionDuration ?? Popover2.defaultProps.transitionDuration;
setTimeout(() => this.maybeResetActiveItemToSelectedItem(), timeout);
}
if (this.state.isOpen && !prevState.isOpen && this.queryList != null) {
this.queryList.scrollActiveItemIntoView();
}
}
private renderQueryList = (listProps: QueryListRendererProps<T>) => {
const { popoverContentProps = {}, popoverProps = {}, popoverRef } = this.props;
const { isOpen } = this.state;
const { handleKeyDown, handleKeyUp } = listProps;
// N.B. no need to set `popoverProps.fill` since that is unused with the `renderTarget` API
return (
<Popover2
autoFocus={false}
enforceFocus={false}
isOpen={isOpen}
placement={popoverProps.position || popoverProps.placement ? undefined : "bottom-start"}
{...popoverProps}
className={classNames(listProps.className, popoverProps.className)}
content={
<div {...popoverContentProps} onKeyDown={handleKeyDown} onKeyUp={handleKeyUp}>
{listProps.itemList}
</div>
}
interactionKind="click"
onInteraction={this.handlePopoverInteraction}
onOpened={this.handlePopoverOpened}
onOpening={this.handlePopoverOpening}
popoverClassName={classNames(Classes.SUGGEST_POPOVER, popoverProps.popoverClassName)}
popupKind={PopupKind.LISTBOX}
ref={popoverRef}
renderTarget={this.getPopoverTargetRenderer(listProps, isOpen)}
/>
);
};
// We use the renderTarget API to flatten the rendered DOM and make it easier to implement features like
// the "fill" prop. Note that we must take `isOpen` as an argument to force this render function to be called
// again after that state changes.
private getPopoverTargetRenderer =
(listProps: QueryListRendererProps<T>, isOpen: boolean) =>
// eslint-disable-next-line react/display-name
({
// pull out `isOpen` so that it's not forwarded to the DOM
isOpen: _isOpen,
// pull out `defaultValue` due to type incompatibility with InputGroup
defaultValue,
ref,
...targetProps
}: Popover2TargetProps & React.HTMLProps<HTMLInputElement>) => {
const { disabled, fill, inputProps = {}, inputValueRenderer, popoverProps = {}, resetOnClose } = this.props;
const { selectedItem } = this.state;
const { handleKeyDown, handleKeyUp } = listProps;
const selectedItemText = selectedItem == null ? "" : inputValueRenderer(selectedItem);
const { autoComplete = "off", placeholder = "Search..." } = inputProps;
// placeholder shows selected item while open.
const inputPlaceholder = isOpen && selectedItemText ? selectedItemText : placeholder;
// value shows query when open, and query remains when closed if nothing is selected.
// if resetOnClose is enabled, then hide query when not open. (see handlePopoverOpening)
const inputValue = isOpen ? listProps.query : selectedItemText ?? (resetOnClose ? "" : listProps.query);
return (
<InputGroup
aria-controls={this.listboxId}
autoComplete={autoComplete}
disabled={disabled}
tagName={popoverProps.targetTagName}
{...targetProps}
{...inputProps}
aria-autocomplete="list"
aria-expanded={isOpen}
className={classNames(targetProps.className, inputProps.className)}
fill={fill}
inputRef={mergeRefs(this.handleInputRef, ref)}
onChange={listProps.handleQueryChange}
onFocus={this.handleInputFocus}
onKeyDown={this.getTargetKeyDownHandler(handleKeyDown)}
onKeyUp={this.getTargetKeyUpHandler(handleKeyUp)}
placeholder={inputPlaceholder}
role="combobox"
value={inputValue}
/>
);
};
private selectText = () => {
// wait until the input is properly focused to select the text inside of it
this.requestAnimationFrame(() => {
this.inputElement?.setSelectionRange(0, this.inputElement.value.length);
});
};
private handleInputFocus = (event: React.FocusEvent<HTMLInputElement>) => {
this.selectText();
// TODO can we leverage Popover2.openOnTargetFocus for this?
if (!this.props.openOnKeyDown) {
this.setState({ isOpen: true });
}
this.props.inputProps?.onFocus?.(event);
};
private handleItemSelect = (item: T, event?: React.SyntheticEvent<HTMLElement>) => {
let nextOpenState: boolean;
if (!this.props.closeOnSelect) {
this.inputElement?.focus();
this.selectText();
nextOpenState = true;
} else {
this.inputElement?.blur();
nextOpenState = false;
}
// the internal state should only change when uncontrolled.
if (this.props.selectedItem === undefined) {
this.setState({
isOpen: nextOpenState,
selectedItem: item,
});
} else {
// otherwise just set the next open state.
this.setState({ isOpen: nextOpenState });
}
this.props.onItemSelect?.(item, event);
};
private getInitialSelectedItem(): T | null {
// controlled > uncontrolled > default
if (this.props.selectedItem !== undefined) {
return this.props.selectedItem;
} else if (this.props.defaultSelectedItem !== undefined) {
return this.props.defaultSelectedItem;
} else {
return null;
}
}
// Popover2 interaction kind is CLICK, so this only handles click events.
// Note that we defer to the next animation frame in order to get the latest activeElement
private handlePopoverInteraction = (nextOpenState: boolean, event?: React.SyntheticEvent<HTMLElement>) =>
this.requestAnimationFrame(() => {
const isInputFocused = this.inputElement === Utils.getActiveElement(this.inputElement);
if (this.inputElement != null && !isInputFocused) {
// the input is no longer focused, we should close the popover
this.setState({ isOpen: false });
}
this.props.popoverProps?.onInteraction?.(nextOpenState, event);
});
private handlePopoverOpening = (node: HTMLElement) => {
// reset query before opening instead of when closing to prevent flash of unfiltered items.
// this is a limitation of the interactions between QueryList state and Popover2 transitions.
if (this.props.resetOnClose && this.queryList) {
this.queryList.setQuery("", true);
}
this.props.popoverProps?.onOpening?.(node);
};
private handlePopoverOpened = (node: HTMLElement) => {
// scroll active item into view after popover transition completes and all dimensions are stable.
if (this.queryList != null) {
this.queryList.scrollActiveItemIntoView();
}
this.props.popoverProps?.onOpened?.(node);
};
private getTargetKeyDownHandler = (
handleQueryListKeyDown: React.EventHandler<React.KeyboardEvent<HTMLElement>>,
) => {
return (evt: React.KeyboardEvent<HTMLInputElement>) => {
// HACKHACK: https://github.com/palantir/blueprint/issues/4165
// eslint-disable-next-line deprecation/deprecation
const { which } = evt;
if (which === Keys.ESCAPE || which === Keys.TAB) {
this.inputElement?.blur();
this.setState({ isOpen: false });
} else if (
this.props.openOnKeyDown &&
which !== Keys.BACKSPACE &&
which !== Keys.ARROW_LEFT &&
which !== Keys.ARROW_RIGHT
) {
this.setState({ isOpen: true });
}
if (this.state.isOpen) {
handleQueryListKeyDown?.(evt);
}
this.props.inputProps?.onKeyDown?.(evt);
};
};
private getTargetKeyUpHandler = (handleQueryListKeyUp: React.EventHandler<React.KeyboardEvent<HTMLElement>>) => {
return (evt: React.KeyboardEvent<HTMLInputElement>) => {
if (this.state.isOpen) {
handleQueryListKeyUp?.(evt);
}
this.props.inputProps?.onKeyUp?.(evt);
};
};
private maybeResetActiveItemToSelectedItem() {
const shouldResetActiveItemToSelectedItem =
this.props.activeItem === undefined && this.state.selectedItem !== null && !this.props.resetOnSelect;
if (this.queryList !== null && shouldResetActiveItemToSelectedItem) {
this.queryList.setActiveItem(this.props.selectedItem ?? this.state.selectedItem);
}
}
}