UNPKG

4.28 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _classnames = _interopRequireDefault(require("classnames"));
7
8var _react = _interopRequireWildcard(require("react"));
9
10function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
12function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16const DropdownListInput = /*#__PURE__*/_react.default.forwardRef(function ({
17 name,
18 autoComplete,
19 value,
20 allowSearch,
21 placeholder,
22 textAccessor,
23 dataKeyAccessor,
24 searchTerm,
25 onSearch,
26 onAutofill,
27 onAutofillChange,
28 renderValue,
29 disabled,
30 readOnly
31}, ref) {
32 const [autofilling, setAutofilling] = (0, _react.useState)(false);
33 const searchRef = (0, _react.useRef)(null);
34
35 const handleAutofillDetect = ({
36 animationName
37 }) => {
38 let autofilling;
39 if (animationName === 'react-widgets-autofill-start') autofilling = true;else if (animationName === 'react-widgets-autofill-cancel') autofilling = false;else return;
40 setAutofilling(autofilling);
41 onAutofill(autofilling);
42 };
43
44 const handleAutofill = e => {
45 setAutofilling(false);
46 onAutofillChange(e);
47 };
48
49 let dataKey = dataKeyAccessor(value);
50 let text = value == null ? '' : textAccessor(value);
51 let strValue = String(dataKey != null ? dataKey : '');
52 if (strValue === String({})) strValue = '';
53 const inputValue = !value && placeholder ? /*#__PURE__*/_react.default.createElement("span", {
54 className: "rw-placeholder"
55 }, placeholder) : renderValue ? renderValue({
56 item: value,
57 dataKey,
58 text
59 }) : text;
60 (0, _react.useImperativeHandle)(ref, () => ({
61 focus() {
62 if (searchRef.current) searchRef.current.focus();
63 }
64
65 })); // There is some interaction between unmounting the search and value inputs
66 // that cancels autofilling in Chrome, it may be due to an input the browser
67 // was considering suddenly disappeared. hiding it seems to avoid the issue
68
69 const style = autofilling ? {
70 display: 'none'
71 } : undefined;
72 return /*#__PURE__*/_react.default.createElement("div", {
73 className: "rw-dropdown-list-input"
74 }, autoComplete !== 'off' && /*#__PURE__*/_react.default.createElement("input", {
75 name: name,
76 tabIndex: -1,
77 disabled: disabled,
78 readOnly: readOnly,
79 value: strValue == null ? '' : strValue,
80 autoComplete: autoComplete,
81 onChange: handleAutofill,
82 onAnimationStart: handleAutofillDetect,
83 "aria-hidden": !autofilling,
84 className: (0, _classnames.default)('rw-detect-autofill', !autofilling && 'rw-sr')
85 }), /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, allowSearch && /*#__PURE__*/_react.default.createElement("input", {
86 ref: searchRef,
87 disabled: disabled,
88 readOnly: readOnly,
89 style: style,
90 className: "rw-dropdownlist-search",
91 autoComplete: "off",
92 value: searchTerm || '',
93 size: (searchTerm || '').length + 2,
94 onChange: onSearch
95 }), !searchTerm && /*#__PURE__*/_react.default.createElement("span", {
96 className: "rw-dropdown-list-value",
97 style: style
98 }, inputValue)));
99});
100
101DropdownListInput.displayName = 'DropdownListInput';
102var _default = DropdownListInput;
103exports.default = _default;
\No newline at end of file