Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ListBase.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ The `<ListBase>` component accepts the following props:
* [`debounce`](./List.md#debounce)
* [`disableAuthentication`](./List.md#disableauthentication)
* [`disableSyncWithLocation`](./List.md#disablesyncwithlocation)
* [`empty`](./List.md#empty)
* [`emptyWhileLoading`](./List.md#emptywhileloading)
* [`exporter`](./List.md#exporter)
* [`filter`](./List.md#filter-permanent-filter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from './InfiniteListBase.stories';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { testDataProvider } from '../../dataProvider';
import { Empty } from './ListBase.stories';

describe('InfiniteListBase', () => {
it('should fetch a list of records on mount, put it in a ListContext, and render its children', async () => {
Expand Down Expand Up @@ -160,6 +161,14 @@ describe('InfiniteListBase', () => {
expect(screen.queryByText('Loading...')).toBeNull();
});

it('should render a custom empty component when data is empty', async () => {
render(<Empty />);
expect(screen.queryByText('Loading...')).not.toBeNull();
expect(screen.queryByText('War and Peace')).toBeNull();
fireEvent.click(screen.getByText('Resolve books loading'));
await screen.findByText('No books');
});

it('should render loading component while loading', async () => {
render(<Loading />);
expect(screen.queryByText('Loading books...')).not.toBeNull();
Expand Down
33 changes: 33 additions & 0 deletions packages/ra-core/src/controller/list/InfiniteListBase.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,39 @@ export const FetchError = () => {
);
};

export const Empty = () => {
let resolveGetList: (() => void) | null = null;
const dataProvider = {
...defaultDataProvider,
getList: () => {
return new Promise<GetListResult>(resolve => {
resolveGetList = () => resolve({ data: [], total: 0 });
});
},
};

return (
<CoreAdminContext dataProvider={dataProvider}>
<button
onClick={() => {
resolveGetList && resolveGetList();
}}
>
Resolve books loading
</button>
<InfiniteListBase
resource="books"
perPage={5}
loading={<p>Loading...</p>}
empty={<p>No books</p>}
>
<BookListView />
<InfinitePagination />
</InfiniteListBase>
</CoreAdminContext>
);
};

const defaultI18nProvider = polyglotI18nProvider(
locale =>
locale === 'fr'
Expand Down
32 changes: 29 additions & 3 deletions packages/ra-core/src/controller/list/InfiniteListBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const InfiniteListBase = <RecordType extends RaRecord = any>({
loading,
offline,
error,
empty,
children,
render,
...props
Expand All @@ -71,6 +72,11 @@ export const InfiniteListBase = <RecordType extends RaRecord = any>({
isPending,
isPlaceholderData,
error: errorState,
data,
total,
hasPreviousPage,
hasNextPage,
filterValues,
} = controllerProps;

const showAuthLoading =
Expand All @@ -95,6 +101,23 @@ export const InfiniteListBase = <RecordType extends RaRecord = any>({

const showError = errorState && error !== false && error !== undefined;

const showEmpty =
!errorState &&
// the list is not loading data for the first time
!isPending &&
// the API returned no data (using either normal or partial pagination)
(total === 0 ||
(total == null &&
hasPreviousPage === false &&
hasNextPage === false &&
// @ts-ignore FIXME total may be undefined when using partial pagination but the ListControllerResult type is wrong about it
data.length === 0)) &&
// the user didn't set any filters
!Object.keys(filterValues).length &&
// there is an empty page component
empty !== undefined &&
empty !== false;

return (
// We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided
<OptionalResourceContextProvider value={props.resource}>
Expand All @@ -118,9 +141,11 @@ export const InfiniteListBase = <RecordType extends RaRecord = any>({
? offline
: showError
? error
: render
? render(controllerProps)
: children}
: showEmpty
? empty
: render
? render(controllerProps)
: children}
</InfinitePaginationContext.Provider>
</ListContextProvider>
</OptionalResourceContextProvider>
Expand All @@ -133,6 +158,7 @@ export interface InfiniteListBaseProps<RecordType extends RaRecord = any>
loading?: ReactNode;
offline?: ReactNode;
error?: ReactNode;
empty?: ReactNode;
children?: ReactNode;
render?: (props: InfiniteListControllerResult<RecordType>) => ReactNode;
}
8 changes: 8 additions & 0 deletions packages/ra-core/src/controller/list/ListBase.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import {
AccessControl,
DefaultTitle,
Empty,
EmptyWhileLoading,
EmptyWhileLoadingRender,
FetchError,
Expand Down Expand Up @@ -168,6 +169,13 @@ describe('ListBase', () => {
await screen.findByText('War and Peace');
await screen.findByText('You are offline, the data may be outdated');
});
it('should render a custom empty component when data is empty', async () => {
render(<Empty />);
expect(screen.queryByText('Loading...')).not.toBeNull();
expect(screen.queryByText('War and Peace')).toBeNull();
fireEvent.click(screen.getByText('Resolve books loading'));
await screen.findByText('No books');
});
it('should render nothing while loading if emptyWhileLoading is set to true', async () => {
render(<EmptyWhileLoading />);
expect(screen.queryByText('Loading...')).toBeNull();
Expand Down
33 changes: 33 additions & 0 deletions packages/ra-core/src/controller/list/ListBase.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,39 @@ export const FetchError = () => {
);
};

export const Empty = () => {
let resolveGetList: (() => void) | null = null;
const baseProvider = defaultDataProvider(0);
const dataProvider = {
...baseProvider,
getList: () => {
return new Promise<GetListResult>(resolve => {
resolveGetList = () => resolve({ data: [], total: 0 });
});
},
};

return (
<CoreAdminContext dataProvider={dataProvider}>
<button
onClick={() => {
resolveGetList && resolveGetList();
}}
>
Resolve books loading
</button>
<ListBase
resource="books"
perPage={5}
loading={<p>Loading...</p>}
empty={<p>No books</p>}
>
<BookListView />
</ListBase>
</CoreAdminContext>
);
};

export const EmptyWhileLoading = () => {
let resolveGetList: (() => void) | null = null;
const baseProvider = defaultDataProvider(0);
Expand Down
37 changes: 32 additions & 5 deletions packages/ra-core/src/controller/list/ListBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const ListBase = <RecordType extends RaRecord = any>({
loading,
offline,
error,
empty,
render,
...props
}: ListBaseProps<RecordType>) => {
Expand All @@ -71,6 +72,11 @@ export const ListBase = <RecordType extends RaRecord = any>({
isPending,
isPlaceholderData,
error: errorState,
data,
total,
hasPreviousPage,
hasNextPage,
filterValues,
} = controllerProps;

const showAuthLoading =
Expand All @@ -95,7 +101,25 @@ export const ListBase = <RecordType extends RaRecord = any>({

const showError = errorState && error !== false && error !== undefined;

const showEmpty = isPending && !showOffline && emptyWhileLoading === true;
const showEmptyWhileLoading =
isPending && !showOffline && emptyWhileLoading === true;

const showEmpty =
!errorState &&
// the list is not loading data for the first time
!isPending &&
// the API returned no data (using either normal or partial pagination)
(total === 0 ||
(total == null &&
hasPreviousPage === false &&
hasNextPage === false &&
// @ts-ignore FIXME total may be undefined when using partial pagination but the ListControllerResult type is wrong about it
data.length === 0)) &&
// the user didn't set any filters
!Object.keys(filterValues).length &&
// there is an empty page component
empty !== undefined &&
empty !== false;

return (
// We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided
Expand All @@ -109,11 +133,13 @@ export const ListBase = <RecordType extends RaRecord = any>({
? offline
: showError
? error
: showEmpty
: showEmptyWhileLoading
? null
: render
? render(controllerProps)
: children}
: showEmpty
? empty
: render
? render(controllerProps)
: children}
</ListContextProvider>
</OptionalResourceContextProvider>
);
Expand All @@ -126,6 +152,7 @@ export interface ListBaseProps<RecordType extends RaRecord = any>
loading?: ReactNode;
offline?: ReactNode;
error?: ReactNode;
empty?: ReactNode;
children?: ReactNode;
render?: (props: ListControllerResult<RecordType, Error>) => ReactNode;
}
5 changes: 4 additions & 1 deletion packages/ra-ui-materialui/src/list/InfiniteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const defaultFilter = {};
const defaultAuthLoading = <Loading />;

export interface InfiniteListProps<RecordType extends RaRecord = any>
extends Omit<InfiniteListBaseProps<RecordType>, 'children' | 'render'>,
extends Omit<
InfiniteListBaseProps<RecordType>,
'children' | 'render' | 'empty'
>,
ListViewProps {}

const PREFIX = 'RaInfiniteList';
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/list/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const List = <RecordType extends RaRecord = any>(
};

export interface ListProps<RecordType extends RaRecord = any>
extends Omit<ListBaseProps<RecordType>, 'children' | 'render'>,
extends Omit<ListBaseProps<RecordType>, 'children' | 'render' | 'empty'>,
ListViewProps {}

const defaultFilter = {};
Expand Down
Loading