Skip to content

Commit 5847702

Browse files
authored
refactor: rewrite internal keyboard utils in typescript (#18707)
1 parent e8c5872 commit 5847702

File tree

13 files changed

+179
-208
lines changed

13 files changed

+179
-208
lines changed

packages/react/src/components/FileUploader/FileUploader.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -242,12 +242,7 @@ const FileUploader = React.forwardRef(
242242
iconDescription={iconDescription}
243243
status={filenameStatus}
244244
onKeyDown={(evt) => {
245-
if (
246-
matches(evt as unknown as Event, [
247-
keys.Enter,
248-
keys.Space,
249-
])
250-
) {
245+
if (matches(evt, [keys.Enter, keys.Space])) {
251246
handleClick(evt, { index, filenameStatus });
252247
}
253248
}}

packages/react/src/components/FileUploader/FileUploaderDropContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function FileUploaderDropContainer({
204204
className={dropareaClasses}
205205
ref={innerRef}
206206
onKeyDown={(evt) => {
207-
if (matches(evt as unknown as Event, [keys.Enter, keys.Space])) {
207+
if (matches(evt, [keys.Enter, keys.Space])) {
208208
evt.preventDefault();
209209
inputRef.current?.click();
210210
}

packages/react/src/components/FileUploader/FileUploaderItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -156,7 +156,7 @@ function FileUploaderItem({
156156
: undefined
157157
}
158158
onKeyDown={(evt) => {
159-
if (matches(evt as unknown as Event, [keys.Enter, keys.Space])) {
159+
if (matches(evt, [keys.Enter, keys.Space])) {
160160
if (status === 'edit') {
161161
evt.preventDefault();
162162
onDelete(evt, { uuid: id });

packages/react/src/components/MultiSelect/FilterableMultiSelect.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ const FilterableMultiSelect = React.forwardRef(function FilterableMultiSelect<
680680
event?: KeyboardEvent<Element> | MouseEvent<HTMLButtonElement>
681681
) {
682682
const value = textInput.current?.value;
683-
if (value?.length === 1 || (event && match(event, keys.Escape))) {
683+
if (
684+
value?.length === 1 ||
685+
(event && 'key' in event && match(event, keys.Escape))
686+
) {
684687
setInputValue('');
685688
} else {
686689
setInputValue(value ?? '');

packages/react/src/components/Tabs/Tabs.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import React, {
2121
type ReactNode,
2222
type MouseEvent,
2323
type KeyboardEvent,
24-
type SyntheticEvent,
2524
type HTMLAttributes,
2625
type RefObject,
2726
type ComponentType,
@@ -306,7 +305,7 @@ TabsVertical.propTypes = {
306305
* given a count of the total items and the current index
307306
*/
308307
function getNextIndex(
309-
event: SyntheticEvent,
308+
event: KeyboardEvent,
310309
total: number,
311310
index: number
312311
): number {
@@ -333,7 +332,7 @@ function getNextIndex(
333332
* given a count of the total items and the current index
334333
*/
335334
function getNextIndexVertical(
336-
event: SyntheticEvent,
335+
event: KeyboardEvent,
337336
total: number,
338337
index: number
339338
): number {

packages/react/src/components/Tooltip/Tooltip.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -168,7 +168,7 @@ const Tooltip: TooltipComponent = React.forwardRef(
168168
}
169169

170170
const onKeyDown = useCallback(
171-
(event: React.SyntheticEvent | Event) => {
171+
(event: KeyboardEvent) => {
172172
if (open && match(event, keys.Escape)) {
173173
event.stopPropagation();
174174
setOpen(false);

packages/react/src/components/UIShell/SideNav.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -211,7 +211,7 @@ function SideNavRenderFunction(
211211
};
212212
}
213213

214-
useWindowEvent('keydown', (event: Event) => {
214+
useWindowEvent('keydown', (event) => {
215215
const focusedElement = document.activeElement;
216216

217217
if (
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

88
export * as keys from './keys';
9-
export { match, matches, getCharacterFor } from './match';
10-
export { getNextIndex } from './navigation';
9+
export * from './match';
10+
export * from './navigation';
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
export const Tab = {
8+
import { KeyboardEvent } from 'react';
9+
10+
export type Key = Pick<KeyboardEvent, 'which' | 'keyCode' | 'code'> & {
11+
key?: string | string[];
12+
};
13+
14+
export const Tab: Key = {
915
key: 'Tab',
1016
which: 9,
1117
keyCode: 9,
1218
code: 'Tab',
1319
};
1420

15-
export const Enter = {
21+
export const Enter: Key = {
1622
key: 'Enter',
1723
which: 13,
1824
keyCode: 13,
1925
code: 'Enter',
2026
};
2127

22-
export const Escape = {
28+
export const Escape: Key = {
2329
key: [
2430
'Escape',
2531
// IE11 Escape
@@ -30,72 +36,72 @@ export const Escape = {
3036
code: 'Esc',
3137
};
3238

33-
export const Space = {
39+
export const Space: Key = {
3440
key: ' ',
3541
which: 32,
3642
keyCode: 32,
3743
code: 'Space',
3844
};
3945

40-
export const PageUp = {
46+
export const PageUp: Key = {
4147
key: 'PageUp',
4248
which: 33,
4349
keyCode: 33,
4450
code: 'Numpad9',
4551
};
4652

47-
export const PageDown = {
53+
export const PageDown: Key = {
4854
key: 'PageDown',
4955
which: 34,
5056
keyCode: 34,
5157
code: 'Numpad3',
5258
};
5359

54-
export const End = {
60+
export const End: Key = {
5561
key: 'End',
5662
which: 35,
5763
keyCode: 35,
5864
code: 'Numpad1',
5965
};
6066

61-
export const Home = {
67+
export const Home: Key = {
6268
key: 'Home',
6369
which: 36,
6470
keyCode: 36,
6571
code: 'Numpad7',
6672
};
6773

68-
export const ArrowLeft = {
74+
export const ArrowLeft: Key = {
6975
key: 'ArrowLeft',
7076
which: 37,
7177
keyCode: 37,
7278
code: 'ArrowLeft',
7379
};
7480

75-
export const ArrowUp = {
81+
export const ArrowUp: Key = {
7682
key: 'ArrowUp',
7783
which: 38,
7884
keyCode: 38,
7985
code: 'ArrowUp',
8086
};
8187

82-
export const ArrowRight = {
88+
export const ArrowRight: Key = {
8389
key: 'ArrowRight',
8490
which: 39,
8591
keyCode: 39,
8692
code: 'ArrowRight',
8793
};
8894

89-
export const ArrowDown = {
95+
export const ArrowDown: Key = {
9096
key: 'ArrowDown',
9197
which: 40,
9298
keyCode: 40,
9399
code: 'ArrowDown',
94100
};
95101

96-
export const Delete = {
102+
export const Delete: Key = {
97103
key: 'Delete',
98-
which: 8 || 46,
99-
keyCode: 8 || 46,
104+
which: 8,
105+
keyCode: 8,
100106
code: 'ArrowDecimal',
101107
};

packages/react/src/internal/keyboard/match.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)