UNPKG

15 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 { AbstractPureComponent2, Button, Classes as CoreClasses, DISPLAYNAME_PREFIX, Keys, mergeRefs, refHandler, setRef, TagInput, Utils, } from "@blueprintjs/core";
19import { Popover2, PopupKind } from "@blueprintjs/popover2";
20import { Classes } from "../../common";
21import { QueryList } from "../query-list/queryList";
22/**
23 * Multi select (v2) component.
24 *
25 * @see https://blueprintjs.com/docs/#select/multi-select2
26 */
27var MultiSelect2 = /** @class */ (function (_super) {
28 __extends(MultiSelect2, _super);
29 function MultiSelect2() {
30 var _this = this;
31 var _a;
32 _this = _super.apply(this, arguments) || this;
33 _this.listboxId = Utils.uniqueId("listbox");
34 _this.state = {
35 isOpen: (_this.props.popoverProps && _this.props.popoverProps.isOpen) || false,
36 };
37 _this.input = null;
38 _this.queryList = null;
39 _this.refHandlers = {
40 input: refHandler(_this, "input", (_a = _this.props.tagInputProps) === null || _a === void 0 ? void 0 : _a.inputRef),
41 popover: React.createRef(),
42 queryList: function (ref) { return (_this.queryList = ref); },
43 };
44 _this.renderQueryList = function (listProps) {
45 var _a = _this.props, disabled = _a.disabled, _b = _a.popoverContentProps, popoverContentProps = _b === void 0 ? {} : _b, _c = _a.popoverProps, popoverProps = _c === void 0 ? {} : _c;
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(Popover2, __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({}, popoverContentProps, { onKeyDown: handleKeyDown, onKeyUp: handleKeyUp }), 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) })));
52 };
53 // We use the renderTarget API to flatten the rendered DOM and make it easier to implement features like
54 // the "fill" prop. Note that we must take `isOpen` as an argument to force this render function to be called
55 // again after that state changes.
56 _this.getPopoverTargetRenderer = function (listProps, isOpen) {
57 // N.B. pull out `isOpen` so that it's not forwarded to the DOM, but remember not to use it directly
58 // since it may be stale (`renderTarget` is not re-invoked on this.state changes).
59 // eslint-disable-next-line react/display-name
60 return function (_a) {
61 var _b;
62 var _c;
63 var _isOpen = _a.isOpen, ref = _a.ref, targetProps = __rest(_a, ["isOpen", "ref"]);
64 var _d = _this.props, disabled = _d.disabled, fill = _d.fill, onClear = _d.onClear, placeholder = _d.placeholder, _e = _d.popoverProps, popoverProps = _e === void 0 ? {} : _e, _f = _d.popoverTargetProps, popoverTargetProps = _f === void 0 ? {} : _f, selectedItems = _d.selectedItems, _g = _d.tagInputProps, tagInputProps = _g === void 0 ? {} : _g;
65 var handleKeyDown = listProps.handleKeyDown, handleKeyUp = listProps.handleKeyUp;
66 if (disabled) {
67 tagInputProps.disabled = true;
68 }
69 if (fill) {
70 tagInputProps.fill = true;
71 }
72 // add our own inputProps.className so that we can reference it in event handlers
73 var inputProps = __assign(__assign({}, tagInputProps.inputProps), { className: classNames((_c = tagInputProps.inputProps) === null || _c === void 0 ? void 0 : _c.className, Classes.MULTISELECT_TAG_INPUT_INPUT) });
74 var maybeClearButton = onClear !== undefined && selectedItems.length > 0 ? (
75 // use both aria-label and title a11y attributes here, for screen readers
76 // and mouseover interactions respectively
77 React.createElement(Button, { "aria-label": "Clear selected items", disabled: disabled, icon: "cross", minimal: true, onClick: _this.handleClearButtonClick, title: "Clear selected items" })) : undefined;
78 var _h = popoverProps.targetTagName, targetTagName = _h === void 0 ? "div" : _h;
79 return React.createElement(targetTagName, __assign(__assign(__assign({ "aria-autocomplete": "list", "aria-controls": _this.listboxId }, popoverTargetProps), targetProps), { "aria-expanded": isOpen,
80 // Note that we must set FILL here in addition to TagInput to get the wrapper element to full width
81 className: classNames(targetProps.className, popoverTargetProps.className, (_b = {},
82 _b[CoreClasses.FILL] = fill,
83 _b)),
84 // Normally, Popover2 would also need to attach its own `onKeyDown` handler via `targetProps`,
85 // but in our case we fully manage that interaction and listen for key events to open/close
86 // the popover, so we elide it from the DOM.
87 onKeyDown: _this.getTagInputKeyDownHandler(handleKeyDown), onKeyUp: _this.getTagInputKeyUpHandler(handleKeyUp), ref: ref, role: "combobox" }), React.createElement(TagInput, __assign({ placeholder: placeholder, rightElement: maybeClearButton }, tagInputProps, { className: classNames(Classes.MULTISELECT, tagInputProps.className), 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) })));
88 };
89 };
90 _this.handleItemSelect = function (item, evt) {
91 var _a, _b, _c;
92 if (_this.input != null) {
93 _this.input.focus();
94 }
95 (_b = (_a = _this.props).onItemSelect) === null || _b === void 0 ? void 0 : _b.call(_a, item, evt);
96 (_c = _this.refHandlers.popover.current) === null || _c === void 0 ? void 0 : _c.reposition(); // reposition when size of input changes
97 };
98 _this.handleQueryChange = function (query, evt) {
99 var _a, _b;
100 _this.setState({ isOpen: query.length > 0 || !_this.props.openOnKeyDown });
101 (_b = (_a = _this.props).onQueryChange) === null || _b === void 0 ? void 0 : _b.call(_a, query, evt);
102 };
103 // Popover interaction kind is CLICK, so this only handles click events.
104 // Note that we defer to the next animation frame in order to get the latest activeElement
105 _this.handlePopoverInteraction = function (nextOpenState, evt) {
106 return _this.requestAnimationFrame(function () {
107 var _a, _b;
108 var isInputFocused = _this.input === Utils.getActiveElement(_this.input);
109 if (_this.input != null && !isInputFocused) {
110 // input is no longer focused, we should close the popover
111 _this.setState({ isOpen: false });
112 }
113 else if (!_this.props.openOnKeyDown) {
114 // we should open immediately on click focus events
115 _this.setState({ isOpen: true });
116 }
117 (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onInteraction) === null || _b === void 0 ? void 0 : _b.call(_a, nextOpenState, evt);
118 });
119 };
120 _this.handlePopoverOpened = function (node) {
121 var _a, _b;
122 if (_this.queryList != null) {
123 // scroll active item into view after popover transition completes and all dimensions are stable.
124 _this.queryList.scrollActiveItemIntoView();
125 }
126 (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onOpened) === null || _b === void 0 ? void 0 : _b.call(_a, node);
127 };
128 _this.handleTagRemove = function (tag, index) {
129 var _a, _b;
130 var _c = _this.props, selectedItems = _c.selectedItems, onRemove = _c.onRemove, tagInputProps = _c.tagInputProps;
131 onRemove === null || onRemove === void 0 ? void 0 : onRemove(selectedItems[index], index);
132 (_a = tagInputProps === null || tagInputProps === void 0 ? void 0 : tagInputProps.onRemove) === null || _a === void 0 ? void 0 : _a.call(tagInputProps, tag, index);
133 (_b = _this.refHandlers.popover.current) === null || _b === void 0 ? void 0 : _b.reposition(); // reposition when size of input changes
134 };
135 _this.getTagInputAddHandler = function (listProps) { return function (values, method) {
136 if (method === "paste") {
137 listProps.handlePaste(values);
138 }
139 }; };
140 _this.getTagInputKeyDownHandler = function (handleQueryListKeyDown) {
141 return function (e) {
142 var _a, _b;
143 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
144 // eslint-disable-next-line deprecation/deprecation
145 var which = e.which;
146 if (which === Keys.ESCAPE || which === Keys.TAB) {
147 // By default the escape key will not trigger a blur on the
148 // input element. It must be done explicitly.
149 if (_this.input != null) {
150 _this.input.blur();
151 }
152 _this.setState({ isOpen: false });
153 }
154 else if (!(which === Keys.BACKSPACE || which === Keys.ARROW_LEFT || which === Keys.ARROW_RIGHT)) {
155 _this.setState({ isOpen: true });
156 }
157 var isTargetingTagRemoveButton = e.target.closest(".".concat(CoreClasses.TAG_REMOVE)) != null;
158 if (_this.state.isOpen && !isTargetingTagRemoveButton) {
159 handleQueryListKeyDown === null || handleQueryListKeyDown === void 0 ? void 0 : handleQueryListKeyDown(e);
160 }
161 (_b = (_a = _this.props.popoverTargetProps) === null || _a === void 0 ? void 0 : _a.onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
162 };
163 };
164 _this.getTagInputKeyUpHandler = function (handleQueryListKeyUp) {
165 return function (e) {
166 var _a, _b;
167 var isTargetingInput = e.target.classList.contains(Classes.MULTISELECT_TAG_INPUT_INPUT);
168 // only handle events when the focus is on the actual <input> inside the TagInput, as that's
169 // what QueryList is designed to do
170 if (_this.state.isOpen && isTargetingInput) {
171 handleQueryListKeyUp === null || handleQueryListKeyUp === void 0 ? void 0 : handleQueryListKeyUp(e);
172 }
173 (_b = (_a = _this.props.popoverTargetProps) === null || _a === void 0 ? void 0 : _a.onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
174 };
175 };
176 _this.handleClearButtonClick = function () {
177 var _a, _b, _c;
178 (_b = (_a = _this.props).onClear) === null || _b === void 0 ? void 0 : _b.call(_a);
179 (_c = _this.refHandlers.popover.current) === null || _c === void 0 ? void 0 : _c.reposition(); // reposition when size of input changes
180 };
181 return _this;
182 }
183 /** @deprecated no longer necessary now that the TypeScript parser supports type arguments on JSX element tags */
184 MultiSelect2.ofType = function () {
185 return MultiSelect2;
186 };
187 MultiSelect2.prototype.componentDidUpdate = function (prevProps) {
188 var _a, _b, _c, _d, _e;
189 if (((_a = prevProps.tagInputProps) === null || _a === void 0 ? void 0 : _a.inputRef) !== ((_b = this.props.tagInputProps) === null || _b === void 0 ? void 0 : _b.inputRef)) {
190 setRef((_c = prevProps.tagInputProps) === null || _c === void 0 ? void 0 : _c.inputRef, null);
191 this.refHandlers.input = refHandler(this, "input", (_d = this.props.tagInputProps) === null || _d === void 0 ? void 0 : _d.inputRef);
192 setRef((_e = this.props.tagInputProps) === null || _e === void 0 ? void 0 : _e.inputRef, this.input);
193 }
194 if ((prevProps.onClear === undefined && this.props.onClear !== undefined) ||
195 (prevProps.onClear !== undefined && this.props.onClear === undefined)) {
196 this.forceUpdate();
197 }
198 };
199 MultiSelect2.prototype.render = function () {
200 // omit props specific to this component, spread the rest.
201 var _a = this.props, menuProps = _a.menuProps, openOnKeyDown = _a.openOnKeyDown, popoverProps = _a.popoverProps, tagInputProps = _a.tagInputProps, restProps = __rest(_a, ["menuProps", "openOnKeyDown", "popoverProps", "tagInputProps"]);
202 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 })));
203 };
204 MultiSelect2.displayName = "".concat(DISPLAYNAME_PREFIX, ".MultiSelect2");
205 MultiSelect2.defaultProps = {
206 disabled: false,
207 fill: false,
208 placeholder: "Search...",
209 };
210 return MultiSelect2;
211}(AbstractPureComponent2));
212export { MultiSelect2 };
213//# sourceMappingURL=multiSelect2.js.map
\No newline at end of file