Skip to content

Commit

Permalink
[DataGridPremium] Make clipboard copy respect the sorting during cell…
Browse files Browse the repository at this point in the history
… selection (#12235)
  • Loading branch information
MBilalShafi authored Feb 28, 2024
1 parent ffedb8e commit dd38268
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
gridFocusCellSelector,
GridCellParams,
GRID_REORDER_COL_DEF,
useGridSelector,
gridSortedRowIdsSelector,
} from '@mui/x-data-grid-pro';
import { gridCellSelectionStateSelector } from './gridCellSelectionSelector';
import { GridCellSelectionApi } from './gridCellSelectionInterfaces';
Expand Down Expand Up @@ -61,6 +63,7 @@ export const useGridCellSelection = (
const lastMouseDownCell = React.useRef<GridCellCoordinates | null>();
const mousePosition = React.useRef<{ x: number; y: number } | null>(null);
const autoScrollRAF = React.useRef<number | null>();
const sortedRowIds = useGridSelector(apiRef, gridSortedRowIdsSelector);

const ignoreValueFormatterProp = props.ignoreValueFormatterDuringExport;
const ignoreValueFormatter =
Expand Down Expand Up @@ -547,7 +550,11 @@ export const useGridCellSelection = (
return value;
}
const cellSelectionModel = apiRef.current.getCellSelectionModel();
const copyData = Object.keys(cellSelectionModel).reduce((acc, rowId) => {
const unsortedSelectedRowIds = Object.keys(cellSelectionModel);
const sortedSelectedRowIds = sortedRowIds.filter((id) =>
unsortedSelectedRowIds.includes(`${id}`),
);
const copyData = sortedSelectedRowIds.reduce<string>((acc, rowId) => {
const fieldsMap = cellSelectionModel[rowId];
const rowString = Object.keys(fieldsMap).reduce((acc2, field) => {
let cellData: string;
Expand All @@ -566,7 +573,7 @@ export const useGridCellSelection = (
}, '');
return copyData;
},
[apiRef, ignoreValueFormatter, clipboardCopyCellDelimiter],
[apiRef, ignoreValueFormatter, clipboardCopyCellDelimiter, sortedRowIds],
);

useGridRegisterPipeProcessor(apiRef, 'isCellSelected', checkIfCellIsSelected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ describe('<DataGridPremium /> - Clipboard', () => {
fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true });
expect(writeText.firstCall.args[0]).to.equal([['0', 'USDGBP', '1'].join('\t')].join('\r\n'));
});

it(`should copy cells range selected based on their sorted order`, () => {
const columns = [{ field: 'brand' }];
const rows = [
{ id: 0, brand: 'Nike' },
{ id: 1, brand: 'Adidas' },
{ id: 2, brand: 'Puma' },
];
render(
<DataGridPremium
columns={columns}
rows={rows}
cellSelection
sortModel={[{ field: 'brand', sort: 'asc' }]}
/>,
);

const cell = getCell(0, 0);
cell.focus();
userEvent.mousePress(cell);

fireEvent.keyDown(cell, { key: 'Ctrl' });
fireEvent.click(getCell(1, 0), { ctrlKey: true });

fireEvent.keyDown(cell, { key: 'Ctrl' });
fireEvent.click(getCell(2, 0), { ctrlKey: true });

fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true });
expect(writeText.lastCall.firstArg).to.equal(['Adidas', 'Nike', 'Puma'].join('\r\n'));
});
});

describe('paste', () => {
Expand Down

0 comments on commit dd38268

Please sign in to comment.