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

[data grid][pickers][tree-view] Fix shortcut with localization keyboard #14220

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Changes from 3 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
@@ -4,6 +4,7 @@ import { useGridApiOptionHandler, useGridNativeEventListener } from '../../utils
import { gridFocusCellSelector } from '../focus/gridFocusStateSelector';
import { serializeCellValue } from '../export/serializers/csvSerializer';
import type { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { isCopyShortcut } from '../../../utils/keyboardUtils';

function writeToClipboardPolyfill(data: string) {
const span = document.createElement('span');
@@ -74,14 +75,7 @@ export const useGridClipboard = (

const handleCopy = React.useCallback(
(event: KeyboardEvent) => {
if (
!(
(event.ctrlKey || event.metaKey) &&
event.key.toLowerCase() === 'c' &&
!event.shiftKey &&
!event.altKey
)
) {
if (!isCopyShortcut(event)) {
return;
}

2 changes: 1 addition & 1 deletion packages/x-data-grid/src/internals/index.ts
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ export {
getActiveElement,
isEventTargetInPortal,
} from '../utils/domUtils';
export { isNavigationKey, isPasteShortcut } from '../utils/keyboardUtils';
export { isNavigationKey, isPasteShortcut, isCopyShortcut } from '../utils/keyboardUtils';
export * from '../utils/utils';
export { exportAs } from '../utils/exportAs';
export * from '../utils/getPublicApiRef';
24 changes: 18 additions & 6 deletions packages/x-data-grid/src/utils/keyboardUtils.ts
Original file line number Diff line number Diff line change
@@ -45,13 +45,25 @@ export const isHideMenuKey = (key: React.KeyboardEvent['key']) => key === 'Tab'
// In theory, on macOS, ctrl + v doesn't trigger a paste, so the function should return false.
// However, maybe it's overkill to fix, so let's be lazy.
export function isPasteShortcut(event: React.KeyboardEvent) {
if (
return (
(event.ctrlKey || event.metaKey) &&
event.key.toLowerCase() === 'v' &&
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
// We can't use event.code === 'KeyV' as event.code assumes a QWERTY keyboard layout,
// for example, it would be another letter on a Dvorak physical keyboard.
// We can't use event.key === 'v' as event.key is not stable with key modifiers and keyboard layouts,
// for example, it would be ה on a Hebrew keyboard layout.
// https://github.com/w3c/uievents/issues/377 could be a long-term solution
String.fromCharCode(event.keyCode) === 'V' &&
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
!event.shiftKey &&
!event.altKey
) {
return true;
}
return false;
);
}

// Checks if the keyboard event corresponds to the copy shortcut (CTRL+C or CMD+C) across different localization keyboards.
export function isCopyShortcut(event: KeyboardEvent): boolean {
return (
(event.ctrlKey || event.metaKey) &&
String.fromCharCode(event.keyCode) === 'C' &&
!event.shiftKey &&
!event.altKey
);
}
Original file line number Diff line number Diff line change
@@ -125,7 +125,7 @@ export const useField = <
switch (true) {
// Select all
case (event.ctrlKey || event.metaKey) &&
event.key.toLowerCase() === 'a' &&
String.fromCharCode(event.keyCode) === 'A' &&
!event.shiftKey &&
!event.altKey: {
// prevent default to make sure that the next line "select all" while updating