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 { AbstractPureComponent2, Button, Classes as CoreClasses, DISPLAYNAME_PREFIX, Keys, mergeRefs, refHandler, setRef, TagInput, Utils, } from "@blueprintjs/core";
|
19 | import { Popover2, PopupKind } from "@blueprintjs/popover2";
|
20 | import { Classes } from "../../common";
|
21 | import { QueryList } from "../query-list/queryList";
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var MultiSelect2 = (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 |
|
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 |
|
54 |
|
55 |
|
56 | _this.getPopoverTargetRenderer = function (listProps, isOpen) {
|
57 |
|
58 |
|
59 |
|
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 |
|
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 |
|
76 |
|
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 |
|
81 | className: classNames(targetProps.className, popoverTargetProps.className, (_b = {},
|
82 | _b[CoreClasses.FILL] = fill,
|
83 | _b)),
|
84 |
|
85 |
|
86 |
|
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();
|
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 |
|
104 |
|
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 |
|
111 | _this.setState({ isOpen: false });
|
112 | }
|
113 | else if (!_this.props.openOnKeyDown) {
|
114 |
|
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 |
|
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();
|
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 |
|
144 |
|
145 | var which = e.which;
|
146 | if (which === Keys.ESCAPE || which === Keys.TAB) {
|
147 |
|
148 |
|
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 |
|
169 |
|
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();
|
180 | };
|
181 | return _this;
|
182 | }
|
183 |
|
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 |
|
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));
|
212 | export { MultiSelect2 };
|
213 |
|
\ | No newline at end of file |