UNPKG

13.4 kBJavaScriptView Raw
1/*
2 * Copyright 2017 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __extends, __rest } from "tslib";
17/**
18 * @fileoverview This component is DEPRECATED, and the code is frozen.
19 * All changes & bugfixes should be made to Suggest2 instead.
20 */
21/* eslint-disable deprecation/deprecation, @blueprintjs/no-deprecated-components */
22import classNames from "classnames";
23import * as React from "react";
24import { AbstractPureComponent2, DISPLAYNAME_PREFIX, InputGroup, Keys, Popover, PopoverInteractionKind, Position, refHandler, setRef, } from "@blueprintjs/core";
25import { Classes } from "../../common";
26import { QueryList } from "../query-list/queryList";
27/**
28 * Suggest component.
29 *
30 * @see https://blueprintjs.com/docs/#select/suggest
31 * @deprecated use { Suggest2 } from "@blueprintjs/select"
32 */
33var Suggest = /** @class */ (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 // placeholder shows selected item while open.
54 var inputPlaceholder = isOpen && selectedItemText ? selectedItemText : placeholder;
55 // value shows query when open, and query remains when closed if nothing is selected.
56 // if resetOnClose is enabled, then hide query when not open. (see handlePopoverOpening)
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 // wait until the input is properly focused to select the text inside of it
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 // TODO can we leverage Popover.openOnTargetFocus for this?
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 // the internal state should only change when uncontrolled.
97 if (_this.props.selectedItem === undefined) {
98 _this.setState({
99 isOpen: nextOpenState,
100 selectedItem: item,
101 });
102 }
103 else {
104 // otherwise just set the next open state.
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 // Popover interaction kind is CLICK, so this only handles click events.
110 // Note that we defer to the next animation frame in order to get the latest document.activeElement
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 // the input is no longer focused, we should close the popover
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 // reset query before opening instead of when closing to prevent flash of unfiltered items.
125 // this is a limitation of the interactions between QueryList state and Popover transitions.
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 // scroll active item into view after popover transition completes and all dimensions are stable.
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 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
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 // omit props specific to this component, spread the rest.
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 // If the selected item prop changes, update the underlying state.
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 // just closed, likely by keyboard interaction
194 // wait until the transition ends so there isn't a flash of content in the popover
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 // controlled > uncontrolled > default
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));
230export { Suggest };
231//# sourceMappingURL=suggest.js.map
\No newline at end of file