-
Notifications
You must be signed in to change notification settings - Fork 53
/
Tabs.tsx
98 lines (87 loc) · 2.13 KB
/
Tabs.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
import { createContext, useContext, useState } from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import { cn } from '../lib/utils';
interface TabsContextProps {
activeTab: string;
setActiveTab: (id: string) => void;
}
const TabsContext = createContext<TabsContextProps>({
activeTab: '',
setActiveTab: () => {},
});
interface TabsProps {
defaultValue: string;
children: React.ReactNode;
}
function Tabs({ defaultValue, children }: TabsProps) {
const [activeTab, setActiveTab] = useState(defaultValue);
return (
<TabsContext.Provider value={{ activeTab, setActiveTab }}>
{children}
</TabsContext.Provider>
);
}
function TabsList({
className,
...props
}: React.ComponentPropsWithoutRef<typeof View>) {
return (
<View
className={cn('flex flex-row justify-center', className)}
{...props}
/>
);
}
interface TabsTriggerProps
extends React.ComponentPropsWithoutRef<typeof TouchableOpacity> {
value: string;
title: string;
textClasses?: string;
}
function TabsTrigger({
value,
title,
className,
textClasses,
...props
}: TabsTriggerProps) {
const { activeTab, setActiveTab } = useContext(TabsContext);
return (
<TouchableOpacity
className={cn('px-8 py-3 rounded-md w-1/2 bg-muted', {
'bg-foreground': activeTab === value,
className,
})}
onPress={() => setActiveTab(value)}
{...props}
>
<Text
className={cn(
'font-medium text-center text-muted-foreground',
{ 'text-background': activeTab === value },
textClasses
)}
>
{title}
</Text>
</TouchableOpacity>
);
}
interface TabsContentProps extends React.ComponentPropsWithoutRef<typeof View> {
value: string;
}
function TabsContent({ value, className, ...props }: TabsContentProps) {
const { activeTab } = useContext(TabsContext);
if (value === activeTab)
return (
<View
className={cn(
'border border-border mt-2 px-4 py-4 rounded-xl',
className
)}
{...props}
/>
);
return null;
}
export { Tabs, TabsList, TabsTrigger, TabsContent };