UNPKG

17.5 kBJavaScriptView Raw
1/*
2 * Copyright 2022 Palantir Technologies, Inc. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { __assign, __extends, __rest } from "tslib";
16import classNames from "classnames";
17import * as React from "react";
18import { AbstractPureComponent, Button, Classes as CoreClasses, DISPLAYNAME_PREFIX, mergeRefs, Popover, PopupKind, refHandler, setRef, TagInput, Utils, } from "@blueprintjs/core";
19import { Cross } from "@blueprintjs/icons";
20import { Classes } from "../../common";
21import { QueryList } from "../query-list/queryList";
22/**
23 * Multi select component.
24 *
25 * @see https://blueprintjs.com/docs/#select/multi-select
26 */
27var MultiSelect = /** @class */ (function (_super) {
28 __extends(MultiSelect, _super);
29 function MultiSelect() {
30 var _a;
31 var _this = _super.apply(this, arguments) || this;
32 _this.listboxId = Utils.uniqueId("listbox");
33 _this.state = {
34 isOpen: (_this.props.popoverProps && _this.props.popoverProps.isOpen) || false,
35 };
36 _this.input = null;
37 _this.queryList = null;
38 _this.refHandlers = {
39 input: refHandler(_this, "input", (_a = _this.props.tagInputProps) === null || _a === void 0 ? void 0 : _a.inputRef),
40 popover: React.createRef(),
41 queryList: function (ref) { return (_this.queryList = ref); },
42 };
43 _this.renderQueryList = function (listProps) {
44 var _a;
45 var _b = _this.props, disabled = _b.disabled, _c = _b.popoverContentProps, popoverContentProps = _c === void 0 ? {} : _c, _d = _b.popoverProps, popoverProps = _d === void 0 ? {} : _d;
46 var handleKeyDown = listProps.handleKeyDown, handleKeyUp = listProps.handleKeyUp;
47 var popoverRef = _this.props.popoverRef === undefined
48 ? _this.refHandlers.popover
49 : mergeRefs(_this.refHandlers.popover, _this.props.popoverRef);
50 // N.B. no need to set `popoverProps.fill` since that is unused with the `renderTarget` API
51 return (React.createElement(Popover, __assign({ autoFocus: false, canEscapeKeyClose: true, disabled: disabled, enforceFocus: false, isOpen: _this.state.isOpen, placement: popoverProps.position || popoverProps.placement ? undefined : "bottom-start" }, popoverProps, { className: classNames(listProps.className, popoverProps.className), content: React.createElement("div", __assign({
52 // In the case where customTarget is supplied and the TagInput is rendered within the Popover,
53 // without matchTargetWidth there is no width defined in any of TagInput's
54 // grandparents when it's rendered through usePortal, so it will never flex-wrap
55 // and infinitely grow horizontally. To address this, if there is no width guidance
56 // from matchTargetWidth, explicitly set a default width to so Tags will flex-wrap.
57 className: _this.props.customTarget != null && !((_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.matchTargetWidth)
58 ? Classes.MULTISELECT_POPOVER_DEFAULT_WIDTH
59 : undefined }, popoverContentProps, { onKeyDown: handleKeyDown, onKeyUp: handleKeyUp }),
60 _this.props.customTarget != null &&
61 _this.getTagInput(listProps, classNames(CoreClasses.FILL, Classes.MULTISELECT_POPOVER_TAG_INPUT_MARGIN)),
62 listProps.itemList), interactionKind: "click", onInteraction: _this.handlePopoverInteraction, onOpened: _this.handlePopoverOpened, popoverClassName: classNames(Classes.MULTISELECT_POPOVER, popoverProps.popoverClassName), popupKind: PopupKind.LISTBOX, ref: popoverRef, renderTarget: _this.getPopoverTargetRenderer(listProps, _this.state.isOpen) })));
63 };
64 // We use the renderTarget API to flatten the rendered DOM and make it easier to implement features like
65 // the "fill" prop. Note that we must take `isOpen` as an argument to force this render function to be called
66 // again after that state changes.
67 _this.getPopoverTargetRenderer = function (listProps, isOpen) {
68 // N.B. pull out `isOpen` so that it's not forwarded to the DOM, but remember not to use it directly
69 // since it may be stale (`renderTarget` is not re-invoked on this.state changes).
70 // eslint-disable-next-line react/display-name
71 return function (_a) {
72 var _b;
73 var _isOpen = _a.isOpen, ref = _a.ref, targetProps = __rest(_a, ["isOpen", "ref"]);
74 var _c = _this.props, disabled = _c.disabled, fill = _c.fill, selectedItems = _c.selectedItems, _d = _c.popoverProps, popoverProps = _d === void 0 ? {} : _d, _e = _c.popoverTargetProps, popoverTargetProps = _e === void 0 ? {} : _e;
75 var handleKeyDown = listProps.handleKeyDown, handleKeyUp = listProps.handleKeyUp;
76 var _f = popoverProps.targetTagName, targetTagName = _f === void 0 ? "div" : _f;
77 return React.createElement(targetTagName, __assign(__assign(__assign({ "aria-autocomplete": "list", "aria-controls": _this.listboxId }, popoverTargetProps), targetProps), { "aria-disabled": disabled, "aria-expanded": isOpen,
78 // Note that we must set FILL here in addition to TagInput to get the wrapper element to full width
79 className: classNames(targetProps.className, popoverTargetProps.className, (_b = {},
80 _b[CoreClasses.FILL] = fill,
81 _b)),
82 // Normally, Popover would also need to attach its own `onKeyDown` handler via `targetProps`,
83 // but in our case we fully manage that interaction and listen for key events to open/close
84 // the popover, so we elide it from the DOM.
85 onKeyDown: _this.getTagInputKeyDownHandler(handleKeyDown), onKeyUp: _this.getTagInputKeyUpHandler(handleKeyUp), ref: ref, role: "combobox" }), _this.props.customTarget != null
86 ? _this.props.customTarget(selectedItems, isOpen)
87 : _this.getTagInput(listProps));
88 };
89 };
90 _this.getTagInput = function (listProps, className) {
91 var _a;
92 var _b = _this.props, disabled = _b.disabled, fill = _b.fill, onClear = _b.onClear, placeholder = _b.placeholder, selectedItems = _b.selectedItems, _c = _b.tagInputProps, tagInputProps = _c === void 0 ? {} : _c;
93 var maybeClearButton = onClear !== undefined && selectedItems.length > 0 ? (
94 // use both aria-label and title a11y attributes here, for screen readers
95 // and mouseover interactions respectively
96 React.createElement(Button, { "aria-label": "Clear selected items", disabled: disabled, icon: React.createElement(Cross, null), minimal: true, onClick: _this.handleClearButtonClick, title: "Clear selected items" })) : undefined;
97 // add our own inputProps.className so that we can reference it in event handlers
98 var inputProps = __assign(__assign({}, tagInputProps.inputProps), { className: classNames((_a = tagInputProps.inputProps) === null || _a === void 0 ? void 0 : _a.className, Classes.MULTISELECT_TAG_INPUT_INPUT) });
99 return (React.createElement(TagInput, __assign({ placeholder: placeholder, rightElement: maybeClearButton }, tagInputProps, { className: classNames(className, Classes.MULTISELECT, tagInputProps.className), disabled: disabled, fill: fill, inputRef: _this.refHandlers.input, inputProps: inputProps, inputValue: listProps.query, onAdd: _this.getTagInputAddHandler(listProps), onInputChange: listProps.handleQueryChange, onRemove: _this.handleTagRemove, values: selectedItems.map(_this.props.tagRenderer) })));
100 };
101 _this.handleItemSelect = function (item, evt) {
102 var _a, _b, _c;
103 if (_this.input != null) {
104 _this.input.focus();
105 }
106 (_b = (_a = _this.props).onItemSelect) === null || _b === void 0 ? void 0 : _b.call(_a, item, evt);
107 (_c = _this.refHandlers.popover.current) === null || _c === void 0 ? void 0 : _c.reposition(); // reposition when size of input changes
108 };
109 _this.handleQueryChange = function (query, evt) {
110 var _a, _b;
111 _this.setState({ isOpen: query.length > 0 || (_this.props.customTarget == null && !_this.props.openOnKeyDown) });
112 (_b = (_a = _this.props).onQueryChange) === null || _b === void 0 ? void 0 : _b.call(_a, query, evt);
113 };
114 // Popover interaction kind is CLICK, so this only handles click events.
115 // Note that we defer to the next animation frame in order to get the latest activeElement
116 _this.handlePopoverInteraction = function (nextOpenState, evt) {
117 var _a, _b;
118 if (_this.props.customTarget != null) {
119 _this.setState({ isOpen: nextOpenState });
120 (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onInteraction) === null || _b === void 0 ? void 0 : _b.call(_a, nextOpenState, evt);
121 return;
122 }
123 _this.requestAnimationFrame(function () {
124 var _a, _b;
125 var isInputFocused = _this.input === Utils.getActiveElement(_this.input);
126 if (_this.input != null && !isInputFocused) {
127 // input is no longer focused, we should close the popover
128 _this.setState({ isOpen: false });
129 }
130 else if (!_this.props.openOnKeyDown) {
131 // we should open immediately on click focus events
132 _this.setState({ isOpen: true });
133 }
134 (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onInteraction) === null || _b === void 0 ? void 0 : _b.call(_a, nextOpenState, evt);
135 });
136 };
137 _this.handlePopoverOpened = function (node) {
138 var _a, _b, _c, _d;
139 if (_this.queryList != null) {
140 // scroll active item into view after popover transition completes and all dimensions are stable.
141 _this.queryList.scrollActiveItemIntoView();
142 }
143 var hasCustomTarget = _this.props.customTarget != null;
144 if (hasCustomTarget && _this.input != null) {
145 var shouldAutofocus = ((_b = (_a = _this.props.tagInputProps) === null || _a === void 0 ? void 0 : _a.inputProps) === null || _b === void 0 ? void 0 : _b.autoFocus) !== false;
146 if (shouldAutofocus) {
147 _this.input.focus();
148 }
149 }
150 (_d = (_c = _this.props.popoverProps) === null || _c === void 0 ? void 0 : _c.onOpened) === null || _d === void 0 ? void 0 : _d.call(_c, node);
151 };
152 _this.handleTagRemove = function (tag, index) {
153 var _a, _b;
154 var _c = _this.props, selectedItems = _c.selectedItems, onRemove = _c.onRemove, tagInputProps = _c.tagInputProps;
155 onRemove === null || onRemove === void 0 ? void 0 : onRemove(selectedItems[index], index);
156 (_a = tagInputProps === null || tagInputProps === void 0 ? void 0 : tagInputProps.onRemove) === null || _a === void 0 ? void 0 : _a.call(tagInputProps, tag, index);
157 (_b = _this.refHandlers.popover.current) === null || _b === void 0 ? void 0 : _b.reposition(); // reposition when size of input changes
158 };
159 _this.getTagInputAddHandler = function (listProps) { return function (values, method) {
160 if (method === "paste") {
161 listProps.handlePaste(values);
162 }
163 }; };
164 _this.getTagInputKeyDownHandler = function (handleQueryListKeyDown) {
165 return function (e) {
166 var _a, _b;
167 if (e.key === "Escape" || e.key === "Tab") {
168 // By default the escape key will not trigger a blur on the
169 // input element. It must be done explicitly.
170 if (_this.input != null) {
171 _this.input.blur();
172 }
173 _this.setState({ isOpen: false });
174 }
175 else if (!(e.key === "Backspace" || e.key === "ArrowLeft" || e.key === "ArrowRight")) {
176 // Custom target might not be an input, so certain keystrokes might have other effects (space pushing the scrollview down)
177 if (_this.props.customTarget != null) {
178 if (e.key === " ") {
179 e.preventDefault();
180 _this.setState({ isOpen: true });
181 }
182 else if (e.key === "Enter") {
183 _this.setState({ isOpen: true });
184 }
185 }
186 else {
187 _this.setState({ isOpen: true });
188 }
189 }
190 var isTargetingTagRemoveButton = e.target.closest(".".concat(CoreClasses.TAG_REMOVE)) != null;
191 if (_this.state.isOpen && !isTargetingTagRemoveButton) {
192 handleQueryListKeyDown === null || handleQueryListKeyDown === void 0 ? void 0 : handleQueryListKeyDown(e);
193 }
194 (_b = (_a = _this.props.popoverTargetProps) === null || _a === void 0 ? void 0 : _a.onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
195 };
196 };
197 _this.getTagInputKeyUpHandler = function (handleQueryListKeyUp) {
198 return function (e) {
199 var _a, _b;
200 var isTargetingInput = e.target.classList.contains(Classes.MULTISELECT_TAG_INPUT_INPUT);
201 // only handle events when the focus is on the actual <input> inside the TagInput, as that's
202 // what QueryList is designed to do
203 if (_this.state.isOpen && isTargetingInput) {
204 handleQueryListKeyUp === null || handleQueryListKeyUp === void 0 ? void 0 : handleQueryListKeyUp(e);
205 }
206 (_b = (_a = _this.props.popoverTargetProps) === null || _a === void 0 ? void 0 : _a.onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
207 };
208 };
209 _this.handleClearButtonClick = function () {
210 var _a, _b, _c;
211 (_b = (_a = _this.props).onClear) === null || _b === void 0 ? void 0 : _b.call(_a);
212 (_c = _this.refHandlers.popover.current) === null || _c === void 0 ? void 0 : _c.reposition(); // reposition when size of input changes
213 };
214 return _this;
215 }
216 /** @deprecated no longer necessary now that the TypeScript parser supports type arguments on JSX element tags */
217 MultiSelect.ofType = function () {
218 return MultiSelect;
219 };
220 MultiSelect.prototype.componentDidUpdate = function (prevProps) {
221 var _a, _b, _c, _d, _e;
222 if (((_a = prevProps.tagInputProps) === null || _a === void 0 ? void 0 : _a.inputRef) !== ((_b = this.props.tagInputProps) === null || _b === void 0 ? void 0 : _b.inputRef)) {
223 setRef((_c = prevProps.tagInputProps) === null || _c === void 0 ? void 0 : _c.inputRef, null);
224 this.refHandlers.input = refHandler(this, "input", (_d = this.props.tagInputProps) === null || _d === void 0 ? void 0 : _d.inputRef);
225 setRef((_e = this.props.tagInputProps) === null || _e === void 0 ? void 0 : _e.inputRef, this.input);
226 }
227 if ((prevProps.onClear === undefined && this.props.onClear !== undefined) ||
228 (prevProps.onClear !== undefined && this.props.onClear === undefined)) {
229 this.forceUpdate();
230 }
231 };
232 MultiSelect.prototype.render = function () {
233 // omit props specific to this component, spread the rest.
234 var _a = this.props, menuProps = _a.menuProps, openOnKeyDown = _a.openOnKeyDown, popoverProps = _a.popoverProps, tagInputProps = _a.tagInputProps, customTarget = _a.customTarget, restProps = __rest(_a, ["menuProps", "openOnKeyDown", "popoverProps", "tagInputProps", "customTarget"]);
235 return (React.createElement(QueryList, __assign({}, restProps, { menuProps: __assign(__assign({ "aria-label": "selectable options" }, menuProps), { "aria-multiselectable": true, id: this.listboxId }), onItemSelect: this.handleItemSelect, onQueryChange: this.handleQueryChange, ref: this.refHandlers.queryList, renderer: this.renderQueryList })));
236 };
237 MultiSelect.displayName = "".concat(DISPLAYNAME_PREFIX, ".MultiSelect");
238 MultiSelect.defaultProps = {
239 disabled: false,
240 fill: false,
241 placeholder: "Search...",
242 };
243 return MultiSelect;
244}(AbstractPureComponent));
245export { MultiSelect };
246//# sourceMappingURL=multiSelect.js.map
\No newline at end of file