1 | import * as React from "react";
|
2 | import { As, PropsWithAs } from "reakit-utils/types";
|
3 | import { createComponent } from "reakit-system/createComponent";
|
4 | import { createHook } from "reakit-system/createHook";
|
5 | import { GroupOptions, GroupHTMLProps, useGroup } from "../Group/Group";
|
6 | import { DeepPath } from "./__utils/types";
|
7 | import { getInputId } from "./__utils/getInputId";
|
8 | import { getMessageId } from "./__utils/getMessageId";
|
9 | import { getLabelId } from "./__utils/getLabelId";
|
10 | import { shouldShowError } from "./__utils/shouldShowError";
|
11 | import { unstable_FormStateReturn } from "./FormState";
|
12 | import { FORM_GROUP_KEYS } from "./__keys";
|
13 |
|
14 | export type unstable_FormGroupOptions<
|
15 | V,
|
16 | P extends DeepPath<V, P>
|
17 | > = GroupOptions &
|
18 | Pick<unstable_FormStateReturn<V>, "baseId" | "touched" | "errors"> & {
|
19 | |
20 |
|
21 |
|
22 | name: P;
|
23 | };
|
24 |
|
25 | export type unstable_FormGroupHTMLProps = GroupHTMLProps &
|
26 | React.FieldsetHTMLAttributes<any>;
|
27 |
|
28 | export type unstable_FormGroupProps<
|
29 | V,
|
30 | P extends DeepPath<V, P>
|
31 | > = unstable_FormGroupOptions<V, P> & unstable_FormGroupHTMLProps;
|
32 |
|
33 | export const unstable_useFormGroup = createHook<
|
34 | unstable_FormGroupOptions<any, any>,
|
35 | unstable_FormGroupHTMLProps
|
36 | >({
|
37 | name: "FormGroup",
|
38 | compose: useGroup,
|
39 | keys: FORM_GROUP_KEYS,
|
40 |
|
41 | useProps(options, htmlProps) {
|
42 | return {
|
43 | id: getInputId(options.name, options.baseId),
|
44 | tabIndex: -1,
|
45 | "aria-describedby": getMessageId(options.name, options.baseId),
|
46 | "aria-labelledby": getLabelId(options.name, options.baseId),
|
47 | "aria-invalid": shouldShowError(options, options.name),
|
48 | ...htmlProps,
|
49 | };
|
50 | },
|
51 | }) as <V, P extends DeepPath<V, P>>(
|
52 | options: unstable_FormGroupOptions<V, P>,
|
53 | htmlProps?: unstable_FormGroupHTMLProps
|
54 | ) => unstable_FormGroupHTMLProps;
|
55 |
|
56 | export const unstable_FormGroup = (createComponent({
|
57 | as: "fieldset",
|
58 | useHook: unstable_useFormGroup,
|
59 | }) as unknown) as <V, P extends DeepPath<V, P>, T extends As = "fieldset">(
|
60 | props: PropsWithAs<unstable_FormGroupOptions<V, P>, T>
|
61 | ) => JSX.Element;
|