@chayns-components/core
Version:
A set of beautiful React components for developing your own applications with chayns.
94 lines (93 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _chaynsApi = require("chayns-api");
var _react = _interopRequireWildcard(require("react"));
var _Button = _interopRequireDefault(require("../button/Button"));
var _SelectButton = require("./SelectButton.styles");
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); }
const SelectButton = ({
buttonText,
isDisabled,
list,
onSelect,
selectedItemIds,
shouldAllowMultiSelect,
shouldShowButtonTextWithSelection,
maxDisplayedItems = 3,
additionalText = ', ##count## weitere',
shouldShowSearch,
selectAllText,
title,
shouldEnableKeyboardHighlighting
}) => {
const itemList = (0, _react.useMemo)(() => {
const items = [];
list.forEach(({
text,
id
}) => {
const isSelected = selectedItemIds ? selectedItemIds.includes(id) : false;
items.push({
name: text,
id,
isSelected
});
});
return items;
}, [list, selectedItemIds]);
const internalButtonText = (0, _react.useMemo)(() => {
if (shouldShowButtonTextWithSelection || !selectedItemIds || selectedItemIds.length === 0) {
return buttonText;
}
let addedCount = 0;
let newText = '';
const additionalCount = selectedItemIds.length - maxDisplayedItems;
list.forEach(({
text,
id
}) => {
if (addedCount < maxDisplayedItems && selectedItemIds !== null && selectedItemIds !== void 0 && selectedItemIds.includes(id)) {
addedCount++;
newText += newText.length === 0 ? `${text}` : `, ${text}`;
}
});
if (additionalCount >= 1) {
newText += additionalText.replace('##count##', String(additionalCount));
}
return newText;
}, [additionalText, buttonText, list, maxDisplayedItems, selectedItemIds, shouldShowButtonTextWithSelection]);
const handleClick = () => {
void (0, _chaynsApi.createDialog)({
text: title ? `[h1]${title}[/h1]` : undefined,
type: _chaynsApi.DialogType.SELECT,
list: itemList,
multiselect: shouldAllowMultiSelect,
quickfind: shouldShowSearch,
selectAllCheckbox: selectAllText
}).open().then(result => {
// Ignore because there is no type
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (result && result.buttonType === 1 && typeof onSelect === 'function') {
// Ignore because there is no type
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
onSelect(result.result);
}
});
};
return /*#__PURE__*/_react.default.createElement(_SelectButton.StyledSelectButton, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
onClick: handleClick,
isDisabled: isDisabled,
isSecondary: true,
shouldShowTextAsRobotoMedium: false,
shouldEnableKeyboardHighlighting: shouldEnableKeyboardHighlighting
}, internalButtonText));
};
SelectButton.displayName = 'SelectButton';
var _default = exports.default = SelectButton;
//# sourceMappingURL=SelectButton.js.map