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

Fix Datagrid bulkActionButtons shows empty toolbar when user cannot access the delete action #10347

Merged
merged 2 commits into from
Nov 14, 2024
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
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
Loading