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

[DataGridPremium] Fix clipboard paste not working for a single cell on non-first page #14261

Merged
merged 2 commits into from
Aug 19, 2024
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 @@ -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