UNPKG

@spaced-out/ui-design-system

Version:
91 lines (90 loc) 3.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleOptionButton = void 0; var React = _interopRequireWildcard(require("react")); var _menu = require("../../utils/menu"); var _OptionButton = require("./OptionButton"); 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 SimpleOptionButtonBase = (props, ref) => { const { size = 'small', classNames, tooltip, anchorPosition, options, optionsVariant, allowSearch, selectedKeys, onOptionSelect, onButtonClick, onMenuOpen, onMenuClose, resolveLabel, resolveSecondaryLabel, children, menuSize = 'small', menuVirtualization, header, footer, menuClassNames, showLabelTooltip, clickAwayRef, allowWrap = false, testId, ...buttonProps } = props; const [optionButtonSelectedKeys, setOptionButtonSelectedKeys] = React.useState(selectedKeys); React.useEffect(() => { setOptionButtonSelectedKeys(selectedKeys); }, [selectedKeys]); const handleOptionChange = (selectedOption, e) => { e?.stopPropagation(); let newSelectedKeys = [selectedOption.key]; if (optionsVariant === 'checkbox') { newSelectedKeys = (0, _menu.getSelectedKeysFromSelectedOption)(selectedOption, optionButtonSelectedKeys); } else { newSelectedKeys = [selectedOption.key]; } setOptionButtonSelectedKeys(newSelectedKeys); setTimeout(() => { onOptionSelect?.(selectedOption, e); }); }; React.useImperativeHandle(ref, () => ({ selectedKeys: optionButtonSelectedKeys })); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_OptionButton.OptionButton, { ...buttonProps, tooltip: tooltip, onButtonClick: onButtonClick, anchorPosition: anchorPosition, classNames: classNames, size: size, onOptionSelect: handleOptionChange, onMenuOpen: onMenuOpen, onMenuClose: onMenuClose, clickAwayRef: clickAwayRef, testId: testId, menu: { isFluid: false, options, selectedKeys: optionButtonSelectedKeys, optionsVariant, allowSearch, resolveLabel, resolveSecondaryLabel, size: menuSize, virtualization: menuVirtualization, header, footer, classNames: menuClassNames, showLabelTooltip, allowWrap }, children: children }); }; const SimpleOptionButton = exports.SimpleOptionButton = /*#__PURE__*/React.forwardRef(SimpleOptionButtonBase);