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] Remove Alt+C keyboard shortcut #7466

Merged
merged 2 commits into from
Jan 13, 2023
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
1 change: 0 additions & 1 deletion docs/data/data-grid/accessibility/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ On macOS:
| <kbd class="key">Shift</kbd>+ Click on cell | Select the range of rows between the first and the last clicked rows |
| <kbd><kbd class="key">Ctrl</kbd>+<kbd class="key">A</kbd></kbd> | Select all rows |
| <kbd><kbd class="key">Ctrl</kbd>+<kbd class="key">C</kbd></kbd> | Copy the currently selected rows |
| <kbd><kbd class="key">Alt</kbd>+<kbd class="key">C</kbd></kbd> | Copy the currently selected rows, including headers |
| <kbd class="key">Ctrl</kbd>+ Click on cell | Enable multi-selection |
| <kbd class="key">Ctrl</kbd>+ Click on a selected row | Deselect the row |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ describe('<DataGridPro /> - Clipboard', () => {
expect(writeText.firstCall.args[0]).to.equal(['0\tNike', '1\tAdidas'].join('\r\n'));
});

it('should include the headers when includeHeaders=true', () => {
render(<Test />);
act(() => apiRef.current.selectRows([0, 1]));
act(() => apiRef.current.unstable_copySelectedRowsToClipboard(true));
expect(writeText.firstCall.args[0]).to.equal(
['id\tBrand', '0\tNike', '1\tAdidas'].join('\r\n'),
);
});

['ctrlKey', 'metaKey'].forEach((key) => {
it(`should copy the selected rows to the clipboard when ${key} + C is pressed`, () => {
render(<Test disableRowSelectionOnClick />);
Expand All @@ -90,15 +81,5 @@ describe('<DataGridPro /> - Clipboard', () => {
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 />);
act(() => apiRef.current.selectRows([0, 1]));
const cell = getCell(0, 0);
userEvent.mousePress(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'));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,28 @@ function hasNativeSelection(element: HTMLInputElement) {
export const useGridClipboard = (apiRef: React.MutableRefObject<GridPrivateApiCommunity>): void => {
const copySelectedRowsToClipboard = React.useCallback<
GridClipboardApi['unstable_copySelectedRowsToClipboard']
>(
(includeHeaders = false) => {
if (apiRef.current.getSelectedRows().size === 0) {
return;
}

const data = apiRef.current.getDataAsCsv({
includeHeaders,
delimiter: '\t',
});

if (navigator.clipboard) {
navigator.clipboard.writeText(data).catch(() => {
writeToClipboardPolyfill(data);
});
} else {
>(() => {
if (apiRef.current.getSelectedRows().size === 0) {
return;
}

const data = apiRef.current.getDataAsCsv({
includeHeaders: false,
delimiter: '\t',
});

if (navigator.clipboard) {
navigator.clipboard.writeText(data).catch(() => {
writeToClipboardPolyfill(data);
}
},
[apiRef],
);
});
} else {
writeToClipboardPolyfill(data);
}
}, [apiRef]);

const handleKeydown = React.useCallback(
(event: KeyboardEvent) => {
const isModifierKeyPressed = event.ctrlKey || event.metaKey || event.altKey;
// event.key === 'c' is not enough as alt+c can lead to ©, ç, or other characters on macOS.
const isModifierKeyPressed = event.ctrlKey || event.metaKey;
// 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) {
Expand All @@ -85,7 +81,7 @@ export const useGridClipboard = (apiRef: React.MutableRefObject<GridPrivateApiCo
return;
}

apiRef.current.unstable_copySelectedRowsToClipboard(event.altKey);
apiRef.current.unstable_copySelectedRowsToClipboard();
},
[apiRef],
);
Expand Down
3 changes: 1 addition & 2 deletions packages/grid/x-data-grid/src/models/api/gridClipboardApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export interface GridClipboardApi {
/**
* Copies the selected rows to the clipboard.
* The fields will be separated by the TAB character.
* @param {boolean} includeHeaders Whether to include the headers or not. Default is `false`.
* @ignore - do not document.
*/
unstable_copySelectedRowsToClipboard: (includeHeaders?: boolean) => void;
unstable_copySelectedRowsToClipboard: () => void;
}