|
| 1 | +import React, { useCallback, useMemo, useState } from 'react'; |
| 2 | +import { RefreshControl, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; |
| 3 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 4 | +import { |
| 5 | + Header, |
| 6 | + LargeHeader, |
| 7 | + ScalingView, |
| 8 | + ScrollViewWithHeaders, |
| 9 | + ScrollHeaderProps, |
| 10 | + ScrollLargeHeaderProps, |
| 11 | +} from '@codeherence/react-native-header'; |
| 12 | + |
| 13 | +import { range } from '../../utils'; |
| 14 | +import { BackButton } from '../../components'; |
| 15 | +import type { TestScreenNavigationProps } from '../../navigation'; |
| 16 | + |
| 17 | +const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar }) => { |
| 18 | + return ( |
| 19 | + <Header |
| 20 | + showNavBar={showNavBar} |
| 21 | + headerCenterFadesIn={false} |
| 22 | + borderColor="red" |
| 23 | + initialBorderColor="blue" |
| 24 | + headerCenter={ |
| 25 | + <Text style={styles.navBarTitle} numberOfLines={1}> |
| 26 | + Header |
| 27 | + </Text> |
| 28 | + } |
| 29 | + headerLeft={<BackButton />} |
| 30 | + /> |
| 31 | + ); |
| 32 | +}; |
| 33 | + |
| 34 | +const Simple: React.FC<TestScreenNavigationProps> = () => { |
| 35 | + const { bottom } = useSafeAreaInsets(); |
| 36 | + const [refreshing, setRefreshing] = useState(false); |
| 37 | + |
| 38 | + const data = useMemo(() => range({ end: 100 }), []); |
| 39 | + |
| 40 | + const onRefresh = async () => { |
| 41 | + if (refreshing) return; |
| 42 | + |
| 43 | + setRefreshing(true); |
| 44 | + // Mimic some asynchronous task |
| 45 | + await new Promise((res) => setTimeout(res, 2500)); |
| 46 | + setRefreshing(false); |
| 47 | + }; |
| 48 | + |
| 49 | + const LargeHeaderComponent: React.FC<ScrollLargeHeaderProps> = useCallback(({ scrollY }) => { |
| 50 | + return ( |
| 51 | + <LargeHeader> |
| 52 | + <ScalingView scrollY={scrollY}> |
| 53 | + <View |
| 54 | + style={{ |
| 55 | + flexDirection: 'row', |
| 56 | + justifyContent: 'space-between', |
| 57 | + alignItems: 'center', |
| 58 | + paddingHorizontal: 24, |
| 59 | + }} |
| 60 | + > |
| 61 | + <Text style={{ fontSize: 24 }}>List</Text> |
| 62 | + <TouchableOpacity onPress={() => console.log('Pressed')}> |
| 63 | + <Text>Icon</Text> |
| 64 | + </TouchableOpacity> |
| 65 | + </View> |
| 66 | + </ScalingView> |
| 67 | + </LargeHeader> |
| 68 | + ); |
| 69 | + }, []); |
| 70 | + |
| 71 | + return ( |
| 72 | + <ScrollViewWithHeaders |
| 73 | + HeaderComponent={HeaderComponent} |
| 74 | + LargeHeaderComponent={LargeHeaderComponent} |
| 75 | + automaticallyAdjustsScrollIndicatorInsets={false} |
| 76 | + scrollIndicatorInsets={{ bottom }} |
| 77 | + contentContainerStyle={{ paddingBottom: bottom }} |
| 78 | + refreshControl={ |
| 79 | + <RefreshControl refreshing={refreshing} colors={['#8E8E93']} onRefresh={onRefresh} /> |
| 80 | + } |
| 81 | + > |
| 82 | + <View style={styles.children}> |
| 83 | + {data.map((i) => ( |
| 84 | + <View key={`text-${i}`} style={styles.box} /> |
| 85 | + ))} |
| 86 | + </View> |
| 87 | + </ScrollViewWithHeaders> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export default Simple; |
| 92 | + |
| 93 | +const styles = StyleSheet.create({ |
| 94 | + children: { marginTop: 16, paddingHorizontal: 16 }, |
| 95 | + box: { |
| 96 | + backgroundColor: 'lightgray', |
| 97 | + height: 50, |
| 98 | + marginVertical: 8, |
| 99 | + borderRadius: 8, |
| 100 | + }, |
| 101 | + navBarTitle: { fontSize: 16, fontWeight: 'bold' }, |
| 102 | + largeHeaderStyle: { flexDirection: 'row-reverse' }, |
| 103 | + largeHeaderSubtitleStyle: { |
| 104 | + fontSize: 16, |
| 105 | + fontWeight: 'bold', |
| 106 | + paddingHorizontal: 16, |
| 107 | + }, |
| 108 | +}); |
0 commit comments