@spaced-out/ui-design-system
Version:
Sense UI components library
141 lines (140 loc) • 6.12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MenuOptionButton = void 0;
var React = _interopRequireWildcard(require("react"));
var _classify = require("../../utils/classify");
var _Button = require("../Button");
var _Checkbox = require("../Checkbox");
var _Icon = require("../Icon");
var _RadioButton = require("../RadioButton");
var _Truncate = require("../Truncate");
var _TruncatedTextWithTooltip = require("../TruncatedTextWithTooltip");
var _MenuModule = _interopRequireDefault(require("./Menu.module.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 MenuOptionButton = props => {
const lastMenuItemRef = React.useRef(null);
const {
option,
size = 'medium',
onSelect,
selectedOption,
menuDisabled,
classNames,
optionsVariant = 'normal',
selectedKeys,
isLastItem,
onTabOut,
resolveLabel,
resolveSecondaryLabel,
style,
showLabelTooltip,
allowWrap = false
} = props;
const {
key,
label,
secondaryLabel,
customComponent,
iconLeft,
iconLeftType,
classNames: optionClassNames,
iconRight,
iconRightType,
disabled,
optionSize,
optionVariant = optionsVariant,
indeterminate = false
} = option;
const [buttonSize, setButtonSize] = React.useState(optionSize || size);
const resolvedLabel = resolveLabel ? resolveLabel(option) : label;
const resolvedSecondaryLabel = resolveSecondaryLabel ? resolveSecondaryLabel(option) : secondaryLabel;
const isSelected = () => {
if (!selectedKeys || !Array.isArray(selectedKeys) || !selectedKeys.length) {
return false;
}
return selectedKeys.includes(option.key);
};
React.useEffect(() => {
setButtonSize(optionSize || size);
}, [optionSize, size]);
React.useEffect(() => {
const handleKeyDown = event => {
if (event.key === 'Tab' && !event.shiftKey) {
// Tab pressed without shift key, calling tab out callback
onTabOut?.();
}
};
lastMenuItemRef.current?.addEventListener('keydown', handleKeyDown);
return () => {
lastMenuItemRef.current?.removeEventListener('keydown', handleKeyDown);
};
}, [isLastItem]);
return /*#__PURE__*/React.createElement(_Button.UnstyledButton, _extends({
className: (0, _classify.classify)(_MenuModule.default.option, {
[_MenuModule.default.selected]: isSelected() || key === selectedOption?.key,
[_MenuModule.default.optionSmall]: buttonSize === 'small',
[_MenuModule.default.optionMedium]: buttonSize === 'medium',
[_MenuModule.default.disabled]: menuDisabled || disabled,
[_MenuModule.default.withIconLeft]: !!iconLeft,
[_MenuModule.default.withIconRight]: !!iconRight
}, classNames?.option, optionClassNames?.wrapper),
style: style,
disabled: menuDisabled || disabled,
onClick: e => onSelect && onSelect(option, e),
autoFocus: selectedOption?.key === key
}, isLastItem ? {
ref: lastMenuItemRef
} : {}), optionVariant === 'checkbox' && /*#__PURE__*/React.createElement(_Checkbox.Checkbox, {
tabIndex: -1,
disabled: menuDisabled || disabled,
checked: isSelected(),
indeterminate: indeterminate
}), optionVariant === 'radio' && /*#__PURE__*/React.createElement(_RadioButton.RadioButton, {
disabled: menuDisabled || disabled,
value: option.key,
selectedValue: selectedKeys?.[0],
tabIndex: -1
}), !!iconLeft && /*#__PURE__*/React.createElement(_Icon.Icon, {
name: iconLeft,
type: iconLeftType,
size: "small",
className: _MenuModule.default.icon
}), /*#__PURE__*/React.createElement("div", {
className: (0, _classify.classify)(_MenuModule.default.optionTextContainer, classNames?.optionTextContainer)
}, /*#__PURE__*/React.isValidElement(customComponent) ? customComponent : /*#__PURE__*/React.createElement("div", {
className: (0, _classify.classify)(_MenuModule.default.optionTextLabel, classNames?.optionTextLabel)
}, renderLabel(resolvedLabel, allowWrap, showLabelTooltip)), !!secondaryLabel && /*#__PURE__*/React.createElement("div", {
className: _MenuModule.default.optionTextSecondaryLabel
}, /*#__PURE__*/React.createElement(_Truncate.Truncate, null, resolvedSecondaryLabel))), !!iconRight && /*#__PURE__*/React.createElement(_Icon.Icon, {
name: iconRight,
type: iconRightType,
size: "small",
className: (0, _classify.classify)(_MenuModule.default.icon, _MenuModule.default.rightIcon)
}));
};
exports.MenuOptionButton = MenuOptionButton;
const renderLabel = (label, allowWrap, showLabelTooltip) => {
if (showLabelTooltip) {
return /*#__PURE__*/React.createElement(_TruncatedTextWithTooltip.TruncatedTextWithTooltip, {
tooltip: {
bodyMaxLines: showLabelTooltip.maxLines ?? 10,
delayMotionDuration: 'normal'
},
line: 3
}, label);
}
if (allowWrap) {
return /*#__PURE__*/React.createElement(_TruncatedTextWithTooltip.TruncatedTextWithTooltip, {
tooltip: {
bodyMaxLines: 10,
delayMotionDuration: 'normal'
},
line: 3
}, label);
}
return /*#__PURE__*/React.createElement(_Truncate.Truncate, null, label);
};