@spaced-out/ui-design-system
Version:
Sense UI components library
90 lines (89 loc) • 3.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SimpleInlineDropdown = void 0;
var React = _interopRequireWildcard(require("react"));
var _menu = require("../../utils/menu");
var _InlineDropdown = require("./InlineDropdown");
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 SimpleInlineDropdownBase = (props, ref) => {
const {
size = 'medium',
classNames,
anchorPosition,
options,
optionsVariant,
allowSearch,
selectedKeys,
onOptionSelect,
onMenuOpen,
onMenuClose,
resolveLabel,
resolveSecondaryLabel,
children,
isFluid,
menuVirtualization,
header,
footer,
menuClassNames,
showLabelTooltip,
clickAwayRef,
allowWrap = false,
elevation = 'modal',
...buttonProps
} = props;
const [btnText, setBtnText] = React.useState('');
const [inlineDropdownSelectedKeys, setInlineDropdownSelectedKeys] = React.useState(selectedKeys);
React.useEffect(() => {
const newBtnText = (0, _menu.getButtonLabelFromSelectedKeys)(selectedKeys, children);
setInlineDropdownSelectedKeys(selectedKeys);
setBtnText(newBtnText);
}, [selectedKeys]);
const handleOptionChange = (selectedOption, e) => {
e?.stopPropagation();
let newSelectedKeys = [];
if (optionsVariant === 'checkbox') {
newSelectedKeys = (0, _menu.getSelectedKeysFromSelectedOption)(selectedOption, inlineDropdownSelectedKeys);
} else {
newSelectedKeys = [selectedOption.key];
}
const newBtnText = (0, _menu.getButtonLabelFromSelectedKeys)(newSelectedKeys, children);
setInlineDropdownSelectedKeys(newSelectedKeys);
setBtnText(newBtnText);
setTimeout(() => {
onOptionSelect?.(selectedOption, e);
});
};
React.useImperativeHandle(ref, () => ({
selectedKeys: inlineDropdownSelectedKeys
}));
return /*#__PURE__*/React.createElement(_InlineDropdown.InlineDropdown, _extends({}, buttonProps, {
anchorPosition: anchorPosition,
classNames: classNames,
size: size,
onOptionSelect: handleOptionChange,
onMenuOpen: onMenuOpen,
onMenuClose: onMenuClose,
clickAwayRef: clickAwayRef,
elevation: elevation,
menu: {
isFluid,
options,
selectedKeys: inlineDropdownSelectedKeys,
optionsVariant,
allowSearch,
resolveLabel,
resolveSecondaryLabel,
size: size === 'extraSmall' ? 'small' : size,
virtualization: menuVirtualization,
header,
footer,
classNames: menuClassNames,
showLabelTooltip,
allowWrap
}
}), optionsVariant === 'checkbox' ? btnText : children);
};
const SimpleInlineDropdown = exports.SimpleInlineDropdown = /*#__PURE__*/React.forwardRef(SimpleInlineDropdownBase);