|
| 1 | +import BottomSheet, { |
| 2 | + useBottomSheetScrollableCreator, |
| 3 | +} from '@gorhom/bottom-sheet'; |
| 4 | +import { LegendList, type LegendListRef } from '@legendapp/list'; |
| 5 | +import React, { useCallback, useRef } from 'react'; |
| 6 | +import { StyleSheet, View } from 'react-native'; |
| 7 | +import { Button } from '../../../components/button'; |
| 8 | +import renderItem from './renderItem'; |
| 9 | + |
| 10 | +export const DRAW_DISTANCE = 200; |
| 11 | +export const ESTIMATED_ITEM_LENGTH = 200; |
| 12 | + |
| 13 | +const snapPoints = ['50%', '85%']; |
| 14 | +const data = new Array(500).fill(0).map((_, i) => ({ |
| 15 | + id: i.toString(), |
| 16 | + type: 'item', |
| 17 | +})); |
| 18 | + |
| 19 | +const keyExtractor = (item: (typeof data)[0]) => `id${item.id}`; |
| 20 | + |
| 21 | +const LegendListExample = () => { |
| 22 | + //#region refs |
| 23 | + const bottomSheetRef = useRef<BottomSheet>(null); |
| 24 | + //#endregion |
| 25 | + |
| 26 | + //#region callbacks |
| 27 | + const handleExpandPress = useCallback(() => { |
| 28 | + bottomSheetRef.current?.expand(); |
| 29 | + }, []); |
| 30 | + const handleCollapsePress = useCallback(() => { |
| 31 | + bottomSheetRef.current?.collapse(); |
| 32 | + }, []); |
| 33 | + const handleClosePress = useCallback(() => { |
| 34 | + bottomSheetRef.current?.close(); |
| 35 | + }, []); |
| 36 | + //#endregion |
| 37 | + |
| 38 | + //#region renders |
| 39 | + const BottomSheetLegendListScrollable = useBottomSheetScrollableCreator(); |
| 40 | + //#endregion |
| 41 | + |
| 42 | + return ( |
| 43 | + <View style={styles.container}> |
| 44 | + <Button label="Expand" onPress={handleExpandPress} /> |
| 45 | + <Button label="Collapse" onPress={handleCollapsePress} /> |
| 46 | + <Button label="Close" onPress={handleClosePress} /> |
| 47 | + <BottomSheet |
| 48 | + ref={bottomSheetRef} |
| 49 | + enableDynamicSizing={false} |
| 50 | + snapPoints={snapPoints} |
| 51 | + > |
| 52 | + <LegendList |
| 53 | + style={styles.scrollContainer} |
| 54 | + contentContainerStyle={styles.listContainer} |
| 55 | + data={data} |
| 56 | + renderItem={renderItem} |
| 57 | + keyExtractor={keyExtractor} |
| 58 | + indicatorStyle="black" |
| 59 | + estimatedItemSize={ESTIMATED_ITEM_LENGTH} |
| 60 | + drawDistance={DRAW_DISTANCE} |
| 61 | + maintainVisibleContentPosition |
| 62 | + renderScrollComponent={BottomSheetLegendListScrollable} |
| 63 | + recycleItems |
| 64 | + /> |
| 65 | + </BottomSheet> |
| 66 | + </View> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +const styles = StyleSheet.create({ |
| 71 | + container: { |
| 72 | + flex: 1, |
| 73 | + padding: 24, |
| 74 | + }, |
| 75 | + scrollContainer: { |
| 76 | + flex: 1, |
| 77 | + height: 0.1, |
| 78 | + }, |
| 79 | + listContainer: {}, |
| 80 | +}); |
| 81 | + |
| 82 | +export default LegendListExample; |
0 commit comments