UNPKG

11.2 kBJavaScriptView Raw
1import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2import _createClass from "@babel/runtime/helpers/createClass";
3import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
4import _inherits from "@babel/runtime/helpers/inherits";
5import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7import _defineProperty from "@babel/runtime/helpers/defineProperty";
8import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
9import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
10import _extends from "@babel/runtime/helpers/extends";
11var _excluded = ["children", "onRootClose"];
12
13function 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
15function _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
17function _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
19function _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
21import cx from 'classnames';
22import PropTypes from 'prop-types';
23import React, { forwardRef, useState } from 'react';
24import useRootClose from "react-overlays/useRootClose";
25import Typeahead from '../../core/Typeahead';
26import ClearButton from '../ClearButton';
27import Loader from '../Loader';
28import Overlay from '../Overlay';
29import Token from '../Token/Token';
30import TypeaheadInputMulti from '../TypeaheadInputMulti';
31import TypeaheadInputSingle from '../TypeaheadInputSingle';
32import TypeaheadMenu from '../TypeaheadMenu';
33import { getOptionLabel, isFunction, isSizeLarge, pick, preventInputBlur } from '../../utils';
34import { checkPropType, inputPropsType, sizeType } from '../../propTypes';
35var propTypes = {
36 /**
37 * Displays a button to clear the input when there are selections.
38 */
39 clearButton: PropTypes.bool,
40
41 /**
42 * Props to be applied directly to the input. `onBlur`, `onChange`,
43 * `onFocus`, and `onKeyDown` are ignored.
44 */
45 inputProps: checkPropType(PropTypes.object, inputPropsType),
46
47 /**
48 * Bootstrap 4 only. Adds the `is-invalid` classname to the `form-control`.
49 */
50 isInvalid: PropTypes.bool,
51
52 /**
53 * Indicate whether an asynchronous data fetch is happening.
54 */
55 isLoading: PropTypes.bool,
56
57 /**
58 * Bootstrap 4 only. Adds the `is-valid` classname to the `form-control`.
59 */
60 isValid: PropTypes.bool,
61
62 /**
63 * Callback for custom input rendering.
64 */
65 renderInput: PropTypes.func,
66
67 /**
68 * Callback for custom menu rendering.
69 */
70 renderMenu: PropTypes.func,
71
72 /**
73 * Callback for custom menu rendering.
74 */
75 renderToken: PropTypes.func,
76
77 /**
78 * Specifies the size of the input.
79 */
80 size: sizeType
81};
82var defaultProps = {
83 isLoading: false
84};
85
86var defaultRenderMenu = function defaultRenderMenu(results, menuProps, props) {
87 return /*#__PURE__*/React.createElement(TypeaheadMenu, _extends({}, menuProps, {
88 labelKey: props.labelKey,
89 options: results,
90 text: props.text
91 }));
92};
93
94var defaultRenderToken = function defaultRenderToken(option, props, idx) {
95 return /*#__PURE__*/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
104var overlayPropKeys = ['align', 'dropup', 'flip', 'positionFixed'];
105
106function getOverlayProps(props) {
107 return pick(props, overlayPropKeys);
108}
109
110var 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
124var TypeaheadComponent = /*#__PURE__*/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 /*#__PURE__*/React.createElement(TypeaheadInputSingle, commonProps);
167 }
168
169 var labelKey = props.labelKey,
170 onRemove = props.onRemove,
171 selected = props.selected;
172 return /*#__PURE__*/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 = /*#__PURE__*/React.createElement(Loader, null);
214 } else if (clearButton && !disabled && selected.length) {
215 content = /*#__PURE__*/React.createElement(ClearButton, {
216 onClick: onClear,
217 onMouseDown: preventInputBlur,
218 size: size
219 });
220 }
221
222 return content ? /*#__PURE__*/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 /*#__PURE__*/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 /*#__PURE__*/React.createElement(RootClose, {
255 disabled: open || !isMenuShown,
256 onRootClose: hideMenu
257 }, function (ref) {
258 return /*#__PURE__*/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), /*#__PURE__*/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
288export default /*#__PURE__*/forwardRef(function (props, ref) {
289 return /*#__PURE__*/React.createElement(TypeaheadComponent, _extends({}, props, {
290 instanceRef: ref
291 }));
292});
\No newline at end of file