Skip to content
Merged
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
32 changes: 31 additions & 1 deletion packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { ThemeProvider, createTheme } from '@mui/material';

import { Datagrid } from './Datagrid';
import { AccessControl, SelectAllButton } from './Datagrid.stories';
import { AccessControl, FullApp, SelectAllButton } from './Datagrid.stories';

const TitleField = () => {
const record = useRecordContext();
Expand Down Expand Up @@ -126,6 +126,36 @@ describe('<Datagrid />', () => {
expect(screen.queryByText('Select All')).toBeNull();
});

describe('row selection', () => {
it('should reveal bulk delete button by default on row selection', async () => {
render(<FullApp />);
const checkboxes = await screen.findAllByRole('checkbox');
fireEvent.click(checkboxes[1]);
await screen.findByLabelText('Delete');
await screen.findByText('1 item selected');
fireEvent.click(checkboxes[2]);
await screen.findByText('2 items selected');
});
it('should reveal select all button when selecting the entire page', async () => {
render(<FullApp perPage={5} />);
const checkboxes = await screen.findAllByRole('checkbox');
fireEvent.click(checkboxes[0]);
const selectAllButton = await screen.findByText('Select all');
selectAllButton.click();
await screen.findByText('7 items selected');
});
it('should only unselect the current page records', async () => {
render(<FullApp perPage={5} />);
const checkboxes = await screen.findAllByRole('checkbox');
fireEvent.click(checkboxes[0]);
const selectAllButton = await screen.findByText('Select all');
selectAllButton.click();
await screen.findByText('7 items selected');
fireEvent.click(checkboxes[0]);
await screen.findByText('2 items selected');
});
});

describe('selecting items with the shift key', () => {
it('should call onSelect with the correct ids when the last selection is after the first', () => {
const Test = ({ selectedIds = [] }) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TestMemoryRouter,
SortPayload,
AuthProvider,
memoryStore,
} from 'ra-core';
import fakeRestDataProvider from 'ra-data-fakerest';
import defaultMessages from 'ra-language-english';
Expand Down Expand Up @@ -558,18 +559,21 @@ export const RowClickFalse = () => (

export const FullApp = ({
rowClick,
perPage = undefined,
}: {
rowClick?: DatagridRowProps['rowClick'];
perPage?: number;
}) => (
<AdminContext
dataProvider={dataProvider}
i18nProvider={polyglotI18nProvider(() => defaultMessages, 'en')}
store={memoryStore()}
>
<AdminUI>
<Resource
name="books"
list={() => (
<List>
<List perPage={perPage}>
<Datagrid
expand={<ExpandDetails />}
rowClick={rowClick}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ export const DatagridHeader = (props: DatagridHeaderProps) => {
event.target.checked
? selectedIds.concat(
data
.filter(
record => !selectedIds.includes(record.id)
)
.filter(record =>
!selectedIds.includes(record.id) &&
isRowSelectable
? isRowSelectable(record)
: true
)
.map(record => record.id)
)
: []
: // We should only unselect the ids present in the current page
selectedIds.filter(
id => !data.some(record => record.id === id)
)
);
},
[data, onSelect, isRowSelectable, selectedIds]
Expand Down
10 changes: 10 additions & 0 deletions packages/ra-ui-materialui/src/list/datatable/DataTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ describe('DataTable', () => {
selectAllButton.click();
await screen.findByText('7 items selected');
});
it('should only unselect the current page records', async () => {
render(<Basic />);
const checkboxes = await screen.findAllByRole('checkbox');
fireEvent.click(checkboxes[0]);
const selectAllButton = await screen.findByText('Select all');
selectAllButton.click();
await screen.findByText('7 items selected');
fireEvent.click(checkboxes[0]);
await screen.findByText('2 items selected');
});
});
describe('isRowSelectable', () => {
it('should allow to disable row selection', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ export const SelectPageCheckbox = () => {
event.target.checked
? selectedIds.concat(
data
.filter(
record => !selectedIds.includes(record.id)
)
.filter(record =>
!selectedIds.includes(record.id) &&
isRowSelectable
? isRowSelectable(record)
: true
)
.map(record => record.id)
)
: []
: // We should only unselect the ids present in the current page
selectedIds.filter(
id => !data.some(record => record.id === id)
)
);
},
[data, onSelect, isRowSelectable, selectedIds]
Expand Down
Loading