@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
197 lines (196 loc) • 7.68 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.useCheckboxOrToggleOptions = useCheckboxOrToggleOptions;
var _push = _interopRequireDefault(require("core-js-pure/stable/instance/push.js"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _componentHelper = require("../../../../shared/component-helper.js");
var _index = require("../../../../components/index.js");
var _index2 = _interopRequireDefault(require("../../FieldBlock/index.js"));
var _index3 = require("../../hooks/index.js");
var _useFieldProps = require("../../hooks/useFieldProps.js");
var _utils = require("../../../../components/flex/utils.js");
var _ToggleButtonGroupContext = _interopRequireDefault(require("../../../../components/toggle-button/ToggleButtonGroupContext.js"));
var _index4 = require("../Selection/index.js");
var _Context = _interopRequireDefault(require("../../DataContext/Context.js"));
var _useDataValue = _interopRequireDefault(require("../../hooks/useDataValue.js"));
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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
function ArraySelection(props) {
const {
id,
path,
dataPath,
data,
className,
variant = 'checkbox',
layout = 'vertical',
optionsLayout = 'vertical',
value,
hasError,
info,
warning,
disabled,
size,
width,
emptyValue,
htmlAttributes,
handleChange,
setDisplayValue,
children,
label
} = (0, _index3.useFieldProps)(props);
const {
getValueByPath
} = (0, _useDataValue.default)();
const dataList = dataPath ? getValueByPath(dataPath) : data;
const hasRenderPropChildren = typeof children === 'function';
const renderedChildren = (0, _react.useMemo)(() => {
return resolveChildren(children, value, dataList);
}, [children, dataList, value]);
const fieldBlockProps = {
forId: id,
className: (0, _classnames.default)(`dnb-forms-field-array-selection dnb-forms-field-array-selection--variant-${variant === 'checkbox' ? 'checkbox' : 'button'} dnb-forms-field-array-selection--layout-${layout} dnb-forms-field-array-selection--options-layout--${optionsLayout}`, className),
contentClassName: 'dnb-forms-field-array-selection__options',
disableStatusSummary: true,
asFieldset: Boolean(label),
fieldsetRole: 'group',
...(0, _utils.pickSpacingProps)(props)
};
if (!size) {
fieldBlockProps.labelHeight = 'small';
}
if (width) {
fieldBlockProps.contentWidth = width;
}
const options = useCheckboxOrToggleOptions({
id,
path,
variant,
info,
warning,
emptyValue,
htmlAttributes,
dataList: hasRenderPropChildren ? undefined : dataList,
children: renderedChildren,
value,
disabled,
size,
hasError,
handleChange,
handleActiveData: ({
labels
}) => {
setDisplayValue(labels);
}
});
switch (variant) {
case 'checkbox':
return _react.default.createElement(_index2.default, fieldBlockProps, options);
default:
return _react.default.createElement(_index2.default, fieldBlockProps, _react.default.createElement(_ToggleButtonGroupContext.default.Provider, {
value: {
status: hasError ? 'error' : undefined,
disabled,
size
}
}, options));
}
}
function resolveChildren(children, value, options) {
if (typeof children === 'function') {
return children({
value,
options
});
}
return children;
}
function useCheckboxOrToggleOptions({
id,
path,
variant = 'checkbox',
info,
warning,
emptyValue,
htmlAttributes,
dataList,
children,
value,
disabled,
size,
hasError,
handleChange,
handleActiveData
}) {
const {
setFieldInternals
} = (0, _react.useContext)(_Context.default);
const optionsCount = (0, _react.useMemo)(() => (0, _index4.countOptions)(children) + ((dataList === null || dataList === void 0 ? void 0 : dataList.length) || 0), [dataList, children]);
const activeData = [];
const createOption = (0, _react.useCallback)((props, i) => {
const {
value: active,
error,
title,
help,
className,
children,
...rest
} = props;
if (value !== null && value !== void 0 && value.includes(active)) {
(0, _push.default)(activeData).call(activeData, props);
}
const label = title !== null && title !== void 0 ? title : children;
const suffix = help ? _react.default.createElement(_index.HelpButton, {
size: "small",
title: (0, _componentHelper.convertJsxToString)(help.title)
}, help.content) : undefined;
const handleSelect = () => {
const newValue = value !== null && value !== void 0 && value.includes(active) ? value.filter(value => value !== active) : [...(value !== null && value !== void 0 ? value : []), active];
handleChange === null || handleChange === void 0 || handleChange(newValue.length === 0 ? emptyValue : newValue);
};
const Component = variant === 'checkbox' ? _index.Checkbox : _index.ToggleButton;
return _react.default.createElement(Component, _extends({
id: optionsCount === 1 ? id : undefined,
key: `option-${i}-${value}`,
variant: variant === 'checkbox-button' ? 'checkbox' : undefined,
className: (0, _classnames.default)(`dnb-forms-field-array-selection__${variant === 'checkbox' ? 'checkbox' : 'button'}`, className),
size: size,
label: variant === 'checkbox' ? label : undefined,
text: variant !== 'checkbox' ? label : undefined,
value: value,
disabled: disabled,
checked: value === null || value === void 0 ? void 0 : value.includes(active),
status: (hasError || (0, _useFieldProps.checkForError)([error, info, warning])) && 'error',
suffix: suffix,
role: "checkbox",
on_change: handleSelect
}, htmlAttributes, rest));
}, [disabled, emptyValue, handleChange, hasError, htmlAttributes, id, info, optionsCount, value, variant, warning]);
const result = [...(dataList || []).map((props, i) => createOption(props, i)), ...((0, _index4.mapOptions)(children, {
createOption
}) || []).filter(Boolean)];
if (handleActiveData) {
handleActiveData({
labels: activeData.map(({
title,
children
}) => title !== null && title !== void 0 ? title : children)
});
}
if (path) {
setFieldInternals === null || setFieldInternals === void 0 || setFieldInternals(path + '/arraySelectionData', {
props: activeData
});
}
return result;
}
ArraySelection._supportsSpacingProps = true;
var _default = exports.default = ArraySelection;
//# sourceMappingURL=ArraySelection.js.map