-
Notifications
You must be signed in to change notification settings - Fork 52
/
Checkbox.tsx
50 lines (45 loc) · 1.21 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
import { useState } from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import { cn } from '../lib/utils';
// TODO: make controlled (optional)
interface CheckboxProps extends React.ComponentPropsWithoutRef<typeof View> {
label?: string;
labelClasses?: string;
checkboxClasses?: string;
}
function Checkbox({
label,
labelClasses,
checkboxClasses,
className,
...props
}: CheckboxProps) {
const [isChecked, setChecked] = useState(false);
const toggleCheckbox = () => {
setChecked(prev => !prev);
};
return (
<View
className={cn('flex flex-row items-center gap-2', className)}
{...props}
>
<TouchableOpacity onPress={toggleCheckbox}>
<View
className={cn(
'w-4 h-4 border border-gray-700 rounded bg-background flex justify-center items-center',
{
'bg-foreground': isChecked,
},
checkboxClasses
)}
>
{isChecked && <Text className="text-background text-xs">✓</Text>}
</View>
</TouchableOpacity>
{label && (
<Text className={cn('text-primary', labelClasses)}>{label}</Text>
)}
</View>
);
}
export { Checkbox };