Skip to content

Commit

Permalink
[ESLint] Prefer KeyboardEvent#key over KeyboardEvent#keyCode (#3126)
Browse files Browse the repository at this point in the history
Co-authored-by: Ted Thibodeau Jr <tthibodeau@openlinksw.com>
  • Loading branch information
dimaMachina and TallTed authored Apr 14, 2023
1 parent 15c26eb commit 4879984
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-rice-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/react': patch
---

Prefer KeyboardEvent#key over KeyboardEvent#keyCode
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ module.exports = {
message:
"`useMemo` with an empty dependency array can't provide a stable reference, use `useRef` instead.",
},
{
// ❌ event.keyCode
selector: 'MemberExpression > .property[type=Identifier][name=keyCode]',
message: 'Use `.key` instead of `.keyCode`',
},
],
'no-ternary': 0,
'no-underscore-dangle': 0,
Expand Down Expand Up @@ -282,6 +287,8 @@ module.exports = {
'unicorn/prefer-node-protocol': 'error',
'import/no-unresolved': ['error', { ignore: ['^node:'] }],
'unicorn/prefer-string-replace-all': 'error',
// doesn't catch a lot of cases; we use ESLint builtin `no-restricted-syntax` to forbid `.keyCode`
'unicorn/prefer-keyboard-event-key': 'off',

'unicorn/prefer-switch': 'error',
// TODO: Fix all errors for the following rules included in recommended config
Expand Down
11 changes: 4 additions & 7 deletions packages/graphiql-react/src/editor/header-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,10 @@ export function useHeaderEditor(
});

newEditor.on('keyup', (editorInstance, event) => {
const code = event.keyCode;
if (
(code >= 65 && code <= 90) || // letters
(!event.shiftKey && code >= 48 && code <= 57) || // numbers
(event.shiftKey && code === 189) || // underscore
(event.shiftKey && code === 222) // "
) {
const { keyCode, key, shiftKey } = event;
const isLetter = keyCode >= 65 && keyCode <= 90;
const isNumber = keyCode >= 48 && keyCode <= 57;
if (isLetter || (!shiftKey && isNumber) || key === '_' || key === '"') {
editorInstance.execCommand('autocomplete');
}
});
Expand Down
11 changes: 4 additions & 7 deletions packages/graphiql-react/src/editor/variable-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,10 @@ export function useVariableEditor(
});

newEditor.on('keyup', (editorInstance, event) => {
const code = event.keyCode;
if (
(code >= 65 && code <= 90) || // letters
(!event.shiftKey && code >= 48 && code <= 57) || // numbers
(event.shiftKey && code === 189) || // underscore
(event.shiftKey && code === 222) // "
) {
const { keyCode, key, shiftKey } = event;
const isLetter = keyCode >= 65 && keyCode <= 90;
const isNumber = keyCode >= 48 && keyCode <= 57;
if (isLetter || (!shiftKey && isNumber) || key === '_' || key === '"') {
editorInstance.execCommand('autocomplete');
}
});
Expand Down
5 changes: 3 additions & 2 deletions packages/graphiql-react/src/explorer/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ export function Search() {

useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (event.metaKey && event.keyCode === 75 && inputRef.current) {
inputRef.current.focus();
if (event.metaKey && event.key === 'k') {
inputRef.current?.focus();
}
}

window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, []);
Expand Down
6 changes: 2 additions & 4 deletions packages/graphiql-react/src/history/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ export function HistoryItem(props: QueryHistoryItemProps) {
defaultValue={props.item.label}
ref={inputRef}
onKeyDown={e => {
if (e.keyCode === 27) {
// Escape
if (e.key === 'Esc') {
setIsEditable(false);
} else if (e.keyCode === 13) {
// Enter
} else if (e.key === 'Enter') {
setIsEditable(false);
editLabel({ ...props.item, label: e.currentTarget.value });
}
Expand Down
4 changes: 3 additions & 1 deletion packages/graphiql-react/src/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
const counter = ++counterRef.current;

const maybeIntrospectionData = props.schema;

async function fetchIntrospectionData() {
if (maybeIntrospectionData) {
// No need to introspect if we already have the data
Expand Down Expand Up @@ -317,10 +318,11 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
*/
useEffect(() => {
function triggerIntrospection(event: KeyboardEvent) {
if (event.keyCode === 82 && event.shiftKey && event.ctrlKey) {
if (event.ctrlKey && event.key === 'R') {
introspect();
}
}

window.addEventListener('keydown', triggerIntrospection);
return () => window.removeEventListener('keydown', triggerIntrospection);
});
Expand Down

0 comments on commit 4879984

Please sign in to comment.