-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckButton.tsx
96 lines (88 loc) · 2.42 KB
/
CheckButton.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
import * as React from 'react';
import { Pressable } from 'react-native';
import { getScaleFactor } from '../styles/createScaleFactor';
import { LocalIcon } from './Icon';
export type RadioButtonProps = {
size?: number;
paddingColor?: string;
enabledColor?: string;
disabledColor?: string;
borderColor?: string;
checked?: boolean;
onChecked?: (checked: boolean) => void;
disabled?: boolean;
};
const sf = getScaleFactor();
export default function CheckButton(props: RadioButtonProps): JSX.Element {
// console.log('test:CheckButton:');
const {
size,
paddingColor,
enabledColor,
disabledColor,
borderColor,
checked,
onChecked,
disabled,
} = props;
const [_checked, setChecked] = React.useState(checked ?? false);
// console.log('test:CheckButton:', _checked);
const [hover, setHover] = React.useState(false);
const _delayTime = 1;
const hitSlop = 1;
const cv = React.useMemo(() => {
return {
container: {
padding: size ? sf((6 / 20) * size) : sf(6),
},
outer: {
size: size ? sf(size) : sf(20),
borderRadius: size ? sf(size / 2) : sf(10),
borderWidth: size ? sf(size / 10) : sf(2),
padding: size ? sf((6 / 20) * size) : sf(6),
},
inner: {
size: size ? sf((12 / 20) * size) : sf(12),
borderRadius: size ? sf((6 / 20) * size) : sf(6),
},
};
}, [size]);
const cc = React.useMemo(() => {
return {
paddingColor: paddingColor ? paddingColor : 'rgba(5, 95, 255, 0.1)',
enabledColor: enabledColor ? enabledColor : 'blue',
disabledColor: disabledColor ? disabledColor : 'grey',
borderColor: borderColor ? borderColor : 'white',
};
}, [paddingColor, enabledColor, disabledColor, borderColor]);
const _onCheck = () => {
const c = !_checked;
setChecked(c);
onChecked?.(c);
};
return (
<Pressable
style={[
{
padding: cv.container.padding,
backgroundColor: hover ? cc.paddingColor : undefined,
},
]}
onPress={disabled === true ? undefined : _onCheck}
delayHoverIn={_delayTime}
delayHoverOut={_delayTime}
onHoverIn={() => {
setHover(true);
}}
onHoverOut={() => {
setHover(false);
}}
hitSlop={hitSlop}
>
<LocalIcon
name={_checked ? 'check_enabled' : 'check_disabled'}
size={sf(cv.outer.size)}
/>
</Pressable>
);
}