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] Prevents bubbling in menu header #3000

Merged
merged 6 commits into from
Nov 3, 2021
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 @@ -96,12 +96,18 @@ function GridColumnHeaderItem(props: GridColumnHeaderItemProps) {
}

const publish = React.useCallback(
(eventName: string) => (event: React.SyntheticEvent) =>
(eventName: string) => (event: React.SyntheticEvent) => {
// Ignore portal
// See /~https://github.com/mui-org/material-ui-x/issues/1721
if (!event.currentTarget.contains(event.target as Element)) {
return;
}
apiRef.current.publishEvent(
eventName,
apiRef.current.getColumnHeaderParams(column.field),
event,
),
);
},
[apiRef, column.field],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'test/utils';
import { expect } from 'chai';
import { gridClasses, DataGridPro } from '@mui/x-data-grid-pro';
import { getColumnHeaderCell } from 'test/utils/helperFn';
import { getColumnHeaderCell, getColumnValues } from 'test/utils/helperFn';

const isJSDOM = /jsdom/.test(window.navigator.userAgent);

Expand Down Expand Up @@ -56,6 +56,36 @@ describe('<DataGridPro /> - Column Headers', () => {
await waitFor(() => expect(screen.queryByRole('menu')).to.equal(null));
});

it('should not modify column order when menu is clicked', async () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
await waitFor(() => expect(screen.queryByRole('menu')).not.to.equal(null));
fireEvent.click(screen.queryByRole('menu'));
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
});

it('should sort column when sort by Asc is clicked', async () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
fireEvent.click(menuIconButton);
await waitFor(() => expect(screen.queryByRole('menu')).not.to.equal(null));
fireEvent.click(screen.getByRole('menuitem', { name: 'Sort by ASC' }));
expect(getColumnValues(0)).to.deep.equal(['Adidas', 'Nike', 'Puma']);
});

it('should close the menu of a column when resizing this column', async () => {
render(
<div style={{ width: 300, height: 500 }}>
Expand Down