UNPKG

@spaced-out/ui-design-system

Version:
95 lines (94 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleTypeahead = void 0; var React = _interopRequireWildcard(require("react")); var _menu = require("../../utils/menu"); var _Typeahead = require("./Typeahead"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const SimpleTypeaheadBase = (props, ref) => { const { size = 'medium', classNames, placeholder = 'Select...', options, selectedKeys, onSelect, onMenuOpen, onMenuClose, resolveLabel, resolveSecondaryLabel, onClear, onSearch, menuVirtualization, header, footer, menuClassNames, clickAwayRef, showLabelTooltip, allowInternalFilter = true, allowWrap = false, elevation = 'modal', testId, ...inputProps } = props; const [typeaheadInputText, setTypeaheadInputText] = React.useState(''); const [typeaheadSelectedKeys, setTypeaheadSelectedKeys] = React.useState(selectedKeys); React.useEffect(() => { const newTypeaheadText = (0, _menu.getTextLabelFromSelectedKeys)(selectedKeys, options); setTypeaheadSelectedKeys(selectedKeys); setTypeaheadInputText(newTypeaheadText); }, [selectedKeys]); const handleOptionChange = (selectedOption, e) => { e?.stopPropagation(); const newSelectedKeys = [selectedOption.key]; const newTypeaheadText = (0, _menu.getTextLabelFromSelectedKeys)(newSelectedKeys, options); setTypeaheadSelectedKeys(newSelectedKeys); setTypeaheadInputText(newTypeaheadText); setTimeout(() => { onSelect?.(selectedOption, e); }); }; React.useImperativeHandle(ref, () => ({ selectedKeys: typeaheadSelectedKeys })); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typeahead.Typeahead, { ...inputProps, allowInternalFilter: allowInternalFilter, classNames: classNames, size: size, placeholder: placeholder, onSelect: handleOptionChange, onMenuOpen: onMenuOpen, onMenuClose: onMenuClose, typeaheadInputText: typeaheadInputText, onSearch: e => { setTypeaheadInputText(e.target.value); onSearch?.(e); }, onClear: () => { setTypeaheadInputText(''); setTypeaheadSelectedKeys([]); onClear?.(); }, menu: { options, selectedKeys: typeaheadSelectedKeys, resolveLabel, resolveSecondaryLabel, size, virtualization: menuVirtualization, header, footer, classNames: menuClassNames, showLabelTooltip, allowWrap }, clickAwayRef: clickAwayRef, elevation: elevation, testId: testId }); }; const SimpleTypeahead = exports.SimpleTypeahead = /*#__PURE__*/React.forwardRef(SimpleTypeaheadBase);