UNPKG

@spaced-out/ui-design-system

Version:
91 lines (90 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleButtonDropdown = void 0; var React = _interopRequireWildcard(require("react")); var _menu = require("../../utils/menu"); var _ButtonDropdown = require("./ButtonDropdown"); 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 SimpleButtonDropdownBase = (props, ref) => { const { size = 'medium', classNames, anchorPosition, options, optionsVariant, allowSearch, selectedKeys, onOptionSelect, onMenuOpen, onMenuClose, resolveLabel, resolveSecondaryLabel, children, isFluid, menuVirtualization, header, footer, menuClassNames, showLabelTooltip, allowWrap = false, clickAwayRef, elevation = 'modal', ...buttonProps } = props; const [btnText, setBtnText] = React.useState(''); const [buttonDropdownSelectedKeys, setButtonDropdownSelectedKeys] = React.useState(selectedKeys); React.useEffect(() => { const newBtnText = (0, _menu.getButtonLabelFromSelectedKeys)(selectedKeys, children); setButtonDropdownSelectedKeys(selectedKeys); setBtnText(newBtnText); }, [selectedKeys]); const handleOptionChange = (selectedOption, e) => { e?.stopPropagation(); let newSelectedKeys = []; if (optionsVariant === 'checkbox') { newSelectedKeys = (0, _menu.getSelectedKeysFromSelectedOption)(selectedOption, buttonDropdownSelectedKeys); } else { newSelectedKeys = [selectedOption.key]; } const newBtnText = (0, _menu.getButtonLabelFromSelectedKeys)(newSelectedKeys, children); setButtonDropdownSelectedKeys(newSelectedKeys); setBtnText(newBtnText); setTimeout(() => { onOptionSelect?.(selectedOption, e); }); }; React.useImperativeHandle(ref, () => ({ selectedKeys: buttonDropdownSelectedKeys })); return /*#__PURE__*/React.createElement(_ButtonDropdown.ButtonDropdown, _extends({}, buttonProps, { anchorPosition: anchorPosition, classNames: classNames, size: size, onOptionSelect: handleOptionChange, onMenuOpen: onMenuOpen, elevation: elevation, onMenuClose: onMenuClose, clickAwayRef: clickAwayRef, menu: { // TODO (Sharad): The isFluid prop here controls menu width unlike ButtonDropdown, and is independent from the isFluid prop in ButtonDropdown. There should be a way to set the isFluid prop of ButtonDropdown. isFluid, options, selectedKeys: buttonDropdownSelectedKeys, optionsVariant, allowSearch, resolveLabel, resolveSecondaryLabel, size, virtualization: menuVirtualization, header, footer, classNames: menuClassNames, showLabelTooltip, allowWrap } }), optionsVariant === 'checkbox' ? btnText : children); }; const SimpleButtonDropdown = exports.SimpleButtonDropdown = /*#__PURE__*/React.forwardRef(SimpleButtonDropdownBase);