nishant-design-system
Version:
Sense UI components library
92 lines (90 loc) • 4.65 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 _MenuModule = _interopRequireDefault(require("./Menu.module.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const MenuOptionButton = props => {
const {
option,
size,
onSelect,
selectedOption,
menuDisabled,
classNames,
optionsVariant = 'normal',
selectedKeys
} = props;
const {
key,
label,
secondaryLabel,
customComponent,
iconLeft,
iconLeftType,
iconRight,
iconRightType,
disabled,
optionSize,
optionVariant = optionsVariant
} = option;
const [buttonSize, setButtonSize] = React.useState(optionSize || size);
const isSelected = () => {
if (!selectedKeys || !Array.isArray(selectedKeys) || !selectedKeys.length) {
return false;
}
return selectedKeys.includes(option.key);
};
React.useEffect(() => {
setButtonSize(optionSize || size);
}, [optionSize, size]);
return /*#__PURE__*/React.createElement(_Button.UnstyledButton, {
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),
disabled: menuDisabled || disabled,
onClick: () => onSelect && onSelect(option),
autoFocus: selectedOption?.key === key
}, optionVariant === 'checkbox' && /*#__PURE__*/React.createElement(_Checkbox.Checkbox, {
tabIndex: -1,
disabled: menuDisabled || disabled,
checked: isSelected()
}), 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)
}, /*#__PURE__*/React.createElement(_Truncate.Truncate, null, label)), !!secondaryLabel && /*#__PURE__*/React.createElement("div", {
className: _MenuModule.default.optionTextSecondaryLabel
}, /*#__PURE__*/React.createElement(_Truncate.Truncate, null, secondaryLabel))), !!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;