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

[DataGridPro] Make Row reordering work with pagination #15355

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions docs/pages/x/api/data-grid/selectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@
"description": "",
"supportsApiRef": true
},
{
"name": "gridRowIndexLookupSelector",
"returnType": "Record<GridRowId, number>",
"description": "",
"supportsApiRef": true
},
{
"name": "gridRowMaximumTreeDepthSelector",
"returnType": "number",
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

I noticed it doesn't work as expected with filtered data.

row-reorder.mp4

Could we make it work properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this seems like a separate problem of row reordering with filtered data, also not working in v7 and in v6
v7- https://mui.com/x/react-data-grid/row-ordering/
v6- https://v6.mui.com/x/react-data-grid/row-ordering/

but it is working at the time when row reordering is implemented in #4034
v5- https://deploy-preview-4034--material-ui-x.netlify.app/x/react-data-grid/rows/#row-reorder (working)

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
gridRowMaximumTreeDepthSelector,
useGridApiOptionHandler,
GridRowId,
gridRowIndexLookupSelector,
} from '@mui/x-data-grid';
import { gridEditRowsStateSelector } from '@mui/x-data-grid/internals';
import { GridRowOrderChangeParams } from '../../../models/gridRowOrderChangeParams';
Expand Down Expand Up @@ -63,6 +64,8 @@ export const useGridRowReorder = (
const ownerState = { classes: props.classes };
const classes = useUtilityClasses(ownerState);
const [dragRowId, setDragRowId] = React.useState<GridRowId>('');
const lookup = useGridSelector(apiRef, gridRowIndexLookupSelector);
const getRowIndex = React.useCallback((id: GridRowId) => lookup[id], [lookup]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should we add this method to the GridRowApi ?


React.useEffect(() => {
return () => {
Expand Down Expand Up @@ -91,17 +94,16 @@ export const useGridRowReorder = (

dragRowNode.current = event.currentTarget;
dragRowNode.current.classList.add(classes.rowDragging);

setDragRowId(params.id);

removeDnDStylesTimeout.current = setTimeout(() => {
dragRowNode.current!.classList.remove(classes.rowDragging);
});

originRowIndex.current = apiRef.current.getRowIndexRelativeToVisibleRows(params.id);
originRowIndex.current = getRowIndex(params.id);
apiRef.current.setCellFocus(params.id, GRID_REORDER_COL_DEF.field);
},
[isRowReorderDisabled, classes.rowDragging, logger, apiRef],
[apiRef, isRowReorderDisabled, logger, classes.rowDragging, getRowIndex],
);

const handleDragOver = React.useCallback<GridEventListener<'cellDragOver' | 'rowDragOver'>>(
Expand All @@ -127,7 +129,7 @@ export const useGridRowReorder = (
: event.clientY;

if (params.id !== dragRowId) {
const targetRowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(params.id);
const targetRowIndex = getRowIndex(params.id);

const dragDirection = mouseMovementDiff > 0 ? Direction.DOWN : Direction.UP;
const currentReorderState: ReorderStateProps = {
Expand All @@ -149,7 +151,7 @@ export const useGridRowReorder = (

previousMousePosition = { x: event.clientX, y: event.clientY };
},
[apiRef, logger, dragRowId],
[dragRowId, apiRef, logger, getRowIndex],
);

const handleDragEnd = React.useCallback<GridEventListener<'rowDragEnd'>>(
Expand Down Expand Up @@ -179,7 +181,7 @@ export const useGridRowReorder = (
// Emit the rowOrderChange event only once when the reordering stops.
const rowOrderChangeParams: GridRowOrderChangeParams = {
row: apiRef.current.getRow(dragRowId)!,
targetIndex: apiRef.current.getRowIndexRelativeToVisibleRows(params.id),
targetIndex: getRowIndex(params.id),
oldIndex: originRowIndex.current!,
};

Expand All @@ -188,7 +190,7 @@ export const useGridRowReorder = (

setDragRowId('');
},
[isRowReorderDisabled, logger, apiRef, dragRowId],
[apiRef, dragRowId, isRowReorderDisabled, logger, getRowIndex],
);

useGridApiEventHandler(apiRef, 'rowDragStart', handleDragStart);
Expand Down
50 changes: 48 additions & 2 deletions packages/x-data-grid-pro/src/tests/rowReorder.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer, fireEvent, createEvent } from '@mui/internal-test-utils';
import { getCell, getRowsFieldContent } from 'test/utils/helperFn';
import { createRenderer, fireEvent, screen, createEvent } from '@mui/internal-test-utils';
import { getCell, getColumnValues, getRowsFieldContent } from 'test/utils/helperFn';
import { useGridApiRef, DataGridPro, gridClasses, GridApi } from '@mui/x-data-grid-pro';
import { useBasicDemoData } from '@mui/x-data-grid-generator';

Expand Down Expand Up @@ -200,4 +200,50 @@ describe('<DataGridPro /> - Row reorder', () => {
expect(handleDragOver.callCount).to.equal(0);
expect(handleDragEnd.callCount).to.equal(0);
});

it('should reorder rows correctly on any page when pagination is enabled', () => {
let apiRef: React.MutableRefObject<GridApi>;
const rows = [
{ id: 0, brand: 'Nike' },
{ id: 1, brand: 'Adidas' },
{ id: 2, brand: 'Puma' },
{ id: 3, brand: 'Skechers' },
];
const columns = [{ field: 'brand' }];

function Test() {
apiRef = useGridApiRef();

return (
<div style={{ width: 300, height: 300 }}>
<DataGridPro
apiRef={apiRef}
rows={rows}
columns={columns}
rowReordering
pagination
initialState={{
pagination: {
paginationModel: { pageSize: 2 },
},
}}
pageSizeOptions={[2]}
/>
</div>
);
}

render(<Test />);
fireEvent.click(screen.getByRole('button', { name: /next page/i }));
expect(getColumnValues(0)).to.deep.equal(['2', '3']);
expect(getRowsFieldContent('brand')).to.deep.equal(['Puma', 'Skechers']);
const rowReorderCell = getCell(2, 0).firstChild!;
const targetCell = getCell(3, 0);

fireEvent.dragStart(rowReorderCell);
fireEvent.dragEnter(targetCell);
const dragOverEvent = createDragOverEvent(targetCell);
fireEvent(targetCell, dragOverEvent);
expect(getRowsFieldContent('brand')).to.deep.equal(['Skechers', 'Puma']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,13 @@ export const gridFilterActiveItemsLookupSelector = createSelectorMemoized(
return result;
},
);

export const gridRowIndexLookupSelector = createSelectorMemoized(
gridExpandedSortedRowEntriesSelector,
(visibleSortedRows) => {
return visibleSortedRows.reduce<Record<GridRowId, number>>((acc, { id }, index) => {
acc[id] = index;
return acc;
}, {});
},
);
1 change: 1 addition & 0 deletions packages/x-data-grid/src/hooks/features/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export {
gridFilteredDescendantRowCountSelector,
gridFilterActiveItemsSelector,
gridFilterActiveItemsLookupSelector,
gridRowIndexLookupSelector,
} from './gridFilterSelector';
export type { GridFilterActiveItemsLookup } from './gridFilterSelector';
1 change: 1 addition & 0 deletions scripts/x-data-grid-premium.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@
{ "name": "GridRowId", "kind": "TypeAlias" },
{ "name": "GridRowIdGetter", "kind": "TypeAlias" },
{ "name": "GridRowIdToModelLookup", "kind": "TypeAlias" },
{ "name": "gridRowIndexLookupSelector", "kind": "Variable" },
{ "name": "gridRowMaximumTreeDepthSelector", "kind": "Variable" },
{ "name": "GridRowMode", "kind": "TypeAlias" },
{ "name": "GridRowModel", "kind": "TypeAlias" },
Expand Down
1 change: 1 addition & 0 deletions scripts/x-data-grid-pro.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@
{ "name": "GridRowId", "kind": "TypeAlias" },
{ "name": "GridRowIdGetter", "kind": "TypeAlias" },
{ "name": "GridRowIdToModelLookup", "kind": "TypeAlias" },
{ "name": "gridRowIndexLookupSelector", "kind": "Variable" },
{ "name": "gridRowMaximumTreeDepthSelector", "kind": "Variable" },
{ "name": "GridRowMode", "kind": "TypeAlias" },
{ "name": "GridRowModel", "kind": "TypeAlias" },
Expand Down
1 change: 1 addition & 0 deletions scripts/x-data-grid.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@
{ "name": "GridRowId", "kind": "TypeAlias" },
{ "name": "GridRowIdGetter", "kind": "TypeAlias" },
{ "name": "GridRowIdToModelLookup", "kind": "TypeAlias" },
{ "name": "gridRowIndexLookupSelector", "kind": "Variable" },
{ "name": "gridRowMaximumTreeDepthSelector", "kind": "Variable" },
{ "name": "GridRowMode", "kind": "TypeAlias" },
{ "name": "GridRowModel", "kind": "TypeAlias" },
Expand Down