/
Checkbox.tsx
237 lines (218 loc) · 7.12 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import * as React from 'react';
import PropTypes from 'prop-types';
import {useUID} from '@twilio-paste/uid-library';
import {Box, safelySpreadBoxProps} from '@twilio-paste/box';
import type {BoxProps} from '@twilio-paste/box';
import {CheckboxCheckIcon} from '@twilio-paste/icons/esm/CheckboxCheckIcon';
import {MinusIcon} from '@twilio-paste/icons/esm/MinusIcon';
import {
BaseRadioCheckboxControl,
BaseRadioCheckboxLabel,
BaseRadioCheckboxLabelText,
BaseRadioCheckboxHelpText,
} from '@twilio-paste/base-radio-checkbox';
import {MediaObject, MediaFigure, MediaBody} from '@twilio-paste/media-object';
import {RequiredDot} from '@twilio-paste/label';
import {CheckboxContext} from './CheckboxContext';
const selectAllStyleProps = {
paddingTop: 'space20',
paddingRight: 'space30',
paddingBottom: 'space20',
paddingLeft: 'space30',
borderRadius: 'borderRadius10',
backgroundColor: 'colorBackground',
};
const selectAllActiveStyleProps = {
...selectAllStyleProps,
backgroundColor: 'colorBackground',
};
const selectAllChildStyleProps = {
paddingLeft: 'space30',
paddingRight: 'space30',
};
export interface CheckboxProps extends React.InputHTMLAttributes<HTMLInputElement>, Pick<BoxProps, 'element'> {
children: NonNullable<React.ReactNode>;
hasError?: boolean;
helpText?: string | React.ReactNode;
id?: string;
indeterminate?: boolean;
isSelectAll?: boolean;
isSelectAllChild?: boolean;
checked?: boolean;
defaultChecked?: boolean;
}
type HiddenCheckboxProps = Pick<
CheckboxProps,
| 'checked'
| 'defaultChecked'
| 'element'
| 'disabled'
| 'id'
| 'indeterminate'
| 'name'
| 'onChange'
| 'required'
| 'value'
>;
const HiddenCheckbox = React.forwardRef<HTMLInputElement, HiddenCheckboxProps>((props, ref) => (
<Box
{...props}
ref={ref}
as="input"
type="checkbox"
size="size0"
border="none"
overflow="hidden"
padding="space0"
margin="space0"
whiteSpace="nowrap"
textTransform="none"
position="absolute"
clip="rect(0 0 0 0)"
/>
));
HiddenCheckbox.displayName = 'HiddenCheckbox';
const CheckboxIcon: React.FC<{
indeterminate: boolean | undefined;
checked: boolean | undefined;
element: BoxProps['element'];
}> = ({checked, element, indeterminate}) => {
if (indeterminate) {
return <MinusIcon element={element} decorative color="inherit" size="sizeIcon10" />;
}
return (
<CheckboxCheckIcon
element={element}
display={!checked ? 'none' : 'block'}
decorative
color="inherit"
size="sizeIcon10"
/>
);
};
CheckboxIcon.displayName = 'CheckboxIcon';
const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
(
{
checked,
defaultChecked,
element = 'CHECKBOX',
children,
helpText,
id,
indeterminate,
isSelectAll,
isSelectAllChild,
required,
onChange,
...props
},
ref
) => {
if (checked != null && defaultChecked != null) {
throw new Error(
`[Paste Checkbox] Do not provide both 'defaultChecked' and 'checked' to Checkbox at the same time. Please consider if you want this component to be controlled or uncontrolled.`
);
}
/*
* Keeps track of the `checked` state on uncontrolled Checkboxes
* in order to properly render the checkbox icon svg.
*/
const [checkedState, setCheckedState] = React.useState(defaultChecked);
const checkboxGroupContext = React.useContext(CheckboxContext);
const helpTextId = useUID();
const checkboxId = id ? id : useUID();
// We shouldn't change between controlled and uncontrolled after mount, so we memo this for safety
const isControlled = React.useMemo(() => checked !== undefined, []);
// Determines if the checkbox is checked in either controlled or uncontrolled environments
const mergedChecked = isControlled ? checked : checkedState;
const handleChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>): void => {
if (!isControlled) {
setCheckedState(event.target.checked);
} else if (onChange) {
onChange(event);
} else {
checkboxGroupContext.onChange(event);
}
},
[onChange, checkboxGroupContext.onChange]
);
// Prioritizing direct props values over whatever CheckboxGroupContext passes down
const disabled = props.disabled != null ? props.disabled : checkboxGroupContext.disabled;
const name = props.name != null ? props.name : checkboxGroupContext.name;
const hasError = props.hasError != null ? props.hasError : checkboxGroupContext.hasError;
// Custom checkbox styles if selectAll(Child)
let selectAllStyles = {};
if (isSelectAll) {
selectAllStyles = !disabled && (mergedChecked || indeterminate) ? selectAllActiveStyleProps : selectAllStyleProps;
}
if (isSelectAllChild) {
selectAllStyles = selectAllChildStyleProps;
}
return (
<Box element={element} display="inline-flex" position="relative" flexDirection="column" {...selectAllStyles}>
<HiddenCheckbox
{...safelySpreadBoxProps(props)}
checked={checked}
defaultChecked={defaultChecked}
disabled={disabled}
name={name}
onChange={handleChange}
aria-describedby={helpTextId}
aria-checked={indeterminate ? 'mixed' : checked}
aria-invalid={hasError}
id={checkboxId}
required={required}
ref={ref}
/>
<BaseRadioCheckboxLabel disabled={disabled} htmlFor={checkboxId}>
<BaseRadioCheckboxControl
alignItems="center"
borderRadius="borderRadius20"
element={`${element}_CONTROL`}
disabled={disabled}
position="relative"
type="checkbox"
>
<CheckboxIcon element={`${element}_ICON`} checked={mergedChecked} indeterminate={indeterminate} />
</BaseRadioCheckboxControl>
<BaseRadioCheckboxLabelText
element={`${element}_LABEL_TEXT`}
fontWeight={isSelectAll ? null : 'fontWeightMedium'}
>
<MediaObject verticalAlign="top">
{required && (
<MediaFigure spacing="space20">
<RequiredDot />
</MediaFigure>
)}
<MediaBody>{children}</MediaBody>
</MediaObject>
</BaseRadioCheckboxLabelText>
</BaseRadioCheckboxLabel>
{helpText && (
<BaseRadioCheckboxHelpText element={`${element}_HELP_TEXT_WRAPPER`} helpTextId={helpTextId}>
{helpText}
</BaseRadioCheckboxHelpText>
)}
</Box>
);
}
);
Checkbox.displayName = 'Checkbox';
Checkbox.propTypes = {
children: PropTypes.node.isRequired,
disabled: PropTypes.bool,
name: PropTypes.string,
onChange: PropTypes.func,
hasError: PropTypes.bool,
helpText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
id: PropTypes.string,
indeterminate: PropTypes.bool,
isSelectAll: PropTypes.bool,
isSelectAllChild: PropTypes.bool,
element: PropTypes.string,
defaultChecked: PropTypes.bool,
};
export {Checkbox, HiddenCheckbox, CheckboxIcon};