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

[DataGrid] Fix Alt+c being ignored on some systems #3660

Merged
merged 9 commits into from
Jan 26, 2022
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 @@ -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) {
Copy link
Member

@oliviertassinari oliviertassinari Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have learned something new here. I didn't know about String.fromCharCode, nice trick 👍

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) {
cherniavskii marked this conversation as resolved.
Show resolved Hide resolved
// 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'));
});
});
});