From 21ffe70499cddd7237b93acd1c653510c8cfa9a1 Mon Sep 17 00:00:00 2001 From: Mikhail Golbakh Date: Mon, 5 Aug 2024 23:26:29 +0300 Subject: [PATCH] feat(react): add custom props for data loader components --- .../DataInfiniteLoader/DataInfiniteLoader.tsx | 31 +++++++++---------- .../components/DataInfiniteLoader/types.ts | 16 +++++++--- .../components/DataLoader/DataLoader.tsx | 6 ++-- src/react/components/DataLoader/types.ts | 12 +++++-- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/react/components/DataInfiniteLoader/DataInfiniteLoader.tsx b/src/react/components/DataInfiniteLoader/DataInfiniteLoader.tsx index be03b72..f103dc8 100644 --- a/src/react/components/DataInfiniteLoader/DataInfiniteLoader.tsx +++ b/src/react/components/DataInfiniteLoader/DataInfiniteLoader.tsx @@ -14,6 +14,9 @@ export const DataInfiniteLoader = ({ LoadingView, ErrorView, MoreView, + loadingViewProps, + errorViewProps, + moreViewProps, children, }: DataInfiniteLoaderProps): React.ReactNode => { const errorAction = React.useMemo['action']>( @@ -22,36 +25,32 @@ export const DataInfiniteLoader = ({ [errorActionProp], ); - const content = React.useMemo(() => { + const renderContent = () => { if (status === 'loading') { - return ; + return ; } if (status === 'error') { - return ; + return ; } if (status === 'success' && hasNextPage) { - return ; + return ( + + ); } return null; - }, [ - status, - hasNextPage, - LoadingView, - ErrorView, - error, - errorAction, - MoreView, - isFetchingNextPage, - fetchNextPage, - ]); + }; return ( <> {status === 'success' ? children : null} - {content} + {renderContent()} ); }; diff --git a/src/react/components/DataInfiniteLoader/types.ts b/src/react/components/DataInfiniteLoader/types.ts index 848fad0..f358af9 100644 --- a/src/react/components/DataInfiniteLoader/types.ts +++ b/src/react/components/DataInfiniteLoader/types.ts @@ -8,15 +8,23 @@ export interface MoreViewProps { onClick: () => void; } -export interface DataInfiniteLoaderProps { +export interface DataInfiniteLoaderProps< + TError, + TLoadingViewProps extends {} = {}, + TErrorViewProps extends ErrorViewProps = ErrorViewProps, + TMoreViewProps extends MoreViewProps = MoreViewProps, +> { status: DataLoaderStatus; error: TError | null; errorAction?: ErrorAction | ErrorAction['handler']; hasNextPage: boolean; fetchNextPage: () => unknown; isFetchingNextPage: boolean; - LoadingView: ComponentType; - ErrorView: ComponentType>; - MoreView: ComponentType; + LoadingView: ComponentType; + ErrorView: ComponentType; + MoreView: ComponentType; + loadingViewProps?: TLoadingViewProps; + errorViewProps?: Omit>; + moreViewProps?: Omit; children: ReactNode; } diff --git a/src/react/components/DataLoader/DataLoader.tsx b/src/react/components/DataLoader/DataLoader.tsx index 36c1c67..4bd0dc0 100644 --- a/src/react/components/DataLoader/DataLoader.tsx +++ b/src/react/components/DataLoader/DataLoader.tsx @@ -10,6 +10,8 @@ export const DataLoader = ({ errorAction: errorActionProp, LoadingView, ErrorView, + loadingViewProps, + errorViewProps, children, }: DataLoaderProps): React.ReactNode => { const errorAction = React.useMemo['action']>( @@ -19,11 +21,11 @@ export const DataLoader = ({ ); if (status === 'loading') { - return ; + return ; } if (status === 'error') { - return ; + return ; } return <>{children}; diff --git a/src/react/components/DataLoader/types.ts b/src/react/components/DataLoader/types.ts index eab23fc..949927b 100644 --- a/src/react/components/DataLoader/types.ts +++ b/src/react/components/DataLoader/types.ts @@ -3,11 +3,17 @@ import type {ComponentType, ReactNode} from 'react'; import type {DataLoaderStatus} from '../../../core'; import type {ErrorAction, ErrorViewProps} from '../types'; -export interface DataLoaderProps { +export interface DataLoaderProps< + TError, + TLoadingViewProps extends {} = {}, + TErrorViewProps extends ErrorViewProps = ErrorViewProps, +> { status: DataLoaderStatus; error: TError | null; errorAction?: ErrorAction | ErrorAction['handler']; - LoadingView: ComponentType; - ErrorView: ComponentType>; + LoadingView: ComponentType; + ErrorView: ComponentType; + loadingViewProps?: TLoadingViewProps; + errorViewProps?: Omit>; children: ReactNode; }