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

Add SelectAllButton to BulkActionsToolbar #9043

Closed
wants to merge 1 commit into from
Closed
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
129 changes: 92 additions & 37 deletions packages/ra-core/src/controller/list/useRecordSelection.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,102 @@ describe('useRecordSelection', () => {
expect(selected).toEqual([123, 456]);
});

it('should allow to select/unselect a record', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
describe('select', () => {
it('should allow to select a record', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [selected1, { select }] = result.current;
expect(selected1).toEqual([]);
select([123, 456]);
const [selected2] = result.current;
expect(selected2).toEqual([123, 456]);
});
it('should ignore previous selection', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [selected1, { select }] = result.current;
expect(selected1).toEqual([]);
select([123, 456]);
const [selected2] = result.current;
expect(selected2).toEqual([123, 456]);
select([123, 789]);
const [selected3] = result.current;
expect(selected3).toEqual([123, 789]);
});
const [selected1, { select }] = result.current;
expect(selected1).toEqual([]);
select([123, 456]);
const [selected2, { unselect }] = result.current;
expect(selected2).toEqual([123, 456]);
unselect([123]);
const [selected3] = result.current;
expect(selected3).toEqual([456]);
});

it('should allow to toggle a record', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
describe('unselect', () => {
it('should allow to unselect a record', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [, { select, unselect }] = result.current;
select([123, 456]);
unselect([123]);
const [selected] = result.current;
expect(selected).toEqual([456]);
});
it('should not fail if the record was not selected', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [, { select, unselect }] = result.current;
select([123, 456]);
unselect([789]);
const [selected] = result.current;
expect(selected).toEqual([123, 456]);
});
const [selected1, { toggle }] = result.current;
expect(selected1).toEqual([]);
toggle(123);
const [selected2] = result.current;
expect(selected2).toEqual([123]);
toggle(456);
const [selected3] = result.current;
expect(selected3).toEqual([123, 456]);
toggle(123);
const [selected4] = result.current;
expect(selected4).toEqual([456]);
});

it('should allow to clear the selection', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
describe('toggle', () => {
it('should allow to toggle a record selection', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [selected1, { toggle }] = result.current;
expect(selected1).toEqual([]);
toggle(123);
const [selected2] = result.current;
expect(selected2).toEqual([123]);
toggle(456);
const [selected3] = result.current;
expect(selected3).toEqual([123, 456]);
toggle(123);
const [selected4] = result.current;
expect(selected4).toEqual([456]);
});
it('should allow to empty the selection', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [, { select, toggle }] = result.current;
select([123]);
toggle(123);
const [selected] = result.current;
expect(selected).toEqual([]);
});
});
describe('clearSelection', () => {
it('should allow to clear the selection', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [, { toggle, clearSelection }] = result.current;
toggle(123);
const [selected2] = result.current;
expect(selected2).toEqual([123]);
clearSelection();
const [selected3] = result.current;
expect(selected3).toEqual([]);
});
it('should not fail on empty selection', () => {
const { result } = renderHook(() => useRecordSelection('foo'), {
wrapper,
});
const [, { clearSelection }] = result.current;
clearSelection();
const [selected] = result.current;
expect(selected).toEqual([]);
});
const [, { toggle, clearSelection }] = result.current;
toggle(123);
const [selected2] = result.current;
expect(selected2).toEqual([123]);
clearSelection();
const [selected3] = result.current;
expect(selected3).toEqual([]);
});
});
7 changes: 4 additions & 3 deletions packages/ra-core/src/controller/list/useRecordSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export const useRecordSelection = <RecordType extends RaRecord = any>(

const selectionModifiers = useMemo(
() => ({
select: (idsToAdd: RecordType['id'][]) => {
if (!idsToAdd) return;
setIds([...idsToAdd]);
// erase the selection and replace it with the new one
select: (ids: RecordType['id'][]) => {
if (!ids) return;
setIds([...ids]);
},
unselect(idsToRemove: RecordType['id'][]) {
if (!idsToRemove || idsToRemove.length === 0) return;
Expand Down
44 changes: 43 additions & 1 deletion packages/ra-ui-materialui/src/list/BulkActionsToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,73 @@ import {
useCallback,
} from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import clsx from 'clsx';
import isEqual from 'lodash/isEqual';
import { styled } from '@mui/material/styles';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import { lighten } from '@mui/material/styles';
import IconButton from '@mui/material/IconButton';
import CloseIcon from '@mui/icons-material/Close';
import { useMutation } from 'react-query';
import {
useDataProvider,
useTranslate,
sanitizeListRestProps,
useListContext,
Identifier,
} from 'ra-core';

import TopToolbar from '../layout/TopToolbar';
import { Button } from '@mui/material';

export const BulkActionsToolbar = (props: BulkActionsToolbarProps) => {
const {
label = 'ra.action.bulk_actions',
children,
className,
selectAllLimit = 250,
...rest
} = props;
const {
data,
filterValues,
resource,
selectedIds = [],
onSelect,
onUnselectItems,
total,
perPage,
} = useListContext(props);
const dataProvider = useDataProvider();
const { mutateAsync } = useMutation(() =>
dataProvider.getList(resource, {
filter: filterValues,
pagination: { page: 1, perPage: total },
sort: { field: 'id', order: 'ASC' },
})
);

const translate = useTranslate();

const handleUnselectAllClick = useCallback(() => {
onUnselectItems();
}, [onUnselectItems]);

const handleSelectAll = useCallback(() => {
mutateAsync().then(({ data }) => {
onSelect(data.map(({ id }) => id));
});
}, [mutateAsync, onSelect]);

const isPageSelected =
selectedIds.length === perPage &&
isEqual(new Set(selectedIds), new Set(data.map(({ id }) => id)));
const hasMoreThanOnePage = total > perPage;
const isUnderSelectAllLimit = total <= selectAllLimit;
const displaySelectAllButton =
isPageSelected && hasMoreThanOnePage && isUnderSelectAllLimit;

return (
<Root className={className}>
<Toolbar
Expand All @@ -69,6 +100,16 @@ export const BulkActionsToolbar = (props: BulkActionsToolbarProps) => {
smart_count: selectedIds.length,
})}
</Typography>
{displaySelectAllButton && (
<Button
size="small"
color="primary"
onClick={handleSelectAll}
sx={{ ml: 1 }}
>
{translate('ra.action.select_all')}
</Button>
)}
</div>
<TopToolbar className={BulkActionsToolbarClasses.topToolbar}>
{Children.map(children, child =>
Expand Down Expand Up @@ -96,6 +137,7 @@ export interface BulkActionsToolbarProps {
label?: string;
selectedIds?: Identifier[];
className?: string;
selectAllLimit?: number;
}

const PREFIX = 'RaBulkActionsToolbar';
Expand Down