/
Fieldset.tsx
77 lines (70 loc) · 1.78 KB
/
Fieldset.tsx
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
import type { CSSProperties, FieldsetHTMLAttributes, ReactNode } from "react";
import { forwardRef } from "react";
import cn from "classnames";
import { bem } from "@react-md/utils";
export interface FieldsetProps
extends FieldsetHTMLAttributes<HTMLFieldSetElement> {
/**
* The legend to display. This is required since a fieldset loses most of its'
* benefit for accessibility without a legend.
*/
legend: ReactNode;
/**
* An optional style to apply to the legend element.
*/
legendStyle?: CSSProperties;
/**
* An optional classname to apply to the legend.
*/
legendClassName?: string;
/**
* Boolean if the legend should only be styled to be visible for screen
* readers.
*/
legendSROnly?: boolean;
/**
* Boolean if the fieldset should remove the default browser styles of margin,
* padding, and border.
*/
unstyled?: boolean;
}
const block = bem("rmd-fieldset");
/**
* This is a simple wrapper for the `<fieldset>` that defaults to removing
* the default styles of a border, padding, and margin and having a screen-reader
* visible only legend element for added accessibility.
*/
export const Fieldset = forwardRef<HTMLFieldSetElement, FieldsetProps>(
function Fieldset(
{
className,
legend,
legendStyle,
legendClassName,
legendSROnly = false,
unstyled = true,
children,
...props
},
ref
) {
return (
<fieldset
{...props}
ref={ref}
className={cn(block({ unstyled }), className)}
>
<legend
style={legendStyle}
className={cn(
block("legend", { "sr-only": legendSROnly }),
legendClassName
)}
>
{legend}
</legend>
{children}
</fieldset>
);
}
);