import { FlashList, type FlashListProps } from "@shopify/flash-list"; import React, { forwardRef, useImperativeHandle, useRef } from "react"; import { View, type ViewStyle } from "react-native"; import { Text } from "./Text"; type PartialExcept = Partial & Pick; export interface HorizontalScrollRef { scrollToIndex: (index: number, viewOffset: number) => void; } interface HorizontalScrollProps extends PartialExcept< Omit, "renderItem">, "estimatedItemSize" > { data?: T[] | null; renderItem: (item: T, index: number) => React.ReactNode; keyExtractor?: (item: T, index: number) => string; containerStyle?: ViewStyle; contentContainerStyle?: ViewStyle; loadingContainerStyle?: ViewStyle; height?: number; loading?: boolean; extraData?: any; noItemsText?: string; } export const HorizontalScroll = forwardRef< HorizontalScrollRef, HorizontalScrollProps >( ( { data = [], keyExtractor, renderItem, containerStyle, contentContainerStyle, loadingContainerStyle, loading = false, height = 164, extraData, noItemsText, ...props }: HorizontalScrollProps, ref: React.ForwardedRef, ) => { const flashListRef = useRef>(null); useImperativeHandle(ref!, () => ({ scrollToIndex: (index: number, viewOffset: number) => { flashListRef.current?.scrollToIndex({ index, animated: true, viewPosition: 0, viewOffset, }); }, })); const renderFlashListItem = ({ item, index, }: { item: T; index: number; }) => {renderItem(item, index)}; if (!data || loading) { return ( ); } return ( ref={flashListRef} data={data} extraData={extraData} renderItem={renderFlashListItem} horizontal estimatedItemSize={200} showsHorizontalScrollIndicator={false} contentContainerStyle={{ paddingHorizontal: 16, ...contentContainerStyle, }} keyExtractor={keyExtractor} ListEmptyComponent={() => ( {noItemsText || "No data available"} )} {...props} /> ); }, );