-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathCheckbox.tsx
More file actions
200 lines (181 loc) · 4.75 KB
/
Copy pathCheckbox.tsx
File metadata and controls
200 lines (181 loc) · 4.75 KB
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
import React, { forwardRef, useCallback } from 'react';
import styled, { css } from 'styled-components';
import { createHatchedBackground } from '../common';
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
import {
LabelText,
size,
StyledInput,
StyledLabel
} from '../common/SwitchBase';
import { noOp } from '../common/utils';
import { StyledScrollView } from '../ScrollView/ScrollView';
import { CommonThemeProps } from '../types';
type CheckboxProps = {
checked?: boolean;
className?: string;
defaultChecked?: boolean;
disabled?: boolean;
indeterminate?: boolean;
label?: number | string;
name?: string;
onChange?: React.ChangeEventHandler<HTMLInputElement>;
style?: React.CSSProperties;
value?: number | string;
variant?: 'default' | 'flat';
} & Omit<
React.InputHTMLAttributes<HTMLInputElement>,
| 'checked'
| 'className'
| 'defaultChecked'
| 'disabled'
| 'label'
| 'name'
| 'onChange'
| 'style'
| 'value'
>;
type CheckmarkProps = {
$disabled: boolean;
variant: 'default' | 'flat';
};
const sharedCheckboxStyles = css`
width: ${size}px;
height: ${size}px;
display: flex;
align-items: center;
justify-content: space-around;
margin-right: 0.5rem;
`;
const StyledCheckbox = styled(StyledScrollView)<CommonThemeProps>`
${sharedCheckboxStyles}
width: ${size}px;
height: ${size}px;
background: ${({ $disabled, theme }) =>
$disabled ? theme.material : theme.canvas};
&:before {
box-shadow: none;
}
`;
const StyledFlatCheckbox = styled.div<CommonThemeProps>`
position: relative;
box-sizing: border-box;
display: inline-block;
background: ${({ $disabled, theme }) =>
$disabled ? theme.flatLight : theme.canvas};
${sharedCheckboxStyles}
width: ${size - 4}px;
height: ${size - 4}px;
outline: none;
border: 2px solid ${({ theme }) => theme.flatDark};
background: ${({ $disabled, theme }) =>
$disabled ? theme.flatLight : theme.canvas};
`;
const CheckmarkIcon = styled.span.attrs(() => ({
'data-testid': 'checkmarkIcon'
}))<CheckmarkProps>`
display: inline-block;
position: relative;
width: 100%;
height: 100%;
&:after {
content: '';
display: block;
position: absolute;
left: 50%;
top: calc(50% - 1px);
width: 3px;
height: 7px;
border: solid
${({ $disabled, theme }) =>
$disabled ? theme.checkmarkDisabled : theme.checkmark};
border-width: 0 3px 3px 0;
transform: translate(-50%, -50%) rotate(45deg);
border-color: ${p =>
p.$disabled ? p.theme.checkmarkDisabled : p.theme.checkmark};
}
`;
const IndeterminateIcon = styled.span.attrs(() => ({
'data-testid': 'indeterminateIcon'
}))<CheckmarkProps>`
display: inline-block;
position: relative;
width: 100%;
height: 100%;
&:after {
content: '';
display: block;
width: 100%;
height: 100%;
${({ $disabled, theme }) =>
createHatchedBackground({
mainColor: $disabled ? theme.checkmarkDisabled : theme.checkmark
})}
background-position: 0px 0px, 2px 2px;
}
`;
const CheckboxComponents = {
flat: StyledFlatCheckbox,
default: StyledCheckbox
};
const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
(
{
checked,
className = '',
defaultChecked = false,
disabled = false,
indeterminate = false,
label = '',
onChange = noOp,
style = {},
value,
variant = 'default',
...otherProps
},
ref
) => {
const [state, setState] = useControlledOrUncontrolled({
defaultValue: defaultChecked,
onChange,
readOnly: otherProps.readOnly ?? disabled,
value: checked
});
const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const newState = e.target.checked;
setState(newState);
onChange(e);
},
[onChange, setState]
);
const CheckboxComponent = CheckboxComponents[variant];
let Icon = null;
if (indeterminate) {
Icon = IndeterminateIcon;
} else if (state) {
Icon = CheckmarkIcon;
}
return (
<StyledLabel $disabled={disabled} className={className} style={style}>
<StyledInput
disabled={disabled}
onChange={disabled ? undefined : handleChange}
readOnly={disabled}
type='checkbox'
value={value}
checked={state}
data-indeterminate={indeterminate}
ref={ref}
{...otherProps}
/>
<CheckboxComponent $disabled={disabled} role='presentation'>
{Icon && <Icon $disabled={disabled} variant={variant} />}
</CheckboxComponent>
{label && <LabelText>{label}</LabelText>}
</StyledLabel>
);
}
);
Checkbox.displayName = 'Checkbox';
export { Checkbox, CheckboxProps };