1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import { __assign, __extends, __rest } from "tslib";
|
16 | import classNames from "classnames";
|
17 | import * as React from "react";
|
18 | import { AbstractPureComponent, Button, Classes as CoreClasses, DISPLAYNAME_PREFIX, mergeRefs, Popover, PopupKind, refHandler, setRef, TagInput, Utils, } from "@blueprintjs/core";
|
19 | import { Cross } from "@blueprintjs/icons";
|
20 | import { Classes } from "../../common";
|
21 | import { QueryList } from "../query-list/queryList";
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var MultiSelect = (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 |
|
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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
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 |
|
65 |
|
66 |
|
67 | _this.getPopoverTargetRenderer = function (listProps, isOpen) {
|
68 |
|
69 |
|
70 |
|
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 |
|
79 | className: classNames(targetProps.className, popoverTargetProps.className, (_b = {},
|
80 | _b[CoreClasses.FILL] = fill,
|
81 | _b)),
|
82 |
|
83 |
|
84 |
|
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 |
|
95 |
|
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 |
|
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();
|
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 |
|
115 |
|
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 |
|
128 | _this.setState({ isOpen: false });
|
129 | }
|
130 | else if (!_this.props.openOnKeyDown) {
|
131 |
|
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 |
|
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();
|
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 |
|
169 |
|
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 |
|
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 |
|
202 |
|
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();
|
213 | };
|
214 | return _this;
|
215 | }
|
216 |
|
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 |
|
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));
|
245 | export { MultiSelect };
|
246 |
|
\ | No newline at end of file |