import { FlashList, type FlashListProps } from "@shopify/flash-list"; import React, { useImperativeHandle, useRef } from "react"; import { View, type ViewStyle } from "react-native"; import { Text } from "./Text"; export interface HorizontalScrollRef { scrollToIndex: (index: number, viewOffset: number) => void; } interface HorizontalScrollProps extends Omit, "renderItem" | "estimatedItemSize" | "data"> { data?: T[] | null; renderItem: (item: T, index: number) => React.ReactNode; keyExtractor?: (item: T, index: number) => string; containerStyle?: ViewStyle; contentContainerStyle?: ViewStyle; height?: number; loading?: boolean; extraData?: any; noItemsText?: string; } export const HorizontalScroll = ( props: HorizontalScrollProps & { ref?: React.ForwardedRef; }, ) => { const { data = [], keyExtractor, renderItem, containerStyle, contentContainerStyle, loading = false, height = 164, extraData, noItemsText, ref, ...restProps } = props; 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 showsHorizontalScrollIndicator={false} contentContainerStyle={{ paddingHorizontal: 16, ...contentContainerStyle, }} keyExtractor={keyExtractor} ListEmptyComponent={() => ( {noItemsText || "No data available"} )} {...restProps} /> ); };