import * as React from "react"; import { As, PropsWithAs } from "reakit-utils/types"; import { createComponent } from "reakit-system/createComponent"; import { createHook } from "reakit-system/createHook"; import { GroupOptions, GroupHTMLProps, useGroup } from "../Group/Group"; import { DeepPath } from "./__utils/types"; import { getInputId } from "./__utils/getInputId"; import { getMessageId } from "./__utils/getMessageId"; import { getLabelId } from "./__utils/getLabelId"; import { shouldShowError } from "./__utils/shouldShowError"; import { unstable_FormStateReturn } from "./FormState"; import { FORM_GROUP_KEYS } from "./__keys"; export type unstable_FormGroupOptions< V, P extends DeepPath > = GroupOptions & Pick, "baseId" | "touched" | "errors"> & { /** * FormGroup's name as in form values. */ name: P; }; export type unstable_FormGroupHTMLProps = GroupHTMLProps & React.FieldsetHTMLAttributes; export type unstable_FormGroupProps< V, P extends DeepPath > = unstable_FormGroupOptions & unstable_FormGroupHTMLProps; export const unstable_useFormGroup = createHook< unstable_FormGroupOptions, unstable_FormGroupHTMLProps >({ name: "FormGroup", compose: useGroup, keys: FORM_GROUP_KEYS, useProps(options, htmlProps) { return { id: getInputId(options.name, options.baseId), tabIndex: -1, "aria-describedby": getMessageId(options.name, options.baseId), "aria-labelledby": getLabelId(options.name, options.baseId), "aria-invalid": shouldShowError(options, options.name), ...htmlProps, }; }, }) as >( options: unstable_FormGroupOptions, htmlProps?: unstable_FormGroupHTMLProps ) => unstable_FormGroupHTMLProps; export const unstable_FormGroup = (createComponent({ as: "fieldset", useHook: unstable_useFormGroup, }) as unknown) as , T extends As = "fieldset">( props: PropsWithAs, T> ) => JSX.Element;