Skip to content

Commit

Permalink
Fix <SimpleList> throws an error when no data in standalone mode
Browse files Browse the repository at this point in the history
## Problem

Since #10184, published in 5.2.0, `<SimpleList>` and `<Datagrid>` throw an error when used in standalone mode (i.e., outside of a ListContext) with an empty `data`.

## Solution

The problem comes from `ListNoResults`, which reads the `ListContext` to determine if there is an active filter.

## Solution

Use the alternative `useListContextWithProps` instead of `useListContext`.
  • Loading branch information
fzaninotto committed Oct 29, 2024
1 parent 60bae65 commit a510725
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/ra-ui-materialui/src/list/ListNoResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { CardContent, Typography } from '@mui/material';
import {
useGetResourceLabel,
useListContext,
useListContextWithProps,
useResourceContext,
useTranslate,
} from 'ra-core';
Expand All @@ -12,7 +12,7 @@ import { Button } from '../button';
export const ListNoResults = () => {
const translate = useTranslate();
const resource = useResourceContext();
const { filterValues, setFilters } = useListContext();
const { filterValues, setFilters } = useListContextWithProps();
const getResourceLabel = useGetResourceLabel();
if (!resource) {
throw new Error(
Expand All @@ -22,7 +22,9 @@ export const ListNoResults = () => {
return (
<CardContent>
<Typography variant="body2">
{filterValues && Object.keys(filterValues).length > 0 ? (
{filterValues &&
setFilters &&
Object.keys(filterValues).length > 0 ? (
<>
{translate('ra.navigation.no_filtered_results', {
resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { ListContext, ResourceContextProvider } from 'ra-core';
import { AdminContext } from '../../AdminContext';
import { SimpleList } from './SimpleList';
import { TextField } from '../../field/TextField';
import { NoPrimaryText } from './SimpleList.stories';
import {
NoPrimaryText,
Standalone,
StandaloneEmpty,
} from './SimpleList.stories';
import { Basic } from '../filter/FilterButton.stories';

const Wrapper = ({ children }: any) => (
Expand Down Expand Up @@ -193,4 +197,15 @@ describe('<SimpleList />', () => {
render(<NoPrimaryText />);
await screen.findByText('War and Peace');
});

describe('standalone', () => {
it('should work without a ListContext', async () => {
render(<Standalone />);
await screen.findByText('War and Peace');
});
it('should display a message when there is no result', async () => {
render(<StandaloneEmpty />);
await screen.findByText('No results found.');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,29 @@ export const FullAppInError = () => (
</AdminUI>
</AdminContext>
);

export const Standalone = () => (
<TestMemoryRouter>
<SimpleList
data={data.books}
primaryText={record => record.title}
secondaryText={record => record.author}
tertiaryText={record => record.year}
linkType={false}
/>
</TestMemoryRouter>
);

export const StandaloneEmpty = () => (
<TestMemoryRouter>
<ResourceContextProvider value="books">
<SimpleList<any>
data={[]}
primaryText={record => record.title}
secondaryText={record => record.author}
tertiaryText={record => record.year}
linkType={false}
/>
</ResourceContextProvider>
</TestMemoryRouter>
);

0 comments on commit a510725

Please sign in to comment.