UNPKG

2.07 kBPlain TextView Raw
1import * as React from "react";
2import { As, PropsWithAs } from "reakit-utils/types";
3import { createComponent } from "reakit-system/createComponent";
4import { createHook } from "reakit-system/createHook";
5import { GroupOptions, GroupHTMLProps, useGroup } from "../Group/Group";
6import { DeepPath } from "./__utils/types";
7import { getInputId } from "./__utils/getInputId";
8import { getMessageId } from "./__utils/getMessageId";
9import { getLabelId } from "./__utils/getLabelId";
10import { shouldShowError } from "./__utils/shouldShowError";
11import { unstable_FormStateReturn } from "./FormState";
12import { FORM_GROUP_KEYS } from "./__keys";
13
14export type unstable_FormGroupOptions<
15 V,
16 P extends DeepPath<V, P>
17> = GroupOptions &
18 Pick<unstable_FormStateReturn<V>, "baseId" | "touched" | "errors"> & {
19 /**
20 * FormGroup's name as in form values.
21 */
22 name: P;
23 };
24
25export type unstable_FormGroupHTMLProps = GroupHTMLProps &
26 React.FieldsetHTMLAttributes<any>;
27
28export type unstable_FormGroupProps<
29 V,
30 P extends DeepPath<V, P>
31> = unstable_FormGroupOptions<V, P> & unstable_FormGroupHTMLProps;
32
33export 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
56export 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;