Skip to content

Commit

Permalink
Merge pull request #10347 from marmelab/Fix-bulk-delete-datagrid-can-…
Browse files Browse the repository at this point in the history
…access

Fix Datagrid bulkActionButtons shows empty toolbar when user cannot access the delete action
  • Loading branch information
djhi authored Nov 14, 2024
2 parents 469f568 + 1a5d5a0 commit 33f186b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
24 changes: 22 additions & 2 deletions packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import {
CoreAdminContext,
testDataProvider,
Expand All @@ -9,10 +9,11 @@ import {
} from 'ra-core';
import { ThemeProvider, createTheme } from '@mui/material';
import { Datagrid } from './Datagrid';
import { AccessControl } from './Datagrid.stories';

const TitleField = (): JSX.Element => {
const record = useRecordContext();
return <span>{record.title}</span>;
return <span>{record?.title}</span>;
};

const Wrapper = ({ children, listContext }) => (
Expand Down Expand Up @@ -282,4 +283,23 @@ describe('<Datagrid />', () => {
);
expect(screen.queryByText('ra.navigation.no_results')).not.toBeNull();
});

describe('Access control', () => {
it('should not show row selection when there is no delete permissions', async () => {
render(<AccessControl />);
await screen.findByText('War and Peace');
expect(screen.queryAllByLabelText('Select this row')).toHaveLength(
0
);
});
it('should show row selection when user has delete permissions', async () => {
render(<AccessControl allowedAction="delete" />);
await screen.findByText('War and Peace');
await waitFor(() =>
expect(
screen.queryAllByLabelText('Select this row')
).toHaveLength(4)
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ export const AccessControl = ({
}),
},
}: {
allowedAction?: 'show' | 'edit' | 'invalid';
allowedAction?: 'show' | 'edit' | 'delete' | 'invalid';
authProvider?: AuthProvider;
}) => (
<AdminContext
Expand Down Expand Up @@ -636,10 +636,11 @@ export const AccessControl = ({

AccessControl.argTypes = {
allowedAction: {
options: ['show', 'edit', 'none'],
options: ['show', 'edit', 'delete', 'none'],
mapping: {
show: 'show',
edit: 'edit',
delete: 'delete',
none: 'invalid',
},
control: { type: 'select' },
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
OptionalResourceContextProvider,
RaRecord,
SortPayload,
useCanAccess,
useResourceContext,
} from 'ra-core';
import { Table, TableProps, SxProps } from '@mui/material';
import clsx from 'clsx';
Expand Down Expand Up @@ -117,6 +119,11 @@ const defaultBulkActionButtons = <BulkDeleteButton />;
export const Datagrid: React.ForwardRefExoticComponent<
Omit<DatagridProps, 'ref'> & React.RefAttributes<HTMLTableElement>
> = React.forwardRef<HTMLTableElement, DatagridProps>((props, ref) => {
const resourceFromContext = useResourceContext(props);
const { canAccess: canDelete } = useCanAccess({
resource: resourceFromContext,
action: 'delete',
});
const {
optimized = false,
body = optimized ? PureDatagridBody : DatagridBody,
Expand All @@ -125,7 +132,7 @@ export const Datagrid: React.ForwardRefExoticComponent<
className,
empty = DefaultEmpty,
expand,
bulkActionButtons = defaultBulkActionButtons,
bulkActionButtons = canDelete ? defaultBulkActionButtons : false,
hover,
isRowSelectable,
isRowExpandable,
Expand Down

0 comments on commit 33f186b

Please sign in to comment.