1 | import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
2 | import _createClass from "@babel/runtime/helpers/createClass";
|
3 | import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
4 | import _inherits from "@babel/runtime/helpers/inherits";
|
5 | import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
6 | import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
7 | import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
8 | import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
9 | import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
10 | import _extends from "@babel/runtime/helpers/extends";
|
11 | var _excluded = ["children", "onRootClose"];
|
12 |
|
13 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
14 |
|
15 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
16 |
|
17 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
18 |
|
19 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
20 |
|
21 | import cx from 'classnames';
|
22 | import PropTypes from 'prop-types';
|
23 | import React, { forwardRef, useState } from 'react';
|
24 | import useRootClose from "react-overlays/useRootClose";
|
25 | import Typeahead from '../../core/Typeahead';
|
26 | import ClearButton from '../ClearButton';
|
27 | import Loader from '../Loader';
|
28 | import Overlay from '../Overlay';
|
29 | import Token from '../Token/Token';
|
30 | import TypeaheadInputMulti from '../TypeaheadInputMulti';
|
31 | import TypeaheadInputSingle from '../TypeaheadInputSingle';
|
32 | import TypeaheadMenu from '../TypeaheadMenu';
|
33 | import { getOptionLabel, isFunction, isSizeLarge, pick, preventInputBlur } from '../../utils';
|
34 | import { checkPropType, inputPropsType, sizeType } from '../../propTypes';
|
35 | var propTypes = {
|
36 | |
37 |
|
38 |
|
39 | clearButton: PropTypes.bool,
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 | inputProps: checkPropType(PropTypes.object, inputPropsType),
|
46 |
|
47 | |
48 |
|
49 |
|
50 | isInvalid: PropTypes.bool,
|
51 |
|
52 | |
53 |
|
54 |
|
55 | isLoading: PropTypes.bool,
|
56 |
|
57 | |
58 |
|
59 |
|
60 | isValid: PropTypes.bool,
|
61 |
|
62 | |
63 |
|
64 |
|
65 | renderInput: PropTypes.func,
|
66 |
|
67 | |
68 |
|
69 |
|
70 | renderMenu: PropTypes.func,
|
71 |
|
72 | |
73 |
|
74 |
|
75 | renderToken: PropTypes.func,
|
76 |
|
77 | |
78 |
|
79 |
|
80 | size: sizeType
|
81 | };
|
82 | var defaultProps = {
|
83 | isLoading: false
|
84 | };
|
85 |
|
86 | var defaultRenderMenu = function defaultRenderMenu(results, menuProps, props) {
|
87 | return React.createElement(TypeaheadMenu, _extends({}, menuProps, {
|
88 | labelKey: props.labelKey,
|
89 | options: results,
|
90 | text: props.text
|
91 | }));
|
92 | };
|
93 |
|
94 | var defaultRenderToken = function defaultRenderToken(option, props, idx) {
|
95 | return React.createElement(Token, {
|
96 | disabled: props.disabled,
|
97 | key: idx,
|
98 | onRemove: props.onRemove,
|
99 | option: option,
|
100 | tabIndex: props.tabIndex
|
101 | }, getOptionLabel(option, props.labelKey));
|
102 | };
|
103 |
|
104 | var overlayPropKeys = ['align', 'dropup', 'flip', 'positionFixed'];
|
105 |
|
106 | function getOverlayProps(props) {
|
107 | return pick(props, overlayPropKeys);
|
108 | }
|
109 |
|
110 | var RootClose = function RootClose(_ref) {
|
111 | var children = _ref.children,
|
112 | onRootClose = _ref.onRootClose,
|
113 | props = _objectWithoutProperties(_ref, _excluded);
|
114 |
|
115 | var _useState = useState(null),
|
116 | _useState2 = _slicedToArray(_useState, 2),
|
117 | rootElement = _useState2[0],
|
118 | attachRef = _useState2[1];
|
119 |
|
120 | useRootClose(rootElement, onRootClose, props);
|
121 | return children(attachRef);
|
122 | };
|
123 |
|
124 | var TypeaheadComponent = function (_React$Component) {
|
125 | _inherits(TypeaheadComponent, _React$Component);
|
126 |
|
127 | var _super = _createSuper(TypeaheadComponent);
|
128 |
|
129 | function TypeaheadComponent() {
|
130 | var _this;
|
131 |
|
132 | _classCallCheck(this, TypeaheadComponent);
|
133 |
|
134 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
135 | args[_key] = arguments[_key];
|
136 | }
|
137 |
|
138 | _this = _super.call.apply(_super, [this].concat(args));
|
139 |
|
140 | _defineProperty(_assertThisInitialized(_this), "_referenceElement", null);
|
141 |
|
142 | _defineProperty(_assertThisInitialized(_this), "referenceElementRef", function (referenceElement) {
|
143 | _this._referenceElement = referenceElement;
|
144 | });
|
145 |
|
146 | _defineProperty(_assertThisInitialized(_this), "_renderInput", function (inputProps, props) {
|
147 | var _this$props = _this.props,
|
148 | isInvalid = _this$props.isInvalid,
|
149 | isValid = _this$props.isValid,
|
150 | multiple = _this$props.multiple,
|
151 | renderInput = _this$props.renderInput,
|
152 | renderToken = _this$props.renderToken,
|
153 | size = _this$props.size;
|
154 |
|
155 | if (isFunction(renderInput)) {
|
156 | return renderInput(inputProps, props);
|
157 | }
|
158 |
|
159 | var commonProps = _objectSpread(_objectSpread({}, inputProps), {}, {
|
160 | isInvalid: isInvalid,
|
161 | isValid: isValid,
|
162 | size: size
|
163 | });
|
164 |
|
165 | if (!multiple) {
|
166 | return React.createElement(TypeaheadInputSingle, commonProps);
|
167 | }
|
168 |
|
169 | var labelKey = props.labelKey,
|
170 | onRemove = props.onRemove,
|
171 | selected = props.selected;
|
172 | return React.createElement(TypeaheadInputMulti, _extends({}, commonProps, {
|
173 | placeholder: selected.length ? '' : inputProps.placeholder,
|
174 | selected: selected
|
175 | }), selected.map(function (option, idx) {
|
176 | return (renderToken || defaultRenderToken)(option, _objectSpread(_objectSpread({}, commonProps), {}, {
|
177 | labelKey: labelKey,
|
178 | onRemove: onRemove
|
179 | }), idx);
|
180 | }));
|
181 | });
|
182 |
|
183 | _defineProperty(_assertThisInitialized(_this), "_renderMenu", function (results, menuProps, props) {
|
184 | var _this$props2 = _this.props,
|
185 | emptyLabel = _this$props2.emptyLabel,
|
186 | id = _this$props2.id,
|
187 | maxHeight = _this$props2.maxHeight,
|
188 | newSelectionPrefix = _this$props2.newSelectionPrefix,
|
189 | paginationText = _this$props2.paginationText,
|
190 | renderMenu = _this$props2.renderMenu,
|
191 | renderMenuItemChildren = _this$props2.renderMenuItemChildren;
|
192 | return (renderMenu || defaultRenderMenu)(results, _objectSpread(_objectSpread({}, menuProps), {}, {
|
193 | emptyLabel: emptyLabel,
|
194 | id: id,
|
195 | maxHeight: maxHeight,
|
196 | newSelectionPrefix: newSelectionPrefix,
|
197 | paginationText: paginationText,
|
198 | renderMenuItemChildren: renderMenuItemChildren
|
199 | }), props);
|
200 | });
|
201 |
|
202 | _defineProperty(_assertThisInitialized(_this), "_renderAux", function (_ref2) {
|
203 | var onClear = _ref2.onClear,
|
204 | selected = _ref2.selected;
|
205 | var _this$props3 = _this.props,
|
206 | clearButton = _this$props3.clearButton,
|
207 | disabled = _this$props3.disabled,
|
208 | isLoading = _this$props3.isLoading,
|
209 | size = _this$props3.size;
|
210 | var content;
|
211 |
|
212 | if (isLoading) {
|
213 | content = React.createElement(Loader, null);
|
214 | } else if (clearButton && !disabled && selected.length) {
|
215 | content = React.createElement(ClearButton, {
|
216 | onClick: onClear,
|
217 | onMouseDown: preventInputBlur,
|
218 | size: size
|
219 | });
|
220 | }
|
221 |
|
222 | return content ? React.createElement("div", {
|
223 | className: cx('rbt-aux', {
|
224 | 'rbt-aux-lg': isSizeLarge(size)
|
225 | })
|
226 | }, content) : null;
|
227 | });
|
228 |
|
229 | return _this;
|
230 | }
|
231 |
|
232 | _createClass(TypeaheadComponent, [{
|
233 | key: "render",
|
234 | value: function render() {
|
235 | var _this2 = this;
|
236 |
|
237 | var _this$props4 = this.props,
|
238 | children = _this$props4.children,
|
239 | className = _this$props4.className,
|
240 | instanceRef = _this$props4.instanceRef,
|
241 | open = _this$props4.open,
|
242 | options = _this$props4.options,
|
243 | style = _this$props4.style;
|
244 | return React.createElement(Typeahead, _extends({}, this.props, {
|
245 | options: options,
|
246 | ref: instanceRef
|
247 | }), function (props) {
|
248 | var hideMenu = props.hideMenu,
|
249 | isMenuShown = props.isMenuShown,
|
250 | results = props.results;
|
251 |
|
252 | var auxContent = _this2._renderAux(props);
|
253 |
|
254 | return React.createElement(RootClose, {
|
255 | disabled: open || !isMenuShown,
|
256 | onRootClose: hideMenu
|
257 | }, function (ref) {
|
258 | return React.createElement("div", {
|
259 | className: cx('rbt', {
|
260 | 'has-aux': !!auxContent
|
261 | }, className),
|
262 | ref: ref,
|
263 | style: _objectSpread(_objectSpread({}, style), {}, {
|
264 | outline: 'none',
|
265 | position: 'relative'
|
266 | }),
|
267 | tabIndex: -1
|
268 | }, _this2._renderInput(_objectSpread(_objectSpread({}, props.getInputProps(_this2.props.inputProps)), {}, {
|
269 | referenceElementRef: _this2.referenceElementRef
|
270 | }), props), React.createElement(Overlay, _extends({}, getOverlayProps(_this2.props), {
|
271 | isMenuShown: isMenuShown,
|
272 | referenceElement: _this2._referenceElement
|
273 | }), function (menuProps) {
|
274 | return _this2._renderMenu(results, menuProps, props);
|
275 | }), auxContent, isFunction(children) ? children(props) : children);
|
276 | });
|
277 | });
|
278 | }
|
279 | }]);
|
280 |
|
281 | return TypeaheadComponent;
|
282 | }(React.Component);
|
283 |
|
284 | _defineProperty(TypeaheadComponent, "propTypes", propTypes);
|
285 |
|
286 | _defineProperty(TypeaheadComponent, "defaultProps", defaultProps);
|
287 |
|
288 | export default forwardRef(function (props, ref) {
|
289 | return React.createElement(TypeaheadComponent, _extends({}, props, {
|
290 | instanceRef: ref
|
291 | }));
|
292 | }); |
\ | No newline at end of file |