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] Close other actions menus when opening a new one #3492

Merged
merged 2 commits into from
Dec 23, 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 @@ -27,10 +27,7 @@ const GridActionsCell = (props: GridActionsCellProps) => {
throw new Error('MUI: Missing the `getActions` property in the `GridColDef`.');
}

const showMenu = (event: React.MouseEvent) => {
event.stopPropagation();
setOpen(true);
};
const showMenu = () => setOpen(true);

const hideMenu = () => setOpen(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { useGridLogger } from '../../utils/useGridLogger';
import { useGridState } from '../../utils/useGridState';
import { gridRowsLookupSelector } from '../rows/gridRowsSelector';
import { isGridCellRoot } from '../../../utils/domUtils';
import { findParentElementFromClassName, isGridCellRoot } from '../../../utils/domUtils';
import {
gridSelectionStateSelector,
selectedGridRowsSelector,
Expand All @@ -19,7 +19,7 @@ import {
import { gridPaginatedVisibleSortedGridRowIdsSelector } from '../pagination';
import { gridVisibleSortedRowIdsSelector } from '../filter/gridFilterSelector';
import { GRID_CHECKBOX_SELECTION_COL_DEF, GridColDef } from '../../../models';
import { getDataGridUtilityClass } from '../../../gridClasses';
import { getDataGridUtilityClass, gridClasses } from '../../../gridClasses';
import { useGridStateInit } from '../../utils/useGridStateInit';
import { GridPreProcessingGroup, useGridRegisterPreProcessor } from '../../core/preProcessing';
import { GridCellModes } from '../../../models/gridEditRowModel';
Expand Down Expand Up @@ -331,18 +331,31 @@ export const useGridSelection = (
return;
}

const cellClicked = findParentElementFromClassName(
event.target as HTMLElement,
gridClasses.cell,
);
const field = cellClicked?.getAttribute('data-field');
if (field) {
const column = apiRef.current.getColumn(field);
if (column.type === 'actions') {
return;
}
}

if (event.shiftKey && (canHaveMultipleSelection || checkboxSelection)) {
expandRowRangeSelection(params.id);
} else {
handleSingleRowSelection(params.id, event);
}
},
[
expandRowRangeSelection,
handleSingleRowSelection,
canHaveMultipleSelection,
disableSelectionOnClick,
canHaveMultipleSelection,
checkboxSelection,
apiRef,
expandRowRangeSelection,
handleSingleRowSelection,
],
);

Expand Down
30 changes: 26 additions & 4 deletions packages/grid/x-data-grid/src/tests/rows.DataGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { createRenderer, fireEvent, screen, waitFor } from '@material-ui/monorepo/test/utils';
import { createRenderer, fireEvent, screen } from '@material-ui/monorepo/test/utils';
import { expect } from 'chai';
import { spy, stub } from 'sinon';
import Portal from '@mui/material/Portal';
Expand All @@ -10,7 +10,7 @@ import { getData } from 'storybook/src/data/data-service';
const isJSDOM = /jsdom/.test(window.navigator.userAgent);

describe('<DataGrid /> - Rows', () => {
const { render } = createRenderer();
const { render, clock } = createRenderer({ clock: 'fake' });

const baselineProps = {
autoHeight: isJSDOM,
Expand Down Expand Up @@ -110,7 +110,10 @@ describe('<DataGrid /> - Rows', () => {
});

describe('columnType: actions', () => {
const TestCase = ({ getActions }: { getActions?: () => JSX.Element[] }) => {
const TestCase = ({
getActions,
...other
}: { getActions?: () => JSX.Element[] } & Partial<DataGridProps>) => {
return (
<div style={{ width: 300, height: 300 }}>
<DataGrid
Expand All @@ -123,6 +126,7 @@ describe('<DataGrid /> - Rows', () => {
getActions,
},
]}
{...other}
/>
</div>
);
Expand Down Expand Up @@ -186,7 +190,7 @@ describe('<DataGrid /> - Rows', () => {
);
expect(getRow(0).className).not.to.contain('Mui-selected');
fireEvent.click(screen.getByRole('button', { name: 'more' }));
await waitFor(() => expect(screen.queryByText('print')).not.to.equal(null));
expect(screen.queryByText('print')).not.to.equal(null);
fireEvent.click(screen.queryByText('print'));
expect(getRow(0).className).not.to.contain('Mui-selected');
});
Expand All @@ -197,5 +201,23 @@ describe('<DataGrid /> - Rows', () => {
fireEvent.click(screen.getByRole('button', { name: 'more' }));
expect(getRow(0).className).not.to.contain('Mui-selected');
});

it('should close other menus before opening a new one', () => {
render(
<TestCase
rows={[{ id: 1 }, { id: 2 }]}
getActions={() => [<GridActionsCellItem label="print" showInMenu />]}
/>,
);
expect(screen.queryAllByRole('menu')).to.have.length(0);

fireEvent.click(screen.getAllByRole('button', { name: 'more' })[0]);
clock.runToLast();
expect(screen.queryAllByRole('menu')).to.have.length(1);

fireEvent.click(screen.getAllByRole('button', { name: 'more' })[1]);
clock.runToLast();
expect(screen.queryAllByRole('menu')).to.have.length(1);
});
});
});