@spaced-out/ui-design-system
Version:
Sense UI components library
92 lines (91 loc) • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SimpleTypeahead = void 0;
var React = _interopRequireWildcard(require("react"));
var _menu = require("../../utils/menu");
var _Typeahead = require("./Typeahead");
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); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
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',
...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__*/React.createElement(_Typeahead.Typeahead, _extends({}, 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
}));
};
const SimpleTypeahead = exports.SimpleTypeahead = /*#__PURE__*/React.forwardRef(SimpleTypeaheadBase);