-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
83 lines (70 loc) · 2.37 KB
/
index.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
import React from 'react';
import uniqid from 'uniqid';
import styled from '../../../';
const checkboxSvg =
"'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 9.862 8.198%22%3E%3Cpath fill=%22%23fff%22 d=%22M8.972.188l-5.364 6.72L.883 3.659a.5.5 0 1 0-.766.642l3.117 3.718a.502.502 0 0 0 .383.179h.006a.502.502 0 0 0 .385-.188L9.753.812A.498.498 0 0 0 9.674.11a.498.498 0 0 0-.702.078z%22/%3E%3C/svg%3E%0A'";
const CheckboxContainer = styled.div<{ disabled?: boolean }>(({ disabled }) => ({
display: 'flex',
marginBottom: 8,
opacity: disabled ? 0.5 : 1,
}));
const Input = styled.input(({ theme }) => ({
appearance: 'none',
position: 'relative',
background: 'none',
border: '1px solid currentColor',
width: 20,
height: 20,
flexShrink: 0,
marginRight: '1em',
transition: 'border-color 150ms, background 150ms, opacity 150ms',
borderRadius: theme.borderRadius.small,
'&:after': {
content: '""',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundRepeat: 'no-repeat',
backgroundPosition: '50%',
backgroundSize: '75%',
transition: 'background 150ms',
},
'&:checked:after': {
backgroundImage: `url(${checkboxSvg})`,
},
'&:checked': {
backgroundColor: theme.colors.purple,
borderColor: theme.colors.purple,
},
'&:disabled': {
opacity: 0.6,
borderColor: theme.colors.darkGray,
},
}));
export interface CheckboxProps {
label: string;
name?: string;
checked?: boolean;
disabled?: boolean;
onChange: (event: React.FormEvent<HTMLInputElement>) => void;
}
export interface CheckboxState {
id: string;
}
export class Checkbox extends React.Component<CheckboxProps, CheckboxState> {
static displayName = 'Collector.Checkbox';
state: CheckboxState = {
id: uniqid(),
};
render() {
const { label, name, checked, disabled, onChange, ...rest } = this.props;
return (
<CheckboxContainer disabled={disabled} {...rest}>
<Input type="checkbox" id={this.state.id} name={name} checked={checked} disabled={disabled} onChange={onChange} />
<label htmlFor={this.state.id} dangerouslySetInnerHTML={{ __html: label }} />
</CheckboxContainer>
);
}
}