forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.tsx
51 lines (45 loc) · 1.37 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
51
import React from 'react';
import { Check } from 'lucide-react-native';
import { cn } from '~/lib/utils';
import { Pressable, View } from 'react-native';
import Animated, { FadeIn, FadeOut } from 'react-native-reanimated';
interface CheckboxProps {
value: boolean;
onChange: (checked: boolean) => void;
iconClass?: string;
iconSize?: number;
}
const AnimatedCheck = Animated.createAnimatedComponent(Check);
const Checkbox = React.forwardRef<
React.ElementRef<typeof Pressable>,
Omit<React.ComponentPropsWithoutRef<typeof Pressable>, 'onPress'> &
CheckboxProps
>(({ className, value, onChange, iconClass, iconSize = 16, ...props }, ref) => {
return (
<Pressable
ref={ref}
role='checkbox'
accessibilityState={{ checked: value }}
className={cn(
'peer h-7 w-7 shrink-0 flex items-center bg-card justify-center rounded-md border border-primary ring-offset-background disabled:cursor-not-allowed disabled:opacity-50',
className
)}
onPress={() => {
onChange(!value);
}}
{...props}
>
<View />
{value && (
<AnimatedCheck
entering={FadeIn.duration(200)}
exiting={FadeOut.duration(200)}
size={iconSize}
className={cn('text-foreground', iconClass)}
/>
)}
</Pressable>
);
});
Checkbox.displayName = 'Checkbox';
export { Checkbox };