|
1 | | -import * as React from "react"; |
2 | | -import { View, Text, Pressable, Platform } from "react-native"; |
3 | 1 | import { cn } from "@/lib/utils"; |
| 2 | +import * as React from "react"; |
| 3 | +import { Platform, Pressable, Text, View } from "react-native"; |
4 | 4 |
|
5 | 5 | interface TabsProps { |
6 | | - defaultValue?: string; |
7 | | - value?: string; |
8 | | - onValueChange?: (value: string) => void; |
9 | | - children: React.ReactNode; |
10 | | - className?: string; |
| 6 | + defaultValue?: string; |
| 7 | + value?: string; |
| 8 | + onValueChange?: (value: string) => void; |
| 9 | + children: React.ReactNode; |
| 10 | + className?: string; |
11 | 11 | } |
12 | 12 |
|
13 | 13 | interface TabsListProps { |
14 | | - children: React.ReactNode; |
15 | | - className?: string; |
| 14 | + children: React.ReactNode; |
| 15 | + className?: string; |
16 | 16 | } |
17 | 17 |
|
18 | 18 | interface TabsTriggerProps { |
19 | | - value: string; |
20 | | - children: React.ReactNode; |
21 | | - className?: string; |
| 19 | + value: string; |
| 20 | + children: React.ReactNode; |
| 21 | + className?: string; |
22 | 22 | } |
23 | 23 |
|
24 | 24 | interface TabsContentProps { |
25 | | - value: string; |
26 | | - children: React.ReactNode; |
27 | | - className?: string; |
| 25 | + value: string; |
| 26 | + children: React.ReactNode; |
| 27 | + className?: string; |
28 | 28 | } |
29 | 29 |
|
30 | 30 | const TabsContext = React.createContext<{ |
31 | | - value: string; |
32 | | - onValueChange: (value: string) => void; |
| 31 | + value: string; |
| 32 | + onValueChange: (value: string) => void; |
33 | 33 | }>({ |
34 | | - value: "", |
35 | | - onValueChange: () => { }, |
| 34 | + value: "", |
| 35 | + onValueChange: () => {}, |
36 | 36 | }); |
37 | 37 |
|
38 | 38 | const Tabs = React.forwardRef<View, TabsProps>( |
39 | | - ({ defaultValue, value, onValueChange, children, className }, ref) => { |
40 | | - const [selectedValue, setSelectedValue] = React.useState( |
41 | | - value || defaultValue || "" |
42 | | - ); |
43 | | - |
44 | | - const handleValueChange = React.useCallback( |
45 | | - (newValue: string) => { |
46 | | - setSelectedValue(newValue); |
47 | | - onValueChange?.(newValue); |
48 | | - }, |
49 | | - [onValueChange] |
50 | | - ); |
51 | | - |
52 | | - return ( |
53 | | - <TabsContext.Provider |
54 | | - value={{ |
55 | | - value: selectedValue, |
56 | | - onValueChange: handleValueChange, |
57 | | - }} |
58 | | - > |
59 | | - <View ref={ref} className={cn("w-full", className)}> |
60 | | - {children} |
61 | | - </View> |
62 | | - </TabsContext.Provider> |
63 | | - ); |
64 | | - } |
| 39 | + ({ defaultValue, value, onValueChange, children, className }, ref) => { |
| 40 | + const [selectedValue, setSelectedValue] = React.useState( |
| 41 | + value || defaultValue || "", |
| 42 | + ); |
| 43 | + |
| 44 | + const handleValueChange = React.useCallback( |
| 45 | + (newValue: string) => { |
| 46 | + setSelectedValue(newValue); |
| 47 | + onValueChange?.(newValue); |
| 48 | + }, |
| 49 | + [onValueChange], |
| 50 | + ); |
| 51 | + |
| 52 | + return ( |
| 53 | + <TabsContext.Provider |
| 54 | + value={{ |
| 55 | + value: selectedValue, |
| 56 | + onValueChange: handleValueChange, |
| 57 | + }} |
| 58 | + > |
| 59 | + <View ref={ref} className={cn("flex-1 w-full", className)}> |
| 60 | + {children} |
| 61 | + </View> |
| 62 | + </TabsContext.Provider> |
| 63 | + ); |
| 64 | + }, |
65 | 65 | ); |
66 | 66 |
|
67 | 67 | const TabsList = React.forwardRef<View, TabsListProps>( |
68 | | - ({ children, className }, ref) => { |
69 | | - return ( |
70 | | - <View |
71 | | - ref={ref} |
72 | | - className={cn( |
73 | | - "flex-row items-center justify-center rounded-xl bg-muted p-1", |
74 | | - Platform.OS === "ios" ? "h-12" : "h-14", |
75 | | - className |
76 | | - )} |
77 | | - > |
78 | | - {children} |
79 | | - </View> |
80 | | - ); |
81 | | - } |
| 68 | + ({ children, className }, ref) => { |
| 69 | + return ( |
| 70 | + <View |
| 71 | + ref={ref} |
| 72 | + className={cn( |
| 73 | + "flex-row items-center justify-center rounded-xl bg-muted p-1", |
| 74 | + Platform.OS === "ios" ? "h-12" : "h-14", |
| 75 | + className, |
| 76 | + )} |
| 77 | + > |
| 78 | + {children} |
| 79 | + </View> |
| 80 | + ); |
| 81 | + }, |
82 | 82 | ); |
83 | 83 |
|
84 | 84 | const TabsTrigger = React.forwardRef<View, TabsTriggerProps>( |
85 | | - ({ value, children, className }, ref) => { |
86 | | - const { value: selectedValue, onValueChange } = |
87 | | - React.useContext(TabsContext); |
88 | | - const isSelected = selectedValue === value; |
89 | | - |
90 | | - return ( |
91 | | - <Pressable |
92 | | - ref={ref} |
93 | | - onPress={() => onValueChange(value)} |
94 | | - className={cn( |
95 | | - "flex-1 items-center justify-center rounded-lg px-4 py-2", |
96 | | - Platform.OS === "ios" ? "h-10" : "h-12", |
97 | | - isSelected ? "bg-background" : "bg-transparent", |
98 | | - className |
99 | | - )} |
100 | | - > |
101 | | - <Text |
102 | | - className={cn( |
103 | | - "text-base font-medium", |
104 | | - isSelected ? "text-foreground" : "text-muted-foreground" |
105 | | - )} |
106 | | - > |
107 | | - {children} |
108 | | - </Text> |
109 | | - </Pressable> |
110 | | - ); |
111 | | - } |
| 85 | + ({ value, children, className }, ref) => { |
| 86 | + const { value: selectedValue, onValueChange } = |
| 87 | + React.useContext(TabsContext); |
| 88 | + const isSelected = selectedValue === value; |
| 89 | + |
| 90 | + return ( |
| 91 | + <Pressable |
| 92 | + ref={ref} |
| 93 | + onPress={() => onValueChange(value)} |
| 94 | + className={cn( |
| 95 | + "flex-1 items-center justify-center rounded-lg px-4 py-2", |
| 96 | + Platform.OS === "ios" ? "h-10" : "h-12", |
| 97 | + isSelected ? "bg-background" : "bg-transparent", |
| 98 | + className, |
| 99 | + )} |
| 100 | + > |
| 101 | + <Text |
| 102 | + className={cn( |
| 103 | + "text-base font-medium", |
| 104 | + isSelected ? "text-foreground" : "text-muted-foreground", |
| 105 | + )} |
| 106 | + > |
| 107 | + {children} |
| 108 | + </Text> |
| 109 | + </Pressable> |
| 110 | + ); |
| 111 | + }, |
112 | 112 | ); |
113 | 113 |
|
114 | 114 | const TabsContent = React.forwardRef<View, TabsContentProps>( |
115 | | - ({ value, children, className }, ref) => { |
116 | | - const { value: selectedValue } = React.useContext(TabsContext); |
117 | | - const isSelected = selectedValue === value; |
118 | | - |
119 | | - if (!isSelected) return null; |
120 | | - |
121 | | - return ( |
122 | | - <View ref={ref} className={cn("mt-4", className)}> |
123 | | - {children} |
124 | | - </View> |
125 | | - ); |
126 | | - } |
| 115 | + ({ value, children, className }, ref) => { |
| 116 | + const { value: selectedValue } = React.useContext(TabsContext); |
| 117 | + const isSelected = selectedValue === value; |
| 118 | + |
| 119 | + if (!isSelected) return null; |
| 120 | + |
| 121 | + return ( |
| 122 | + <View ref={ref} className={cn("flex-1 mt-4", className)}> |
| 123 | + {children} |
| 124 | + </View> |
| 125 | + ); |
| 126 | + }, |
127 | 127 | ); |
128 | 128 |
|
129 | 129 | Tabs.displayName = "Tabs"; |
130 | 130 | TabsList.displayName = "TabsList"; |
131 | 131 | TabsTrigger.displayName = "TabsTrigger"; |
132 | 132 | TabsContent.displayName = "TabsContent"; |
133 | 133 |
|
134 | | -export { Tabs, TabsList, TabsTrigger, TabsContent }; |
| 134 | +export { Tabs, TabsContent, TabsList, TabsTrigger }; |
0 commit comments