UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

188 lines (187 loc) 5.86 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import React, { useCallback, useContext, useMemo } from 'react'; import classnames from 'classnames'; import { convertJsxToString } from "../../../../shared/component-helper.js"; import { Checkbox, HelpButton, ToggleButton } from "../../../../components/index.js"; import FieldBlock from "../../FieldBlock/index.js"; import { useFieldProps } from "../../hooks/index.js"; import { checkForError } from "../../hooks/useFieldProps.js"; import { pickSpacingProps } from "../../../../components/flex/utils.js"; import ToggleButtonGroupContext from "../../../../components/toggle-button/ToggleButtonGroupContext.js"; import { countOptions, mapOptions } from "../Selection/index.js"; import DataContext from "../../DataContext/Context.js"; import useDataValue from "../../hooks/useDataValue.js"; 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 } = useFieldProps(props); const { getValueByPath } = useDataValue(); const dataList = dataPath ? getValueByPath(dataPath) : data; const hasRenderPropChildren = typeof children === 'function'; const renderedChildren = useMemo(() => { return resolveChildren(children, value, dataList); }, [children, dataList, value]); const fieldBlockProps = { 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, asFieldset: Boolean(label), fieldsetRole: 'group', ...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.createElement(FieldBlock, fieldBlockProps, options); default: return React.createElement(FieldBlock, fieldBlockProps, React.createElement(ToggleButtonGroupContext.Provider, { value: { status: hasError ? 'error' : undefined, disabled, size } }, options)); } } function resolveChildren(children, value, options) { if (typeof children === 'function') { return children({ value, options }); } return children; } export function useCheckboxOrToggleOptions({ id, path, variant = 'checkbox', info, warning, emptyValue, htmlAttributes, dataList, children, value, disabled, size, hasError, handleChange, handleActiveData }) { const { setFieldInternals } = useContext(DataContext); const optionsCount = useMemo(() => countOptions(children) + (dataList?.length || 0), [dataList, children]); const activeData = []; const createOption = useCallback((props, i) => { const { value: active, error, title, help, className, children, ...rest } = props; if (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?.includes(active) ? value.filter(value => value !== active) : [...(value !== null && value !== void 0 ? value : []), active]; 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?.includes(active), status: (hasError || 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)), ...(mapOptions(children, { createOption }) || []).filter(Boolean)]; if (handleActiveData) { handleActiveData({ labels: activeData.map(({ title, children }) => title !== null && title !== void 0 ? title : children) }); } if (path) { setFieldInternals?.(path + '/arraySelectionData', { props: activeData }); } return result; } ArraySelection._supportsSpacingProps = true; export default ArraySelection; //# sourceMappingURL=ArraySelection.js.map