UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

175 lines (174 loc) 6.67 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; const _excluded = ["value", "error", "title", "help", "className", "children"]; import "core-js/modules/web.dom-collections.iterator.js"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { useCallback, useContext, useMemo } from 'react'; import classnames from 'classnames'; import { convertJsxToString } from '../../../../shared/component-helper'; import { Checkbox, HelpButton, ToggleButton } from '../../../../components'; import FieldBlock from '../../FieldBlock'; import { useFieldProps } from '../../hooks'; import { checkForError } from '../../hooks/useFieldProps'; import { pickSpacingProps } from '../../../../components/flex/utils'; import ToggleButtonGroupContext from '../../../../components/toggle-button/ToggleButtonGroupContext'; import { mapOptions } from '../Selection'; import DataContext from '../../DataContext/Context'; import useDataValue from '../../hooks/useDataValue'; function ArraySelection(props) { const { id, path, dataPath, data, className, variant = 'checkbox', layout = 'vertical', optionsLayout = 'vertical', value, hasError, info, warning, disabled, size, emptyValue, htmlAttributes, handleChange, setDisplayValue, children } = useFieldProps(props); const { getValueByPath } = useDataValue(); const dataList = dataPath ? getValueByPath(dataPath) : data; const fieldBlockProps = _objectSpread({ forId: id, className: classnames(`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 }, pickSpacingProps(props)); if (!size) { fieldBlockProps.labelHeight = 'small'; } const options = useCheckboxOrToggleOptions({ id, path, variant, info, warning, emptyValue, htmlAttributes, dataList, children, value, disabled, size, hasError, handleChange, handleActiveData: _ref => { let { labels } = _ref; setDisplayValue(labels); } }); switch (variant) { case 'checkbox': return React.createElement(FieldBlock, fieldBlockProps, options); default: return React.createElement(FieldBlock, fieldBlockProps, React.createElement(ToggleButtonGroupContext.Provider, { value: { status: hasError ? 'error' : undefined, disabled, size } }, options)); } } export function useCheckboxOrToggleOptions(_ref2) { let { id, path, variant = 'checkbox', info, warning, emptyValue, htmlAttributes, dataList, children, value, disabled, size, hasError, handleChange, handleActiveData } = _ref2; const { setFieldInternals } = useContext(DataContext); const optionsCount = useMemo(() => React.Children.count(children) + ((dataList === null || dataList === void 0 ? void 0 : dataList.length) || 0), [dataList, children]); const activeData = []; const createOption = useCallback((props, i) => { const { value: active, error, title, help, className, children } = props, rest = _objectWithoutProperties(props, _excluded); if (value !== null && value !== void 0 && value.includes(active)) { activeData.push(props); } const label = title !== null && title !== void 0 ? title : children; const suffix = help ? React.createElement(HelpButton, { size: "small", title: 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 ? void 0 : handleChange(newValue.length === 0 ? emptyValue : newValue); }; const Component = variant === 'checkbox' ? Checkbox : ToggleButton; return React.createElement(Component, _extends({ id: optionsCount === 1 ? id : undefined, key: `option-${i}-${value}`, variant: variant === 'checkbox-button' ? 'checkbox' : undefined, className: classnames(`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 || checkForError([error, info, warning])) && 'error', suffix: suffix, 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)), ...(mapOptions(children, { createOption }) || []).filter(Boolean)]; if (handleActiveData) { handleActiveData({ labels: activeData.map(_ref3 => { let { title, children } = _ref3; return title !== null && title !== void 0 ? title : children; }) }); } if (path) { setFieldInternals === null || setFieldInternals === void 0 ? void 0 : setFieldInternals(path + '/arraySelectionData', activeData); } return result; } ArraySelection._supportsSpacingProps = true; export default ArraySelection; //# sourceMappingURL=ArraySelection.js.map