|
| 1 | +import React, { useCallback } from 'react'; |
| 2 | +import { SectionListRenderItem, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; |
| 3 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 4 | +import { useNavigation } from '@react-navigation/native'; |
| 5 | +import { |
| 6 | + Header, |
| 7 | + LargeHeader, |
| 8 | + ScalingView, |
| 9 | + ScrollHeaderProps, |
| 10 | + ScrollLargeHeaderProps, |
| 11 | + SectionListWithHeaders, |
| 12 | +} from '@codeherence/react-native-header'; |
| 13 | +import { Avatar, BackButton } from '../../components'; |
| 14 | +import { RANDOM_IMAGE_NUM } from '../../constants'; |
| 15 | +import type { SectionListUsageScreenNavigationProps } from '../../navigation'; |
| 16 | + |
| 17 | +const DATA = [ |
| 18 | + { |
| 19 | + title: 'Main dishes', |
| 20 | + data: ['Pizza', 'Burger', 'Risotto', 'Pasta', 'Lasagna'], |
| 21 | + }, |
| 22 | + { |
| 23 | + title: 'Sides', |
| 24 | + data: ['French Fries', 'Onion Rings', 'Fried Shrimps', 'Mozzarella Sticks', 'Garlic Bread'], |
| 25 | + }, |
| 26 | + { |
| 27 | + title: 'Drinks', |
| 28 | + data: ['Water', 'Coke', 'Beer', 'Wine', 'Mojito', 'Cuba Libre', 'Pina Colada', 'Margarita'], |
| 29 | + }, |
| 30 | + { |
| 31 | + title: 'Desserts', |
| 32 | + data: ['Cheese Cake', 'Ice Cream', 'Chocolate Cake', 'Tiramisu', 'Panna Cotta', 'Profiteroles'], |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar }) => { |
| 37 | + const navigation = useNavigation(); |
| 38 | + const onPressProfile = () => navigation.navigate('Profile'); |
| 39 | + |
| 40 | + return ( |
| 41 | + <Header |
| 42 | + showNavBar={showNavBar} |
| 43 | + headerCenter={ |
| 44 | + <Text style={styles.navBarTitle} numberOfLines={1}> |
| 45 | + Header |
| 46 | + </Text> |
| 47 | + } |
| 48 | + headerRight={ |
| 49 | + <> |
| 50 | + <TouchableOpacity onPress={onPressProfile}> |
| 51 | + <Avatar |
| 52 | + size="sm" |
| 53 | + source={{ uri: `https://i.pravatar.cc/128?img=${RANDOM_IMAGE_NUM}` }} |
| 54 | + /> |
| 55 | + </TouchableOpacity> |
| 56 | + </> |
| 57 | + } |
| 58 | + headerRightFadesIn |
| 59 | + headerLeft={<BackButton />} |
| 60 | + /> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +const LargeHeaderComponent: React.FC<ScrollLargeHeaderProps> = ({ scrollY }) => { |
| 65 | + const navigation = useNavigation(); |
| 66 | + const onPressProfile = () => navigation.navigate('Profile'); |
| 67 | + |
| 68 | + return ( |
| 69 | + <LargeHeader> |
| 70 | + <ScalingView scrollY={scrollY} style={styles.leftHeader}> |
| 71 | + <Text style={styles.title}>Large Header</Text> |
| 72 | + </ScalingView> |
| 73 | + <TouchableOpacity onPress={onPressProfile}> |
| 74 | + <Avatar size="sm" source={{ uri: `https://i.pravatar.cc/128?img=${RANDOM_IMAGE_NUM}` }} /> |
| 75 | + </TouchableOpacity> |
| 76 | + </LargeHeader> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +// Used for SectionList optimization |
| 81 | +const ITEM_HEIGHT = 60; |
| 82 | + |
| 83 | +const SectionList: React.FC<SectionListUsageScreenNavigationProps> = () => { |
| 84 | + const { bottom } = useSafeAreaInsets(); |
| 85 | + |
| 86 | + const renderItem: SectionListRenderItem<string> = useCallback(({ item }) => { |
| 87 | + return ( |
| 88 | + <View style={styles.item}> |
| 89 | + <Text style={styles.itemText}>{item}. Scroll to see header animation</Text> |
| 90 | + </View> |
| 91 | + ); |
| 92 | + }, []); |
| 93 | + |
| 94 | + return ( |
| 95 | + <SectionListWithHeaders |
| 96 | + HeaderComponent={HeaderComponent} |
| 97 | + LargeHeaderComponent={LargeHeaderComponent} |
| 98 | + contentContainerStyle={{ paddingBottom: bottom }} |
| 99 | + sections={DATA} |
| 100 | + renderItem={renderItem} |
| 101 | + renderSectionHeader={({ section: { title } }) => ( |
| 102 | + <Text style={styles.sectionTitle}>{title}</Text> |
| 103 | + )} |
| 104 | + windowSize={10} |
| 105 | + getItemLayout={(_, index) => ({ index, length: ITEM_HEIGHT, offset: index * ITEM_HEIGHT })} |
| 106 | + initialNumToRender={50} |
| 107 | + maxToRenderPerBatch={100} |
| 108 | + disableAutoFixScroll |
| 109 | + keyExtractor={(_, i) => `text-row-${i}`} |
| 110 | + /> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export default SectionList; |
| 115 | + |
| 116 | +const styles = StyleSheet.create({ |
| 117 | + navBarTitle: { fontSize: 16, fontWeight: 'bold' }, |
| 118 | + title: { fontSize: 32, fontWeight: 'bold' }, |
| 119 | + sectionTitle: { |
| 120 | + fontSize: 18, |
| 121 | + fontWeight: 'bold', |
| 122 | + width: '100%', |
| 123 | + backgroundColor: '#E6E6E6', |
| 124 | + paddingHorizontal: 12, |
| 125 | + paddingVertical: 4, |
| 126 | + }, |
| 127 | + leftHeader: { gap: 2 }, |
| 128 | + item: { minHeight: ITEM_HEIGHT, padding: 16, justifyContent: 'center', alignItems: 'center' }, |
| 129 | + itemText: { textAlign: 'center' }, |
| 130 | +}); |
0 commit comments