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

[DataGrid] Allow to pass custom props to .main element #15919

Merged
merged 2 commits into from
Dec 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const DataGridPremiumRaw = React.forwardRef(function DataGridPremium<R extends G
sx={props.sx}
ref={ref}
{...props.forwardedProps}
{...props.slotProps?.root}
>
<GridHeader />
<GridBody>
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const DataGridProRaw = React.forwardRef(function DataGridPro<R extends GridValid
sx={props.sx}
ref={ref}
{...props.forwardedProps}
{...props.slotProps?.root}
>
<GridHeader />
<GridBody>
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const DataGridRaw = React.forwardRef(function DataGrid<R extends GridValidRowMod
sx={props.sx}
ref={ref}
{...props.forwardedProps}
{...props.slotProps?.root}
>
<GridHeader />
<GridBody />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const GridMainContainer = React.forwardRef<
className={props.className}
tabIndex={-1}
{...ariaAttributes}
{...rootProps.slotProps?.main}
>
<GridPanelAnchor role="presentation" data-id="gridPanelAnchor" />
{props.children}
Expand Down
4 changes: 4 additions & 0 deletions packages/x-data-grid/src/internals/utils/useProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function groupForwardedProps<
[key: string]: any;
},
>(props: T): T {
if (props.slotProps?.root) {
return props;
}

const keys = Object.keys(props);

if (!keys.some((key) => key.startsWith('aria-') || key.startsWith('data-'))) {
Expand Down
10 changes: 10 additions & 0 deletions packages/x-data-grid/src/models/gridSlotsComponentsProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import type { GridRowCountProps } from '../components/GridRowCount';
import type { GridColumnHeaderSortIconProps } from '../components/columnHeaders/GridColumnHeaderSortIcon';

type DividerProps = {};
type RootProps = React.HTMLAttributes<HTMLDivElement> & Record<`data-${string}`, string>;
type MainProps = React.HTMLAttributes<HTMLDivElement> & Record<`data-${string}`, string>;

// Overrides for module augmentation
export interface BaseBadgePropsOverrides {}
Expand Down Expand Up @@ -111,6 +113,14 @@ export interface GridSlotProps {
row: GridRowProps & RowPropsOverrides;
skeletonCell: GridSkeletonCellProps & SkeletonCellPropsOverrides;
toolbar: GridToolbarProps & ToolbarPropsOverrides;
/**
* Props passed to the `.main` (role="grid") element
*/
main: MainProps;
/**
* Props passed to the `.root` element
*/
root: RootProps;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions packages/x-data-grid/src/tests/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ describe('<DataGrid />', () => {
expect(document.querySelector('[aria-label="Grid one"]')).to.equal(gridRef.current);
});

it('should accept aria & data attributes props using `slotProps`', () => {
const rootRef = React.createRef<HTMLDivElement>();
render(
<div style={{ width: 300, height: 500 }}>
<DataGrid
{...baselineProps}
ref={rootRef}
columns={[{ field: 'brand' }]}
slotProps={{
main: {
'data-custom-id': 'grid-1',
'aria-label': 'Grid one',
},
root: {
'data-custom-id': 'root-1',
'aria-label': 'Root one',
},
}}
/>
</div>,
);

expect(document.querySelector('[data-custom-id="root-1"]')).to.equal(rootRef.current);
expect(document.querySelector('[aria-label="Root one"]')).to.equal(rootRef.current);

const mainElement = document.querySelector('[role="grid"]');
expect(document.querySelector('[data-custom-id="grid-1"]')).to.equal(mainElement);
expect(document.querySelector('[aria-label="Grid one"]')).to.equal(mainElement);
});

it('should not fail when row have IDs match Object prototype keys (constructor, hasOwnProperty, etc)', () => {
const rows = [
{ id: 'a', col1: 'Hello', col2: 'World' },
Expand Down
Loading