UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

182 lines (181 loc) 5.79 kB
"use client"; var _AlignmentHelper; import React, { useContext, useRef, useState, useCallback } from 'react'; import clsx from 'clsx'; import useId from "../../shared/helpers/useId.js"; import { extendExistingPropsWithContext, validateDOMAttributes, getStatusState, combineDescribedBy, combineLabelledBy, dispatchCustomElementEvent, removeUndefinedProps } from "../../shared/component-helper.js"; import { pickFormElementProps } from "../../shared/helpers/filterValidProps.js"; import { applySpacing } from "../space/SpacingUtils.js"; import AlignmentHelper from "../../shared/AlignmentHelper.js"; import Space from "../Space.js"; import FormLabel from "../FormLabel.js"; import FormStatus from "../FormStatus.js"; import Flex from "../Flex.js"; import Context from "../../shared/Context.js"; import Suffix from "../../shared/helpers/Suffix.js"; import RadioGroupContext from "./RadioGroupContext.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const radioGroupDefaultProps = { label: null, labelDirection: 'vertical', labelSrOnly: null, labelPosition: null, title: null, disabled: null, skeleton: null, id: null, name: null, size: null, status: null, statusState: 'error', statusProps: null, statusNoAnimation: null, globalStatus: null, suffix: null, vertical: null, layoutDirection: 'row', value: undefined, attributes: null, className: null, children: null, onChange: null }; const parseChecked = state => /true|on/.test(String(state)); function RadioGroup(ownProps) { const context = useContext(Context); const ownPropsRef = useRef(ownProps); ownPropsRef.current = ownProps; const id = useId(ownProps.id); const nameRef = useRef(ownProps.name || id); const [value, setValue] = useState(ownProps.value); const [prevPropsValue, setPrevPropsValue] = useState(ownProps.value); const skipNextPropSync = useRef(false); if (ownProps.value !== prevPropsValue) { setPrevPropsValue(ownProps.value); if (!skipNextPropSync.current) { setValue(ownProps.value); } } skipNextPropSync.current = false; const onChangeHandler = useCallback(({ value: newValue, event }) => { skipNextPropSync.current = true; setValue(newValue); dispatchCustomElementEvent({ props: ownPropsRef.current }, 'onChange', { value: newValue, event }); }, []); const resolvedProps = { ...radioGroupDefaultProps, ...removeUndefinedProps({ ...ownProps }) }; const props = extendExistingPropsWithContext(resolvedProps, radioGroupDefaultProps, pickFormElementProps(context === null || context === void 0 ? void 0 : context.formElement), context === null || context === void 0 ? void 0 : context.RadioGroup); const { status, statusState, statusProps, statusNoAnimation, globalStatus, suffix, label, labelDirection, labelSrOnly, labelPosition, vertical, layoutDirection, size, disabled, skeleton, className, id: _id, name: _name, value: _value, children, onChange, ...rest } = props; const showStatus = getStatusState(status); const rootProps = applySpacing(props, { className: clsx(`dnb-radio-group dnb-radio-group--${layoutDirection} dnb-form-component`, className, status && `dnb-radio-group__status--${statusState}`) }); const params = { ...rest }; const legendId = id + '-label'; if (showStatus || suffix) { params['aria-describedby'] = combineDescribedBy(params, showStatus ? id + '-status' : null, suffix ? id + '-suffix' : null); } if (label) { params['aria-labelledby'] = combineLabelledBy(params, legendId); } validateDOMAttributes(ownProps, params); const groupContext = { name: nameRef.current, value, size, disabled, labelPosition, onChange: onChangeHandler }; const Fieldset = label ? 'fieldset' : 'div'; return _jsx(RadioGroupContext, { value: groupContext, children: _jsxs("div", { ...rootProps, children: [_AlignmentHelper || (_AlignmentHelper = _jsx(AlignmentHelper, {})), _jsx(Fieldset, { className: "dnb-radio-group__fieldset", "aria-labelledby": label ? legendId : undefined, role: "radiogroup", children: _jsxs(Flex.Container, { direction: vertical || labelDirection === 'vertical' ? 'vertical' : 'horizontal', align: "stretch", gap: vertical ? 'x-small' : 'small', children: [label && _jsx(FormLabel, { element: "legend", id: legendId, srOnly: labelSrOnly, vertical: false, children: label }), _jsxs(Space, { element: "span", id: id, className: "dnb-radio-group__shell", ...params, children: [children, suffix && _jsx(Suffix, { className: "dnb-radio-group__suffix", id: id + '-suffix', context: props, children: suffix }), _jsx(FormStatus, { show: showStatus, id: id + '-form-status', globalStatus: globalStatus, label: label, text: status, state: statusState, textId: id + '-status', widthSelector: id + ', ' + legendId, noAnimation: statusNoAnimation, skeleton: skeleton, ...statusProps })] })] }) })] }) }); } withComponentMarkers(RadioGroup, { _supportsSpacingProps: true }); export { parseChecked as RadioGroupParseChecked }; export default RadioGroup; //# sourceMappingURL=RadioGroup.js.map