Skip to content

Commit

Permalink
[DataGridPremium] Clipboard paste not working for a single cell if th…
Browse files Browse the repository at this point in the history
…e current page is not the first page (mui#14261)

Signed-off-by: Armin Mehinovic <4390250+arminmeh@users.noreply.github.com>
Co-authored-by: Bilal Shafi <bilalshafidev@gmail.com>
  • Loading branch information
arminmeh and MBilalShafi authored Aug 19, 2024
1 parent 0ee2e68 commit b4f0999
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ function defaultPasteResolver({
apiRef,
updateCell,
pagination,
paginationMode,
}: {
pastedData: string[][];
apiRef: React.MutableRefObject<GridApiPremium>;
updateCell: CellValueUpdater['updateCell'];
pagination: DataGridPremiumProcessedProps['pagination'];
paginationMode: DataGridPremiumProcessedProps['paginationMode'];
}) {
const isSingleValuePasted = pastedData.length === 1 && pastedData[0].length === 1;

Expand Down Expand Up @@ -297,9 +299,10 @@ function defaultPasteResolver({

const selectedRowId = selectedCell.id;
const selectedRowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(selectedRowId);
const visibleRowIds = pagination
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
: gridExpandedSortedRowIdsSelector(apiRef);
const visibleRowIds =
pagination && paginationMode === 'client'
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
: gridExpandedSortedRowIdsSelector(apiRef);

const selectedFieldIndex = visibleColumnFields.indexOf(selectedCell.field);
pastedData.forEach((rowData, index) => {
Expand Down Expand Up @@ -342,7 +345,7 @@ export const useGridClipboardImport = (

const splitClipboardPastedText = props.splitClipboardPastedText;

const { pagination, onBeforeClipboardPasteStart } = props;
const { pagination, paginationMode, onBeforeClipboardPasteStart } = props;

const handlePaste = React.useCallback<GridEventListener<'cellKeyDown'>>(
async (params, event) => {
Expand Down Expand Up @@ -403,6 +406,7 @@ export const useGridClipboardImport = (
cellUpdater.updateCell(...args);
},
pagination,
paginationMode,
});

cellUpdater.applyUpdates();
Expand All @@ -416,6 +420,7 @@ export const useGridClipboardImport = (
rootEl,
splitClipboardPastedText,
pagination,
paginationMode,
onBeforeClipboardPasteStart,
logger,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,49 @@ describe('<DataGridPremium /> - Clipboard', () => {
expect(getCell(2, 2)).to.have.text(clipboardData);
});

// Context: https://github.com/mui/mui-x/issues/14233
it('should paste into cells on the current page when `paginationMode="server"`', async () => {
const rowLength = 4;

const { setProps } = render(
<Test
rowLength={rowLength}
pagination
paginationModel={{ pageSize: 2, page: 0 }}
paginationMode="server"
pageSizeOptions={[2]}
rowCount={rowLength}
editMode="cell"
/>,
);

const clipboardData = '12';
const cell = getCell(3, 1); // cell in the first row on the next page

expect(cell).not.to.have.text(clipboardData);

cell.focus();
userEvent.mousePress(cell);
paste(cell, clipboardData);

// no update
await waitFor(() => {
expect(getCell(3, 1)).not.to.have.text(clipboardData);
});

// go to the next page
setProps({ paginationModel: { pageSize: 2, page: 1 } });

cell.focus();
userEvent.mousePress(cell);
paste(cell, clipboardData);

// updated
await waitFor(() => {
expect(getCell(3, 1)).to.have.text(clipboardData);
});
});

it('should not paste values outside of the selected cells range', async () => {
render(<Test rowLength={5} colLength={5} />);

Expand Down

0 comments on commit b4f0999

Please sign in to comment.