UNPKG

1.9 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 { RoleOptions, RoleHTMLProps, useRole } from "../Role/Role";
6import { DeepPath } from "./__utils/types";
7import { getInputId } from "./__utils/getInputId";
8import { getLabelId } from "./__utils/getLabelId";
9import { unstable_FormStateReturn } from "./FormState";
10import { FORM_LABEL_KEYS } from "./__keys";
11
12export type unstable_FormLabelOptions<
13 V,
14 P extends DeepPath<V, P>
15> = RoleOptions &
16 Pick<unstable_FormStateReturn<V>, "baseId" | "values"> & {
17 /**
18 * FormInput's name as in form values.
19 */
20 name: P;
21 /**
22 * Label can be passed as the `label` prop or `children`.
23 */
24 label?: any;
25 };
26
27export type unstable_FormLabelHTMLProps = RoleHTMLProps &
28 React.LabelHTMLAttributes<any>;
29
30export type unstable_FormLabelProps<
31 V,
32 P extends DeepPath<V, P>
33> = unstable_FormLabelOptions<V, P> & unstable_FormLabelHTMLProps;
34
35export const unstable_useFormLabel = createHook<
36 unstable_FormLabelOptions<any, any>,
37 unstable_FormLabelHTMLProps
38>({
39 name: "FormLabel",
40 compose: useRole,
41 keys: FORM_LABEL_KEYS,
42
43 useProps(options, htmlProps) {
44 return {
45 children: options.label,
46 id: getLabelId(options.name, options.baseId),
47 htmlFor: getInputId(options.name, options.baseId),
48 ...htmlProps,
49 };
50 },
51}) as <V, P extends DeepPath<V, P>>(
52 options: unstable_FormLabelOptions<V, P>,
53 htmlProps?: unstable_FormLabelHTMLProps
54) => unstable_FormLabelHTMLProps;
55
56export const unstable_FormLabel = (createComponent({
57 as: "label",
58 memo: true,
59 useHook: unstable_useFormLabel,
60}) as unknown) as <V, P extends DeepPath<V, P>, T extends As = "label">(
61 props: PropsWithAs<unstable_FormLabelOptions<V, P>, T>
62) => JSX.Element;