import React, { FieldsetHTMLAttributes, forwardRef, useContext } from "react"; import { BodyShort, ErrorMessage, Label } from "../../typography"; import { omit, useId } from "../../utils-external"; import { cl } from "../../utils/helpers"; import { ReadOnlyIcon, ReadOnlyIconWithTitle } from "../ReadOnlyIcon"; import { FormFieldProps } from "../useFormField"; import { FieldsetContext } from "./context"; import { useFieldset } from "./useFieldset"; export interface FieldsetProps extends FormFieldProps, FieldsetHTMLAttributes { /** * Form fields in Fieldset. */ children: React.ReactNode; /** * Fieldset legend. */ legend: React.ReactNode; /** * If enabled, shows the legend and description for screen readers only. */ hideLegend?: boolean; /** * Toggles error propagation to child-elements. * @default true */ errorPropagation?: boolean; /** * @private */ _fieldsSupportNativeReadOnly?: boolean; } /** * Component for grouping form fields. * * **NB: Only for special use cases.** Form fields should not be grouped by default, * except for checkboxes and radio buttons, for which CheckboxGroup/RadioGroup should be used instead. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/fieldset) * @see 🏷️ {@link FieldsetProps} * * @example * ```jsx *
* * *
* ``` */ export const Fieldset = forwardRef( (props, ref) => { const legendId = useId(); const { inputProps, errorId, showErrorMsg, hasError, size, readOnly, inputDescriptionId, } = useFieldset(props, legendId); const fieldset = useContext(FieldsetContext); const { children, className, errorPropagation = true, legend, description, hideLegend, _fieldsSupportNativeReadOnly = true, ...rest } = props; return (
{!!description && ( {props.description} )} {children}
{showErrorMsg && ( {props.error} )}
); }, ); export default Fieldset;