This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
overlay-checkbox.tsx
152 lines (137 loc) · 3.6 KB
/
overlay-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
/**
* @module @edtr-io/editor-ui
*/
/** Comment needed because of https://github.com/christopherthielen/typedoc-plugin-external-module-name/issues/337 */
import {
createEditorUiTheme,
CheckboxTheme,
EditorThemeProps,
styled
} from '@edtr-io/ui'
import * as React from 'react'
import { createOverlayTheme } from './settings-overlay'
export const createOverlayCheckboxTheme = createEditorUiTheme<CheckboxTheme>(
theme => {
return {
boxSelectedColor: theme.color,
boxDeselectedColor: theme.backgroundColor,
color: theme.color
}
}
)
const CheckboxLabel = styled.label((props: EditorThemeProps) => {
const theme = createOverlayTheme(props.theme)
return {
color: theme.checkbox.color,
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
marginTop: '20px'
}
})
const CheckboxInlineLabel = styled.label((props: EditorThemeProps) => {
const theme = createOverlayTheme(props.theme)
return {
color: theme.checkbox.color,
verticalAlign: 'middle',
margin: '5px 10px',
display: 'inline-block'
}
})
const CheckboxInlineLabelInner = styled.span({
marginRight: '10px',
verticalAlign: 'middle'
})
const CheckboxToggleContainer = styled.div<{
value?: boolean
}>(({ value, ...props }: { value?: boolean } & EditorThemeProps) => {
const theme = createOverlayTheme(props.theme)
return {
cursor: 'pointer',
border: `2px solid ${theme.checkbox.color}`,
borderRadius: '15%',
width: '20px',
height: '20px',
display: 'inline-block',
verticalAlign: 'middle',
backgroundColor: value
? theme.checkbox.boxSelectedColor
: theme.checkbox.boxDeselectedColor
}
})
const CheckboxLabelInner = styled.span({ width: '20%' })
const CheckboxToggle = styled.div<{ value?: boolean }>(
({ value, ...props }: { value?: boolean } & EditorThemeProps) => {
const theme = createOverlayTheme(props.theme)
return {
opacity: value ? 1 : 0,
content: '',
position: 'absolute',
fontWeight: 'bold',
width: '20px',
height: '10px',
border: `3px solid ${theme.checkbox.boxDeselectedColor}`,
borderTop: 'none',
borderRight: 'none',
borderRadius: '1px',
transform: 'rotate(-45deg)',
zIndex: 1000
}
}
)
const CheckboxInner = styled.div({
width: '75%',
textAlign: 'left'
})
export class Checkbox extends React.Component<CheckboxProps> {
public render() {
const { checked, onChange, label } = this.props
return (
<CheckboxLabel>
<CheckboxLabelInner>{label}</CheckboxLabelInner>
<CheckboxInner>
<CheckboxToggleContainer
onClick={() => {
if (onChange) {
onChange(!checked)
}
}}
value={checked}
>
<CheckboxToggle value={checked} />
</CheckboxToggleContainer>
</CheckboxInner>
</CheckboxLabel>
)
}
}
export const InlineCheckbox: React.FunctionComponent<CheckboxProps> = ({
checked,
onChange,
label
}) => {
return (
<CheckboxInlineLabel>
<CheckboxInlineLabelInner>{label}</CheckboxInlineLabelInner>
<CheckboxToggleContainer
onMouseDown={e => {
// avoid loosing focus
e.stopPropagation()
}}
onClick={() => {
if (onChange) {
onChange(!checked)
}
}}
value={checked}
>
<CheckboxToggle value={checked} />
</CheckboxToggleContainer>
</CheckboxInlineLabel>
)
}
export interface CheckboxProps {
checked?: boolean
onChange?: (checked: boolean) => void
label?: string
}