Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update <Datagrid> and <SimpleList> empty message when a filter is active #10184

Merged
merged 18 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion packages/ra-language-english/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ const englishMessages: TranslationMessages = {
"Some of your changes weren't saved. Are you sure you want to ignore them?",
},
navigation: {
no_results: 'No results found',
clear_filters: 'Clear filters.',
no_results: 'No %{resource} found using the current filters.',
no_more_results:
'The page number %{page} is out of boundaries. Try the previous page.',
page_out_of_boundaries: 'Page number %{page} out of boundaries',
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-language-french/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const frenchMessages: TranslationMessages = {
"Certains changements n'ont pas été enregistrés. Êtes-vous sûr(e) de vouloir quitter cette page ?",
},
navigation: {
no_results: 'Aucun résultat',
clear_filters: 'Effacer les filtres.',
no_results: 'Aucun %{resource} trouvé avec les filtres actuels.',
erwanMarmelab marked this conversation as resolved.
Show resolved Hide resolved
no_more_results:
'La page numéro %{page} est en dehors des limites. Essayez la page précédente.',
page_out_of_boundaries: 'La page %{page} est en dehors des limites',
Expand Down
11 changes: 9 additions & 2 deletions packages/ra-ui-materialui/src/list/ListNoResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ import * as React from 'react';
import { memo } from 'react';
import CardContent from '@mui/material/CardContent';
import Typography from '@mui/material/Typography';
import { useResourceContext, useTranslate } from 'ra-core';
import { useListController, useResourceContext, useTranslate } from 'ra-core';
import { Link } from '@mui/material';

export const ListNoResults = memo(() => {
const translate = useTranslate();
const resource = useResourceContext();
const { filterValues, setFilters } = useListController({ resource });
return (
<CardContent>
<Typography variant="body2">
{translate('ra.navigation.no_results', { resource })}
{translate('ra.navigation.no_results', { resource })}{' '}
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
{filterValues && Object.keys(filterValues).length > 0 && (
<Link component="button" onClick={() => setFilters({}, [])}>
{translate('ra.navigation.clear_filters')}
</Link>
)}
</Typography>
</CardContent>
);
Expand Down
73 changes: 61 additions & 12 deletions packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import * as React from 'react';
import { render, screen, waitFor, within } from '@testing-library/react';
import { ListContext, ResourceContextProvider } from 'ra-core';
import {
ListContext,
Resource,
ResourceContextProvider,
TestMemoryRouter,
useListFilterContext,
} from 'ra-core';
import fakerestDataProvider from 'ra-data-fakerest';

import { AdminContext } from '../../AdminContext';
import { SimpleList } from './SimpleList';
import { TextField } from '../../field/TextField';
import { NoPrimaryText } from './SimpleList.stories';
import { Admin, List } from 'react-admin';

const Wrapper = ({ children }: any) => (
<AdminContext>
Expand Down Expand Up @@ -134,18 +142,59 @@ describe('<SimpleList />', () => {

it('should display a message when there is no result', () => {
render(
<ListContext.Provider
value={{
isLoading: false,
data: [],
total: 0,
resource: 'posts',
}}
>
<SimpleList />
</ListContext.Provider>
<TestMemoryRouter>
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
<Admin>
<Resource
name="posts"
list={
<ListContext.Provider
value={{
isLoading: false,
data: [],
total: 0,
resource: 'posts',
}}
>
<SimpleList />
</ListContext.Provider>
}
/>
</Admin>
</TestMemoryRouter>
);
expect(screen.queryByText('ra.navigation.no_results')).not.toBeNull();
expect(
screen.queryByText('No posts found using the current filters.')
).not.toBeNull();
});

it('should display a message when there is no result but filters applied', async () => {
const MyListView = () => {
const { setFilters } = useListFilterContext();
setFilters({ title: 'foo' }, ['title']);
return <SimpleList />;
};

render(
<TestMemoryRouter>
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
<Admin
dataProvider={fakerestDataProvider({
posts: [{ id: 1, title: 'bar' }],
})}
>
<Resource
name="posts"
list={
<List>
<MyListView />
</List>
}
/>
</Admin>
</TestMemoryRouter>
);

await screen.findByText('No posts found using the current filters.');
screen.getByText('Clear filters.');
});

it('should fall back to record representation when no primaryText is provided', async () => {
Expand Down
Loading