diff --git a/packages/ra-ui-materialui/src/list/ListNoResults.tsx b/packages/ra-ui-materialui/src/list/ListNoResults.tsx index 14972a85ba..dff2f3f1fb 100644 --- a/packages/ra-ui-materialui/src/list/ListNoResults.tsx +++ b/packages/ra-ui-materialui/src/list/ListNoResults.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import { CardContent, Typography } from '@mui/material'; import { useGetResourceLabel, - useListContext, + useListContextWithProps, useResourceContext, useTranslate, } from 'ra-core'; @@ -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( @@ -22,7 +22,9 @@ export const ListNoResults = () => { return ( - {filterValues && Object.keys(filterValues).length > 0 ? ( + {filterValues && + setFilters && + Object.keys(filterValues).length > 0 ? ( <> {translate('ra.navigation.no_filtered_results', { resource, diff --git a/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx b/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx index 1b13903936..04734497c8 100644 --- a/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx +++ b/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx @@ -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) => ( @@ -193,4 +197,15 @@ describe('', () => { render(); await screen.findByText('War and Peace'); }); + + describe('standalone', () => { + it('should work without a ListContext', async () => { + render(); + await screen.findByText('War and Peace'); + }); + it('should display a message when there is no result', async () => { + render(); + await screen.findByText('No results found.'); + }); + }); }); diff --git a/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.stories.tsx b/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.stories.tsx index 4a5d0b8d59..595132bfb8 100644 --- a/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.stories.tsx +++ b/packages/ra-ui-materialui/src/list/SimpleList/SimpleList.stories.tsx @@ -201,3 +201,29 @@ export const FullAppInError = () => ( ); + +export const Standalone = () => ( + + record.title} + secondaryText={record => record.author} + tertiaryText={record => record.year} + linkType={false} + /> + +); + +export const StandaloneEmpty = () => ( + + + + data={[]} + primaryText={record => record.title} + secondaryText={record => record.author} + tertiaryText={record => record.year} + linkType={false} + /> + + +);