|
| 1 | +import { faCircle } from '@fortawesome/free-solid-svg-icons'; |
| 2 | +import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome'; |
| 3 | +import type { BottomTabNavigationOptions } from '@react-navigation/bottom-tabs'; |
| 4 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 5 | +import { useCallback } from 'react'; |
| 6 | +import { StyleSheet, Text } from 'react-native'; |
| 7 | +import type { SortableGridRenderItem } from 'react-native-sortables'; |
| 8 | +import Sortable from 'react-native-sortables'; |
| 9 | + |
| 10 | +import { GridCard, Screen, ScrollScreen } from '@/components'; |
| 11 | +import { colors, spacing, text } from '@/theme'; |
| 12 | + |
| 13 | +const Tab = createBottomTabNavigator(); |
| 14 | + |
| 15 | +const DATA_LENGTH = 36; |
| 16 | +const DATA = Array.from( |
| 17 | + { length: DATA_LENGTH }, |
| 18 | + (_, index) => `Item ${index + 1}` |
| 19 | +); |
| 20 | + |
| 21 | +type GridProps = { |
| 22 | + data: Array<string>; |
| 23 | +}; |
| 24 | + |
| 25 | +function Grid({ data }: GridProps) { |
| 26 | + const renderItem = useCallback<SortableGridRenderItem<string>>( |
| 27 | + ({ item }) => <GridCard>{item}</GridCard>, |
| 28 | + [] |
| 29 | + ); |
| 30 | + |
| 31 | + return ( |
| 32 | + <Sortable.Grid |
| 33 | + columnGap={10} |
| 34 | + columns={3} |
| 35 | + data={data} |
| 36 | + renderItem={renderItem} |
| 37 | + rowGap={10} |
| 38 | + /> |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function Screen1() { |
| 43 | + return ( |
| 44 | + <ScrollScreen contentContainerStyle={styles.scrollContainer}> |
| 45 | + <Text style={styles.screenTitle}>Screen 1 grid:</Text> |
| 46 | + <Grid data={DATA.slice(0, DATA_LENGTH / 2)} /> |
| 47 | + </ScrollScreen> |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +function Screen2() { |
| 52 | + return ( |
| 53 | + <ScrollScreen contentContainerStyle={styles.scrollContainer}> |
| 54 | + <Text style={styles.screenTitle}>Screen 2 grid:</Text> |
| 55 | + <Grid data={DATA.slice(DATA_LENGTH / 2)} /> |
| 56 | + </ScrollScreen> |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +function Screen3() { |
| 61 | + return ( |
| 62 | + <ScrollScreen contentContainerStyle={styles.scrollContainer}> |
| 63 | + <Text style={styles.screenTitle}>Screen without sortable grid</Text> |
| 64 | + </ScrollScreen> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +const tabBarOptions: BottomTabNavigationOptions = { |
| 69 | + tabBarIcon: ({ focused }) => ( |
| 70 | + <FontAwesomeIcon |
| 71 | + color={focused ? colors.primary : colors.foreground3} |
| 72 | + icon={faCircle} |
| 73 | + /> |
| 74 | + ), |
| 75 | + tabBarLabel: ({ children, focused }) => ( |
| 76 | + <Text style={focused ? styles.focusedLabel : styles.label}>{children}</Text> |
| 77 | + ) |
| 78 | +}; |
| 79 | + |
| 80 | +function Tabs() { |
| 81 | + return ( |
| 82 | + <Tab.Navigator |
| 83 | + // This option breaks sortable state when navigating between screens |
| 84 | + // https://github.com/MatiPl01/react-native-sortables/issues/308 |
| 85 | + detachInactiveScreens={false} |
| 86 | + screenOptions={{ |
| 87 | + tabBarStyle: { |
| 88 | + shadowColor: 'transparent' |
| 89 | + } |
| 90 | + }}> |
| 91 | + <Tab.Screen |
| 92 | + component={Screen1} |
| 93 | + name='Sortable 1' |
| 94 | + options={tabBarOptions} |
| 95 | + /> |
| 96 | + <Tab.Screen |
| 97 | + component={Screen2} |
| 98 | + name='Sortable 2' |
| 99 | + options={tabBarOptions} |
| 100 | + /> |
| 101 | + <Tab.Screen |
| 102 | + component={Screen3} |
| 103 | + name='No sortable' |
| 104 | + options={tabBarOptions} |
| 105 | + /> |
| 106 | + </Tab.Navigator> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +export default function BottomTabsNavigatorExample() { |
| 111 | + return ( |
| 112 | + <Screen style={styles.container} includeNavBarHeight> |
| 113 | + <Tabs /> |
| 114 | + </Screen> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +const styles = StyleSheet.create({ |
| 119 | + container: { |
| 120 | + backgroundColor: colors.white |
| 121 | + }, |
| 122 | + focusedLabel: { |
| 123 | + ...text.label3, |
| 124 | + color: colors.primary |
| 125 | + }, |
| 126 | + label: { |
| 127 | + ...text.label3, |
| 128 | + color: colors.foreground3, |
| 129 | + fontWeight: 'normal' |
| 130 | + }, |
| 131 | + screenTitle: { |
| 132 | + ...text.label1, |
| 133 | + color: colors.foreground1, |
| 134 | + marginBottom: spacing.md |
| 135 | + }, |
| 136 | + scrollContainer: { |
| 137 | + padding: spacing.md |
| 138 | + } |
| 139 | +}); |
0 commit comments