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 { RoleOptions, RoleHTMLProps, useRole } from "../Role/Role";
|
6 | import { DeepPath } from "./__utils/types";
|
7 | import { getInputId } from "./__utils/getInputId";
|
8 | import { getLabelId } from "./__utils/getLabelId";
|
9 | import { unstable_FormStateReturn } from "./FormState";
|
10 | import { FORM_LABEL_KEYS } from "./__keys";
|
11 |
|
12 | export type unstable_FormLabelOptions<
|
13 | V,
|
14 | P extends DeepPath<V, P>
|
15 | > = RoleOptions &
|
16 | Pick<unstable_FormStateReturn<V>, "baseId" | "values"> & {
|
17 | |
18 |
|
19 |
|
20 | name: P;
|
21 | |
22 |
|
23 |
|
24 | label?: any;
|
25 | };
|
26 |
|
27 | export type unstable_FormLabelHTMLProps = RoleHTMLProps &
|
28 | React.LabelHTMLAttributes<any>;
|
29 |
|
30 | export type unstable_FormLabelProps<
|
31 | V,
|
32 | P extends DeepPath<V, P>
|
33 | > = unstable_FormLabelOptions<V, P> & unstable_FormLabelHTMLProps;
|
34 |
|
35 | export 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 |
|
56 | export 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;
|