import React, { ReactNode } from 'react';
import { RaRecord } from '../../types';
import { ListControllerResult } from './useListController';
import { useListContextWithProps } from './useListContextWithProps';
/**
* Render prop version of useListContextWithProps
*
* @example
* const BookList = () => (
*
* (
*
* {data && data.map(record => (
* - {record.title}
* ))}
*
* )} />
*
* );
*/
export const WithListContext = ({
empty,
loading,
offline,
error,
render,
children,
...props
}: WithListContextProps) => {
const context = useListContextWithProps(props);
const {
data,
total,
isPaused,
isPending,
isPlaceholderData,
error: errorState,
} = context;
if (!isPaused && isPending && loading !== false && loading !== undefined) {
return loading;
}
if (
isPaused &&
(isPending || isPlaceholderData) &&
offline !== false &&
offline !== undefined
) {
return offline;
}
if (errorState && error !== false && error !== undefined) {
return error;
}
if (
(data == null || data.length === 0 || total === 0) &&
empty !== false &&
empty !== undefined
) {
return empty;
}
if (render) {
return render(context);
}
return children;
};
export interface WithListContextProps
extends React.PropsWithChildren<
Partial<
Pick<
ListControllerResult,
'data' | 'total' | 'isPending'
>
>
> {
render?: (context: Partial>) => ReactNode;
loading?: React.ReactNode;
offline?: React.ReactNode;
errorState?: ListControllerResult['error'];
error?: React.ReactNode;
empty?: React.ReactNode;
/**
* @deprecated
*/
label?: string;
}