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 12 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
5 changes: 4 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,10 @@ 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_filtred_results:
'No %{resource} found using the current filters.',
no_results: 'No %{resource} found',
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: 3 additions & 0 deletions packages/ra-language-french/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ const frenchMessages: TranslationMessages = {
"Certains changements n'ont pas été enregistrés. Êtes-vous sûr(e) de vouloir quitter cette page ?",
},
navigation: {
clear_filters: 'Effacer les filtres.',
no_filtred_results:
'Aucun résultat trouvé avec les filtres actuels.',
no_results: 'Aucun résultat',
no_more_results:
'La page numéro %{page} est en dehors des limites. Essayez la page précédente.',
Expand Down
20 changes: 18 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,31 @@ 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 })}
{filterValues && Object.keys(filterValues).length > 0 ? (
<>
{translate('ra.navigation.no_filtred_results', {
resource,
})}
<Link
component="button"
onClick={() => setFilters({}, [])}
>
{translate('ra.navigation.clear_filters')}
</Link>
</>
) : (
translate('ra.navigation.no_results', { resource })
)}
</Typography>
</CardContent>
);
Expand Down
29 changes: 27 additions & 2 deletions packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as React from 'react';
import { render, screen, waitFor, within } from '@testing-library/react';
import {
fireEvent,
erwanMarmelab marked this conversation as resolved.
Show resolved Hide resolved
render,
screen,
waitFor,
within,
} from '@testing-library/react';
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 { Basic } from '../filter/FilterButton.stories';

const Wrapper = ({ children }: any) => (
<AdminContext>
Expand Down Expand Up @@ -143,11 +150,29 @@ describe('<SimpleList />', () => {
}}
>
<SimpleList />
</ListContext.Provider>
</ListContext.Provider>,
{ wrapper: Wrapper }
);
expect(screen.queryByText('ra.navigation.no_results')).not.toBeNull();
});

it('should display a message when there is no result but filters applied', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this test in the FilterButton tests file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we test the <SimpleList> logic here. I use the FilterButton's story to be able to test it with writing a new component. Do you prefer I write a new component to render that test ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then, why the SimpleList and not the List? Should we do both? @fzaninotto

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, the List?oResults is used by SimpleList and Datagrid, so in theory we should test both. Instead, I added a test for ListNoResults itself.

render(<Basic />);

await screen.findByText(
'Accusantium qui nihil voluptatum quia voluptas maxime ab similique'
);

fireEvent.change(screen.getByLabelText('Search'), {
target: { value: 'w' },
});

expect(
await screen.findByText('No posts found using the current filters.')
).not.toBeNull();
expect(screen.getByText('Clear filters.')).not.toBeNull();
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
});

it('should fall back to record representation when no primaryText is provided', async () => {
render(<NoPrimaryText />);
await screen.findByText('War and Peace');
Expand Down
Loading