Skip to content

Commit

Permalink
[DataGrid] Fix Alt+c being ignored on some systems (#3660)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii authored Jan 26, 2022
1 parent cbf80d0 commit 4e99fa4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ Use the arrow keys to move the focus.
### Key assignment conventions

The above key assignments are for Windows and Linux.
On macOS, replace <kbd class="key">CTRL</kbd> with <kbd class="key">⌘ Command</kbd>.
On macOS:

- replace <kbd class="key">CTRL</kbd> with <kbd class="key">⌘ Command</kbd>
- replace <kbd class="key">ALT</kbd> with <kbd class="key">⌥ Option</kbd>

## API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export const useGridClipboard = (apiRef: GridApiRef): void => {
const handleKeydown = React.useCallback(
(event: KeyboardEvent) => {
const isModifierKeyPressed = event.ctrlKey || event.metaKey || event.altKey;
if (event.key.toLowerCase() !== 'c' || !isModifierKeyPressed) {
// event.key === 'c' is not enough as alt+c can lead to ©, ç, or other characters on macOS.
// event.code === 'KeyC' is not enough as event.code assume a QWERTY keyboard layout which would
// be wrong with a Dvorak keyboard (as if pressing J).
if (String.fromCharCode(event.keyCode) !== 'C' || !isModifierKeyPressed) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ describe('<DataGridPro /> - Clipboard', () => {
describe('copySelectedRowsToClipboard', () => {
let writeText;

before(function beforeHook() {
if (!isJSDOM) {
// Needs permission to read the clipboard
this.skip();
}
});

beforeEach(function beforeEachHook() {
writeText = stub().resolves();

Expand Down Expand Up @@ -92,9 +85,20 @@ describe('<DataGridPro /> - Clipboard', () => {
const cell = getCell(0, 0);
fireEvent.mouseUp(cell);
fireEvent.click(cell);
fireEvent.keyDown(cell, { key: 'c', [key]: true });
fireEvent.keyDown(cell, { key: 'c', keyCode: 67, [key]: true });
expect(writeText.firstCall.args[0]).to.equal(['0\tNike', '1\tAdidas'].join('\r\n'));
});
});

it(`should copy the selected rows and headers to the clipboard when Alt + C is pressed`, () => {
render(<Test />);
apiRef.current.selectRows([0, 1]);
const cell = getCell(0, 0);
fireEvent.mouseUp(cell);
fireEvent.click(cell);
fireEvent.keyDown(cell, { key: 'c', keyCode: 67, altKey: true });
expect(writeText.callCount).to.equal(1, "writeText wasn't called");
expect(writeText.firstCall.args[0]).to.equal(['id\tBrand', '0\tNike'].join('\r\n'));
});
});
});

0 comments on commit 4e99fa4

Please sign in to comment.