Skip to content

Commit 09c7880

Browse files
committed
fix(switch): type key events for legacy React Native types
React Native's legacy types, still the default before 0.87 and checked in CI, export neither KeyDownEvent nor KeyUpEvent. Type the Space handlers with a NativeSyntheticEvent of the fields they read, which both type sets accept.
1 parent 830711b commit 09c7880

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎src/components/Switch/Switch.tsx‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react';
22
import {
3-
type KeyDownEvent,
4-
type KeyUpEvent,
3+
type NativeSyntheticEvent,
54
Platform,
65
Pressable,
76
StyleSheet,
@@ -34,6 +33,10 @@ import type { StateOpacityKey, ThemeProp } from '../../theme/types';
3433
import { isKeyboardFocusEvent } from '../../utils/isKeyboardFocusEvent';
3534
import Icon, { type IconSource } from '../Icon';
3635

36+
// React Native 0.87+ exports `KeyDownEvent` and `KeyUpEvent`, but its legacy
37+
// types, the default before 0.87, have neither, so type only what is read.
38+
type KeyEvent = NativeSyntheticEvent<{ key: string; repeat?: boolean }>;
39+
3740
export type Props = {
3841
/**
3942
* Whether the switch is on.
@@ -417,7 +420,7 @@ const Switch = ({
417420
// React Native Web only handles Space for button roles.
418421
...(Platform.OS === 'web'
419422
? {
420-
onKeyDown: (event: KeyDownEvent) => {
423+
onKeyDown: (event: KeyEvent) => {
421424
const { key, repeat } = event.nativeEvent;
422425
if (key !== ' ' && key !== 'Spacebar') return;
423426
event.preventDefault();
@@ -426,7 +429,7 @@ const Switch = ({
426429
pressedSV.value = 1;
427430
}
428431
},
429-
onKeyUp: (event: KeyUpEvent) => {
432+
onKeyUp: (event: KeyEvent) => {
430433
const { key } = event.nativeEvent;
431434
if (key !== ' ' && key !== 'Spacebar') return;
432435
event.preventDefault();

0 commit comments

Comments
 (0)