-
Notifications
You must be signed in to change notification settings - Fork 9
/
Checkbox.tsx
136 lines (130 loc) · 3.49 KB
/
Checkbox.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { type ReactElement, useState } from "react";
import classNames from "classnames";
import useFocusVisible from "../useFocusVisible";
import styles from "./Checkbox.module.css";
import focusStyles from "../Focus.module.css";
import Typography from "../Typography/Typography";
import useIsHydrated from "../useIsHydrated";
const typographySize = {
sm: 100,
md: 200,
} as const;
const iconWidth = {
sm: 12,
md: 20,
};
/**
* [Checkbox](https://cambly-syntax.vercel.app/?path=/docs/components-checkbox--docs) is a clickable element that will show if an option has been selected or not.
*/
const Checkbox = ({
checked = false,
"data-testid": dataTestId,
disabled: disabledProp = false,
size = "md",
label,
error = false,
onChange,
}: {
/**
* Whether or not the box has been clicked
*
* @defaultValue false
*/
checked: boolean;
/**
* Test id for the checkbox
*/
"data-testid"?: string;
/**
* The callback to be called when the checkbox value changes
*/
onChange: React.ChangeEventHandler<HTMLInputElement>;
/**
* Whether or not the box is disabled
*
* @defaultValue false
*/
disabled?: boolean;
/**
* The size of the checkbox and icon
*
* * `sm`: 16px
* * `md`: 24px
*
* @defaultValue "md"
*/
size?: "sm" | "md";
/**
* The text accompanying the checkbox
*/
label: string;
/**
* Whether or not there is an error with the input
*
* @defaultValue false
*/
error?: boolean;
}): ReactElement => {
const isHydrated = useIsHydrated();
const disabled = !isHydrated || disabledProp;
const [isFocused, setIsFocused] = useState(false);
const { isFocusVisible } = useFocusVisible();
const checkboxStyling = classNames(styles.checkbox, styles[size]);
const uncheckedStyling = classNames(checkboxStyling, styles.uncheckedBox, {
[styles.uncheckedBorder]: !error,
[styles.uncheckedErrorBorder]: error,
[focusStyles.accessibilityOutlineFocus]: isFocused && isFocusVisible,
});
const checkedStyling = classNames(checkboxStyling, styles.checkedBox, {
[styles.checkedNonError]: !error,
[styles.checkedError]: error,
[focusStyles.accessibilityOutlineFocus]: isFocused && isFocusVisible,
});
return (
<label
className={classNames(
styles.mainContainer,
styles[`cursor${disabled ? "Disabled" : "Enabled"}`],
{
[styles.disabled]: disabled,
},
)}
>
<div className={checked ? checkedStyling : uncheckedStyling}>
{checked && (
<svg aria-hidden="true" viewBox="0 0 24 24" width={iconWidth[size]}>
<path
fill="#fff"
d="m9 16.2-3.5-3.5a.9839.9839 0 0 0-1.4 0c-.39.39-.39 1.01 0 1.4l4.19 4.19c.39.39 1.02.39 1.41 0L20.3 7.7c.39-.39.39-1.01 0-1.4a.9839.9839 0 0 0-1.4 0L9 16.2z"
></path>
</svg>
)}
</div>
<input
data-testid={dataTestId}
type="checkbox"
className={classNames(
styles.inputOverlay,
styles[size],
styles[`cursor${disabled ? "Disabled" : "Enabled"}`],
)}
checked={checked}
onChange={onChange}
disabled={disabled}
onFocus={() => {
setIsFocused(true);
}}
onBlur={() => {
setIsFocused(false);
}}
/>
<Typography
size={typographySize[size]}
color={error ? "destructive-primary" : "gray900"}
>
{label}
</Typography>
</label>
);
};
export default Checkbox;