import React, { HTMLAttributes, forwardRef, useRef } from "react"; import { BodyShort, Heading } from "../../typography"; import { cl, composeEventHandlers } from "../../utils/helpers"; import { useMergeRefs } from "../../utils/hooks"; import { useI18n } from "../../utils/i18n/i18n.hooks"; import ErrorSummaryItem from "./ErrorSummaryItem"; export interface ErrorSummaryProps extends Omit< HTMLAttributes, "tabIndex" > { /** * Collection of `ErrorSummary.Item`. */ children: React.ReactNode; /** * Changes padding and font-sizes. * @default "medium" */ size?: "medium" | "small"; /** * Heading above links. * @default "Du må rette disse feilene før du kan fortsette:" */ heading?: React.ReactNode; /** * Allows setting a different HTML h-tag. * @default "h2" */ headingTag?: React.ElementType; } interface ErrorSummaryComponent extends React.ForwardRefExoticComponent< ErrorSummaryProps & React.RefAttributes > { /** * Error message with link to field. * * @see [🤖 OverridableComponent](https://aksel.nav.no/grunnleggende/kode/overridablecomponent) support * * @example * ```jsx * * Felt må fylles ut med alder * * ``` */ Item: typeof ErrorSummaryItem; } /** * Summary of errors in a form. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/errorsummary) * @see 🏷️ {@link ErrorSummaryProps} * * @example * ```jsx * * * Felt må fylles ut med alder * * * Tekstfeltet må ha en godkjent e-post * * * ``` */ export const ErrorSummary = forwardRef( ( { children, className, size = "medium", headingTag = "h2", heading, ...rest }, ref, ) => { const translate = useI18n("ErrorSummary"); const wrapperRef = useRef(null); const headingRef = useRef(null); const mergedRef = useMergeRefs(ref, wrapperRef); return ( // biome-ignore lint/a11y/noStaticElementInteractions: Allows focus-calls on div to move focus to heading.
{ if (event.target === wrapperRef.current) { headingRef?.current?.focus(); } })} > {heading ?? translate("heading")} {children}
); }, ) as ErrorSummaryComponent; ErrorSummary.Item = ErrorSummaryItem; export default ErrorSummary;