forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
103 lines (98 loc) · 3.13 KB
/
index.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
99
100
101
102
103
import { useLocalSearchParams } from 'expo-router';
import { Drawer } from 'expo-router/drawer';
import React from 'react';
import { ScrollView, Text, View } from 'react-native';
import { Button } from '~/components/ui/button';
import { Tabs, TabsView, type RenderTabsViewProps } from '~/components/ui/tabs';
import { Alert, AlertDescription, AlertTitle } from '~/components/ui/alert';
const DATA = ['Blue', 'Red', 'Green', 'Orange', 'Purple', 'Fuchsia'];
export default function TabsScreen() {
const tabsRef = React.useRef<React.ElementRef<typeof Tabs>>(null);
const params = useLocalSearchParams<{ active?: string }>();
const renderTabs = React.useCallback(
(props: RenderTabsViewProps) => {
function scrollToRandomTab() {
let index = Math.floor(Math.random() * DATA.length);
if (index === props.index) {
index = Math.floor(Math.random() * DATA.length);
}
tabsRef.current?.scrollToIndex({ index, animated: true });
}
switch (props.item) {
case 'Blue':
return (
<CustomTabsView {...props} scrollToRandomTab={scrollToRandomTab} />
);
case 'Red':
return (
<CustomTabsView {...props} scrollToRandomTab={scrollToRandomTab} />
);
case 'Green':
return (
<CustomTabsView {...props} scrollToRandomTab={scrollToRandomTab} />
);
case 'Orange':
return (
<CustomTabsView {...props} scrollToRandomTab={scrollToRandomTab} />
);
case 'Purple':
return (
<CustomTabsView {...props} scrollToRandomTab={scrollToRandomTab} />
);
case 'Fuchsia':
return (
<CustomTabsView {...props} scrollToRandomTab={scrollToRandomTab} />
);
default:
return null;
}
},
[tabsRef.current]
);
return (
<>
<Drawer.Screen
options={{ headerStyle: { shadowColor: 'transparent' } }}
/>
<Tabs
ref={tabsRef}
tabs={DATA}
renderTabs={renderTabs}
onTabChange={(tab) => {
console.log('Active tab:', tab.item);
}}
initialIndex={DATA.indexOf(params?.active ?? 'Blue')}
/>
</>
);
}
function CustomTabsView(
props: RenderTabsViewProps & { scrollToRandomTab: () => void }
) {
return (
<TabsView {...props}>
<ScrollView contentContainerClassName='flex-1'>
<View className='flex-1 justify-center items-center gap-6'>
<Text
className='text-3xl font-bold'
style={{ color: props.item.toLowerCase() }}
>
{props.item}
</Text>
<Button variant='link' onPress={props.scrollToRandomTab}>
Scroll to random tab
</Button>
</View>
<View className='p-4 w-full'>
<Alert icon='Code' className='max-w-xl'>
<AlertTitle>FYI</AlertTitle>
<AlertDescription>
This reusable does not use "rn-primitives"
</AlertDescription>
</Alert>
</View>
<View style={{ height: 50 }} />
</ScrollView>
</TabsView>
);
}