Skip to content

Commit 1a9fdca

Browse files
jj49411Chengnstepienamanmahajan7
authored
Allow copying highlighted text and prevent editing when pressing ctrl+input key (#3431)
* Allow copying highlighted text * Prevent editing when pressing ctrl+input key * Add test * Refactor conditions * Revert changes * Update test/copyPaste.test.tsx Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com> * Move comments * Check selection and add comment * Refactor getSelection * Allow ctrl+space * Check control v inside isDefaultCellInput * Refactor copying text --------- Co-authored-by: Cheng <ycheng074@emea.comcast.com> Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com> Co-authored-by: Aman Mahajan <amahajan@stratag.com>
1 parent 30165db commit 1a9fdca

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/DataGrid.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ function DataGrid<R, SR, K extends Key>(
559559
const cKey = 67;
560560
const vKey = 86;
561561
if (keyCode === cKey) {
562+
// copy highlighted text only
563+
if (window.getSelection()?.isCollapsed === false) return;
562564
handleCopy();
563565
return;
564566
}

src/utils/keyboardUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export function isCtrlKeyHeldDown(e: React.KeyboardEvent): boolean {
5353
}
5454

5555
export function isDefaultCellInput(event: React.KeyboardEvent<HTMLDivElement>): boolean {
56+
const vKey = 86;
57+
if (isCtrlKeyHeldDown(event) && event.keyCode !== vKey) return false;
5658
return !nonInputKeys.has(event.key);
5759
}
5860

test/copyPaste.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,10 @@ test('should not allow paste on header or summary cells', async () => {
200200
expect(getSelectedCell()).toHaveTextContent('s1');
201201
expect(onPasteSpy).not.toHaveBeenCalled();
202202
});
203+
204+
test('should not start editing when pressing ctrl+<input key>', async () => {
205+
setup();
206+
await userEvent.click(getCellsAtRowIndex(1)[0]);
207+
await userEvent.keyboard('{Control>}b');
208+
expect(getSelectedCell()).not.toHaveClass('rdg-editor-container');
209+
});

0 commit comments

Comments
 (0)