diff --git a/packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx b/packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx
index ff0abdca14..b656136080 100644
--- a/packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx
+++ b/packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx
@@ -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,
@@ -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 {record.title};
+ return {record?.title};
};
const Wrapper = ({ children, listContext }) => (
@@ -282,4 +283,23 @@ describe('', () => {
);
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();
+ 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();
+ await screen.findByText('War and Peace');
+ await waitFor(() =>
+ expect(
+ screen.queryAllByLabelText('Select this row')
+ ).toHaveLength(4)
+ );
+ });
+ });
});
diff --git a/packages/ra-ui-materialui/src/list/datagrid/Datagrid.stories.tsx b/packages/ra-ui-materialui/src/list/datagrid/Datagrid.stories.tsx
index 4dcde52578..86720dc5bf 100644
--- a/packages/ra-ui-materialui/src/list/datagrid/Datagrid.stories.tsx
+++ b/packages/ra-ui-materialui/src/list/datagrid/Datagrid.stories.tsx
@@ -606,7 +606,7 @@ export const AccessControl = ({
}),
},
}: {
- allowedAction?: 'show' | 'edit' | 'invalid';
+ allowedAction?: 'show' | 'edit' | 'delete' | 'invalid';
authProvider?: AuthProvider;
}) => (
;
export const Datagrid: React.ForwardRefExoticComponent<
Omit & React.RefAttributes
> = React.forwardRef((props, ref) => {
+ const resourceFromContext = useResourceContext(props);
+ const { canAccess: canDelete } = useCanAccess({
+ resource: resourceFromContext,
+ action: 'delete',
+ });
const {
optimized = false,
body = optimized ? PureDatagridBody : DatagridBody,
@@ -125,7 +132,7 @@ export const Datagrid: React.ForwardRefExoticComponent<
className,
empty = DefaultEmpty,
expand,
- bulkActionButtons = defaultBulkActionButtons,
+ bulkActionButtons = canDelete ? defaultBulkActionButtons : false,
hover,
isRowSelectable,
isRowExpandable,