1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __extends, __rest } from "tslib";
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | import classNames from "classnames";
|
23 | import * as React from "react";
|
24 | import { AbstractPureComponent2, DISPLAYNAME_PREFIX, InputGroup, Keys, Popover, PopoverInteractionKind, Position, refHandler, setRef, } from "@blueprintjs/core";
|
25 | import { Classes } from "../../common";
|
26 | import { QueryList } from "../query-list/queryList";
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var Suggest = (function (_super) {
|
34 | __extends(Suggest, _super);
|
35 | function Suggest() {
|
36 | var _this = this;
|
37 | var _a;
|
38 | _this = _super.apply(this, arguments) || this;
|
39 | _this.state = {
|
40 | isOpen: (_this.props.popoverProps != null && _this.props.popoverProps.isOpen) || false,
|
41 | selectedItem: _this.getInitialSelectedItem(),
|
42 | };
|
43 | _this.inputElement = null;
|
44 | _this.queryList = null;
|
45 | _this.handleInputRef = refHandler(_this, "inputElement", (_a = _this.props.inputProps) === null || _a === void 0 ? void 0 : _a.inputRef);
|
46 | _this.handleQueryListRef = function (ref) { return (_this.queryList = ref); };
|
47 | _this.renderQueryList = function (listProps) {
|
48 | var _a = _this.props, fill = _a.fill, _b = _a.inputProps, inputProps = _b === void 0 ? {} : _b, _c = _a.popoverProps, popoverProps = _c === void 0 ? {} : _c;
|
49 | var _d = _this.state, isOpen = _d.isOpen, selectedItem = _d.selectedItem;
|
50 | var handleKeyDown = listProps.handleKeyDown, handleKeyUp = listProps.handleKeyUp;
|
51 | var _e = inputProps.autoComplete, autoComplete = _e === void 0 ? "off" : _e, _f = inputProps.placeholder, placeholder = _f === void 0 ? "Search..." : _f;
|
52 | var selectedItemText = selectedItem ? _this.props.inputValueRenderer(selectedItem) : "";
|
53 |
|
54 | var inputPlaceholder = isOpen && selectedItemText ? selectedItemText : placeholder;
|
55 |
|
56 |
|
57 | var inputValue = isOpen
|
58 | ? listProps.query
|
59 | : selectedItemText || (_this.props.resetOnClose ? "" : listProps.query);
|
60 | if (fill) {
|
61 | popoverProps.fill = true;
|
62 | inputProps.fill = true;
|
63 | }
|
64 | return (React.createElement(Popover, __assign({ autoFocus: false, enforceFocus: false, isOpen: isOpen, position: Position.BOTTOM_LEFT }, popoverProps, { className: classNames(listProps.className, popoverProps.className), interactionKind: PopoverInteractionKind.CLICK, onInteraction: _this.handlePopoverInteraction, popoverClassName: classNames(Classes.SELECT_POPOVER, popoverProps.popoverClassName), onOpening: _this.handlePopoverOpening, onOpened: _this.handlePopoverOpened }),
|
65 | React.createElement(InputGroup, __assign({ autoComplete: autoComplete, disabled: _this.props.disabled }, inputProps, { inputRef: _this.handleInputRef, onChange: listProps.handleQueryChange, onFocus: _this.handleInputFocus, onKeyDown: _this.getTargetKeyDownHandler(handleKeyDown), onKeyUp: _this.getTargetKeyUpHandler(handleKeyUp), placeholder: inputPlaceholder, value: inputValue })),
|
66 | React.createElement("div", { onKeyDown: handleKeyDown, onKeyUp: handleKeyUp }, listProps.itemList)));
|
67 | };
|
68 | _this.selectText = function () {
|
69 |
|
70 | _this.requestAnimationFrame(function () {
|
71 | var _a;
|
72 | (_a = _this.inputElement) === null || _a === void 0 ? void 0 : _a.setSelectionRange(0, _this.inputElement.value.length);
|
73 | });
|
74 | };
|
75 | _this.handleInputFocus = function (event) {
|
76 | var _a, _b;
|
77 | _this.selectText();
|
78 |
|
79 | if (!_this.props.openOnKeyDown) {
|
80 | _this.setState({ isOpen: true });
|
81 | }
|
82 | (_b = (_a = _this.props.inputProps) === null || _a === void 0 ? void 0 : _a.onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, event);
|
83 | };
|
84 | _this.handleItemSelect = function (item, event) {
|
85 | var _a, _b, _c, _d;
|
86 | var nextOpenState;
|
87 | if (!_this.props.closeOnSelect) {
|
88 | (_a = _this.inputElement) === null || _a === void 0 ? void 0 : _a.focus();
|
89 | _this.selectText();
|
90 | nextOpenState = true;
|
91 | }
|
92 | else {
|
93 | (_b = _this.inputElement) === null || _b === void 0 ? void 0 : _b.blur();
|
94 | nextOpenState = false;
|
95 | }
|
96 |
|
97 | if (_this.props.selectedItem === undefined) {
|
98 | _this.setState({
|
99 | isOpen: nextOpenState,
|
100 | selectedItem: item,
|
101 | });
|
102 | }
|
103 | else {
|
104 |
|
105 | _this.setState({ isOpen: nextOpenState });
|
106 | }
|
107 | (_d = (_c = _this.props).onItemSelect) === null || _d === void 0 ? void 0 : _d.call(_c, item, event);
|
108 | };
|
109 |
|
110 |
|
111 | _this.handlePopoverInteraction = function (nextOpenState, event) {
|
112 | return _this.requestAnimationFrame(function () {
|
113 | var _a, _b;
|
114 | var isInputFocused = _this.inputElement === document.activeElement;
|
115 | if (_this.inputElement != null && !isInputFocused) {
|
116 |
|
117 | _this.setState({ isOpen: false });
|
118 | }
|
119 | (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onInteraction) === null || _b === void 0 ? void 0 : _b.call(_a, nextOpenState, event);
|
120 | });
|
121 | };
|
122 | _this.handlePopoverOpening = function (node) {
|
123 | var _a, _b;
|
124 |
|
125 |
|
126 | if (_this.props.resetOnClose && _this.queryList) {
|
127 | _this.queryList.setQuery("", true);
|
128 | }
|
129 | (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onOpening) === null || _b === void 0 ? void 0 : _b.call(_a, node);
|
130 | };
|
131 | _this.handlePopoverOpened = function (node) {
|
132 | var _a, _b;
|
133 |
|
134 | if (_this.queryList != null) {
|
135 | _this.queryList.scrollActiveItemIntoView();
|
136 | }
|
137 | (_b = (_a = _this.props.popoverProps) === null || _a === void 0 ? void 0 : _a.onOpened) === null || _b === void 0 ? void 0 : _b.call(_a, node);
|
138 | };
|
139 | _this.getTargetKeyDownHandler = function (handleQueryListKeyDown) {
|
140 | return function (evt) {
|
141 | var _a, _b, _c;
|
142 |
|
143 | var which = evt.which;
|
144 | if (which === Keys.ESCAPE || which === Keys.TAB) {
|
145 | (_a = _this.inputElement) === null || _a === void 0 ? void 0 : _a.blur();
|
146 | _this.setState({ isOpen: false });
|
147 | }
|
148 | else if (_this.props.openOnKeyDown &&
|
149 | which !== Keys.BACKSPACE &&
|
150 | which !== Keys.ARROW_LEFT &&
|
151 | which !== Keys.ARROW_RIGHT) {
|
152 | _this.setState({ isOpen: true });
|
153 | }
|
154 | if (_this.state.isOpen) {
|
155 | handleQueryListKeyDown === null || handleQueryListKeyDown === void 0 ? void 0 : handleQueryListKeyDown(evt);
|
156 | }
|
157 | (_c = (_b = _this.props.inputProps) === null || _b === void 0 ? void 0 : _b.onKeyDown) === null || _c === void 0 ? void 0 : _c.call(_b, evt);
|
158 | };
|
159 | };
|
160 | _this.getTargetKeyUpHandler = function (handleQueryListKeyUp) {
|
161 | return function (evt) {
|
162 | var _a, _b;
|
163 | if (_this.state.isOpen) {
|
164 | handleQueryListKeyUp === null || handleQueryListKeyUp === void 0 ? void 0 : handleQueryListKeyUp(evt);
|
165 | }
|
166 | (_b = (_a = _this.props.inputProps) === null || _a === void 0 ? void 0 : _a.onKeyUp) === null || _b === void 0 ? void 0 : _b.call(_a, evt);
|
167 | };
|
168 | };
|
169 | return _this;
|
170 | }
|
171 | Suggest.ofType = function () {
|
172 | return Suggest;
|
173 | };
|
174 | Suggest.prototype.render = function () {
|
175 | var _a;
|
176 |
|
177 | var _b = this.props, disabled = _b.disabled, inputProps = _b.inputProps, popoverProps = _b.popoverProps, restProps = __rest(_b, ["disabled", "inputProps", "popoverProps"]);
|
178 | return (React.createElement(QueryList, __assign({}, restProps, { initialActiveItem: (_a = this.props.selectedItem) !== null && _a !== void 0 ? _a : undefined, onItemSelect: this.handleItemSelect, ref: this.handleQueryListRef, renderer: this.renderQueryList })));
|
179 | };
|
180 | Suggest.prototype.componentDidUpdate = function (prevProps, prevState) {
|
181 | var _this = this;
|
182 | var _a, _b, _c, _d, _e, _f, _g;
|
183 | if (((_a = prevProps.inputProps) === null || _a === void 0 ? void 0 : _a.inputRef) !== ((_b = this.props.inputProps) === null || _b === void 0 ? void 0 : _b.inputRef)) {
|
184 | setRef((_c = prevProps.inputProps) === null || _c === void 0 ? void 0 : _c.inputRef, null);
|
185 | this.handleInputRef = refHandler(this, "inputElement", (_d = this.props.inputProps) === null || _d === void 0 ? void 0 : _d.inputRef);
|
186 | setRef((_e = this.props.inputProps) === null || _e === void 0 ? void 0 : _e.inputRef, this.inputElement);
|
187 | }
|
188 |
|
189 | if (this.props.selectedItem !== undefined && this.props.selectedItem !== this.state.selectedItem) {
|
190 | this.setState({ selectedItem: this.props.selectedItem });
|
191 | }
|
192 | if (this.state.isOpen === false && prevState.isOpen === true) {
|
193 |
|
194 |
|
195 | var timeout = (_g = (_f = this.props.popoverProps) === null || _f === void 0 ? void 0 : _f.transitionDuration) !== null && _g !== void 0 ? _g : Popover.defaultProps.transitionDuration;
|
196 | setTimeout(function () { return _this.maybeResetActiveItemToSelectedItem(); }, timeout);
|
197 | }
|
198 | if (this.state.isOpen && !prevState.isOpen && this.queryList != null) {
|
199 | this.queryList.scrollActiveItemIntoView();
|
200 | }
|
201 | };
|
202 | Suggest.prototype.getInitialSelectedItem = function () {
|
203 |
|
204 | if (this.props.selectedItem !== undefined) {
|
205 | return this.props.selectedItem;
|
206 | }
|
207 | else if (this.props.defaultSelectedItem !== undefined) {
|
208 | return this.props.defaultSelectedItem;
|
209 | }
|
210 | else {
|
211 | return null;
|
212 | }
|
213 | };
|
214 | Suggest.prototype.maybeResetActiveItemToSelectedItem = function () {
|
215 | var _a;
|
216 | var shouldResetActiveItemToSelectedItem = this.props.activeItem === undefined && this.state.selectedItem !== null && !this.props.resetOnSelect;
|
217 | if (this.queryList !== null && shouldResetActiveItemToSelectedItem) {
|
218 | this.queryList.setActiveItem((_a = this.props.selectedItem) !== null && _a !== void 0 ? _a : this.state.selectedItem);
|
219 | }
|
220 | };
|
221 | Suggest.displayName = "".concat(DISPLAYNAME_PREFIX, ".Suggest");
|
222 | Suggest.defaultProps = {
|
223 | closeOnSelect: true,
|
224 | fill: false,
|
225 | openOnKeyDown: false,
|
226 | resetOnClose: false,
|
227 | };
|
228 | return Suggest;
|
229 | }(AbstractPureComponent2));
|
230 | export { Suggest };
|
231 |
|
\ | No newline at end of file |