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

[XGrid] Close column header menu when resizing column #1989

Merged
merged 23 commits into from
Jul 7, 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 @@ -3,6 +3,7 @@ import clsx from 'clsx';
import { useGridSelector } from '../../hooks/features/core/useGridSelector';
import { optionsSelector } from '../../hooks/utils/optionsSelector';
import { useGridApiContext } from '../../hooks/root/useGridApiContext';
import { GRID_COLUMN_HEADER_SEPARATOR_RESIZABLE_CSS_CLASS } from '../../constants';

export interface GridColumnHeaderSeparatorProps extends React.HTMLAttributes<HTMLDivElement> {
resizable: boolean;
Expand All @@ -27,7 +28,7 @@ export const GridColumnHeaderSeparator = React.memo(function GridColumnHeaderSep
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
<div
className={clsx('MuiDataGrid-columnSeparator', {
'MuiDataGrid-columnSeparator--resizable': resizable,
[GRID_COLUMN_HEADER_SEPARATOR_RESIZABLE_CSS_CLASS]: resizable,
'Mui-resizing': resizing,
})}
style={{ minHeight: height, opacity: showColumnRightBorder ? 0 : 1 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function GridColumnHeadersItemCollection(props: GridColumnHeadersItemColl
: -1;
const hasFocus = columnHeaderFocus !== null && columnHeaderFocus.field === col.field;
const open = columnMenuState.open && columnMenuState.field === col.field;

return (
<GridColumnHeaderItem
key={col.field}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { GridApiRef } from '../../../models/api/gridApiRef';
import { useGridApiMethod } from '../../root/useGridApiMethod';
import { useLogger } from '../../utils/useLogger';
import { useGridState } from '../core/useGridState';
import { useGridApiEventHandler } from '../../root';
import { GRID_COLUMN_RESIZE_START } from '../../../constants';

export const useGridColumnMenu = (apiRef: GridApiRef): void => {
const logger = useLogger('useGridColumnMenu');
Expand Down Expand Up @@ -42,6 +44,22 @@ export const useGridColumnMenu = (apiRef: GridApiRef): void => {
[logger, showColumnMenu, hideColumnMenu, gridState],
);

const handleColumnResizeStart = React.useCallback(() => {
setGridState((oldState) => {
if (oldState.columnMenu.open) {
return {
...oldState,
columnMenu: {
...oldState.columnMenu,
open: false,
},
};
}

return oldState;
});
}, [setGridState]);

React.useEffect(() => {
if (gridState.isScrolling) {
hideColumnMenu();
Expand All @@ -57,4 +75,6 @@ export const useGridColumnMenu = (apiRef: GridApiRef): void => {
},
'ColumnMenuApi',
);

useGridApiEventHandler(apiRef, GRID_COLUMN_RESIZE_START, handleColumnResizeStart);
};
101 changes: 101 additions & 0 deletions packages/grid/x-grid/src/tests/columnHeaders.XGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as React from 'react';
import {
createClientRenderStrictMode,
// @ts-expect-error need to migrate helpers to TypeScript
fireEvent,
// @ts-expect-error need to migrate helpers to TypeScript
screen,
// @ts-expect-error need to migrate helpers to TypeScript
waitFor,
} from 'test/utils';
import { expect } from 'chai';
import { GRID_COLUMN_HEADER_SEPARATOR_RESIZABLE_CSS_CLASS, XGrid } from '@material-ui/x-grid';
import { getColumnHeaderCell } from 'test/utils/helperFn';

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

describe('<XGrid /> - Column Headers', () => {
// TODO v5: replace with createClientRender
const render = createClientRenderStrictMode();

const baselineProps = {
autoHeight: isJSDOM,
disableColumnResize: false,
rows: [
{
id: 0,
brand: 'Nike',
foundationYear: 1964,
},
{
id: 1,
brand: 'Adidas',
foundationYear: 1949,
},
{
id: 2,
brand: 'Puma',
foundationYear: 1948,
},
],
};

describe('GridColumnHeaderMenu', () => {
it('should close the menu of a column when resizing this column', async () => {
render(
<div style={{ width: 300, height: 500 }}>
<XGrid
{...baselineProps}
columns={[
{ field: 'brand', resizable: true },
{ field: 'foundationYear', resizable: true },
]}
/>
</div>,
);

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));

const separator = columnCell.querySelector('.MuiDataGrid-iconSeparator');
fireEvent.mouseDown(separator);
// TODO remove mouseUp once useGridColumnReorder will handle cleanup properly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will likely get this for free with a fix of #2007.

fireEvent.mouseUp(separator);
await waitFor(() => expect(screen.queryByRole('menu')).to.equal(null));
});

it('should close the menu of a column when resizing another column', async () => {
render(
<div style={{ width: 300, height: 500 }}>
<XGrid
{...baselineProps}
columns={[
{ field: 'brand', resizable: true },
{ field: 'foundationYear', resizable: true },
]}
/>
</div>,
);

const columnWithMenuCell = getColumnHeaderCell(0);
const columnToResizeCell = getColumnHeaderCell(1);

const menuIconButton = columnWithMenuCell.querySelector('button[aria-label="Menu"]');

fireEvent.click(menuIconButton);
await waitFor(() => expect(screen.queryByRole('menu')).not.to.equal(null));
Copy link
Member

@oliviertassinari oliviertassinari Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we wouldn't need for an await but it's not obvious how we could make it happen. Maybe we would need to disable the transition in the render factory helper, or to mock the time. I'm raising the point because await often lead to slower and less reliable tests. Anyway, not a blocker.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also in favor of fixing this behavior in a later PR to avoid blocking this one


const separator = columnToResizeCell.querySelector(
`.${GRID_COLUMN_HEADER_SEPARATOR_RESIZABLE_CSS_CLASS}`,
);
fireEvent.mouseDown(separator);
// TODO remove mouseUp once useGridColumnReorder will handle cleanup properly
fireEvent.mouseUp(separator);
await waitFor(() => expect(screen.queryByRole('menu')).to.equal(null));
});
});
});