import * as React from 'react'; import { FlatList as RNFlatList, FlatListProps as RNFlatListProps, View, } from 'react-native'; import { getElement } from '../../hook'; export type FlatListRef = RNFlatList; export type FlatListProps = RNFlatListProps & { /** * Rendered when the list is error. Can be a React Component Class, a render function, or * a rendered element. */ ListErrorComponent?: | React.ComponentType | React.ReactElement | null | undefined; /** * Rendered when the list is loading. Can be a React Component Class, a render function, or * a rendered element. */ ListLoadingComponent?: | React.ComponentType | React.ReactElement | null | undefined; }; export const _FlatList = ( props: FlatListProps, ref?: React.ForwardedRef> ) => { const { ListErrorComponent, ListLoadingComponent, ListEmptyComponent, data } = props; // !!! https://github.com/facebook/react-native/issues/42967 // !!! https://github.com/facebook/react-native/issues/36766 // !!! https://github.com/facebook/react-native/issues/39421 return ( {data?.length !== 0 && } {data?.length === 0 && getElement(ListEmptyComponent)} {getElement(ListErrorComponent)} {getElement(ListLoadingComponent)} ); }; /** * @example * * export const FlatList = FlatListFactory<{ id: string }>(); * export function FlatList() {} * @returns */ export function FlatListFactory() { return React.forwardRef, FlatListProps>(_FlatList); } export type FlatListFactoryReturn = ReturnType< typeof FlatListFactory >;