-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToggle.mjs
95 lines (85 loc) · 2.81 KB
/
Toggle.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// @ts-check
import classNameProp from "class-name-prop";
import React from "react";
import IconTick, { css as cssIconTick } from "./IconTick.mjs";
import useCustomValidity from "./useCustomValidity.mjs";
import useMergedRef from "./useMergedRef.mjs";
import useOnFocusReportValidity from "./useOnFocusReportValidity.mjs";
/** CSS dependency URLs for the React component {@linkcode Toggle}. */
export const css = new Set([
...cssIconTick,
new URL("./Toggle.css", import.meta.url).href,
]);
/** React component for a checkbox or radio {@link HTMLInputElement input}. */
const Toggle = React.forwardRef(
(
/**
* @type {ToggleProps
* & React.ComponentPropsWithoutRef<"label">
* & { [dataAttribute: `data-${string}`]: unknown }}
*/
{
validationMessage,
inputProps: { className: inputClassName, ...inputProps },
className,
children,
...props
},
/** @type {React.ForwardedRef<HTMLLabelElement>} */
ref
) => {
const inputRef = useMergedRef([inputProps.ref]);
useCustomValidity(inputRef, validationMessage);
useOnFocusReportValidity(inputRef);
return React.createElement(
"label",
{
className: classNameProp("daui-Toggle", className),
...props,
ref,
},
React.createElement(
"span",
{ className: "daui-Toggle__toggle" },
React.createElement("input", {
className: classNameProp("daui-Toggle__input", inputClassName),
...inputProps,
ref: inputRef,
}),
React.createElement(
"span",
{ className: "daui-Toggle__tick" },
React.createElement(
"span",
{ className: "daui-Toggle__icon" },
React.createElement(IconTick)
)
)
),
children &&
React.createElement(
"span",
{ className: "daui-Toggle__label" },
children
)
);
}
);
Toggle.displayName = "Toggle";
export default Toggle;
/**
* Props for the {@linkcode Toggle} React component, excluding additional props
* for the {@linkcode HTMLLabelElement} container.
* @typedef {object} ToggleProps
* @prop {string} [validationMessage] Sets the {@linkcode HTMLInputElement}
* [`validationMessage`](https://html.spec.whatwg.org/dev/form-control-infrastructure.html#dom-cva-validationmessage).
* Defaults to `""`.
* @prop {React.ComponentPropsWithRef<"input">
* & { [dataAttribute: `data-${string}`]: unknown }
* & { type: "checkbox" | "radio" }} inputProps Props for the
* {@linkcode HTMLInputElement}.
* @prop {React.ReactNode} [children] Label children. Due to the ancestor
* {@linkcode HTMLLabelElement}, it may only contain
* [phrasing content](https://html.spec.whatwg.org/dev/dom.html#phrasing-content)
* except {@linkcode HTMLLabelElement}.
*/