Skip to content

Commit f67cfce

Browse files
authored
Add onFocus/onBlur/onKeyDown/onKeyUp to Pressable (facebook#962) (facebook#968)
* Add onFocus/onBlur/onKeyDown/onKeyUp to Pressable * yarn lint --fix * add validKeysUp/Down
1 parent be83f3b commit f67cfce

File tree

3 files changed

+81
-18
lines changed

3 files changed

+81
-18
lines changed

Libraries/Components/Pressable/Pressable.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import type {
2929
LayoutEvent,
3030
MouseEvent,
3131
PressEvent,
32+
// [TODO(macOS GH#774)
33+
FocusEvent,
34+
BlurEvent,
35+
KeyEvent,
36+
// ]TODO(macOS GH#774)
3237
} from '../../Types/CoreEventTypes';
3338
import type {DraggedTypesType} from '../View/DraggedType'; // TODO(macOS GH#774)
3439
import View from '../View/View';
@@ -130,6 +135,40 @@ type Props = $ReadOnly<{|
130135
*/
131136
onPressOut?: ?(event: PressEvent) => void,
132137

138+
// [TODO(macOS GH#774)
139+
/**
140+
* Called after the element is focused.
141+
*/
142+
onFocus?: ?(event: FocusEvent) => mixed,
143+
144+
/**
145+
* Called after the element loses focus.
146+
*/
147+
onBlur?: ?(event: BlurEvent) => mixed,
148+
149+
/**
150+
* Called after a key down event is detected.
151+
*/
152+
onKeyDown?: ?(event: KeyEvent) => mixed,
153+
154+
/**
155+
* Called after a key up event is detected.
156+
*/
157+
onKeyUp?: ?(event: KeyEvent) => mixed,
158+
159+
/**
160+
* Array of keys to receive key down events for
161+
* For arrow keys, add "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown",
162+
*/
163+
validKeysDown?: ?Array<string>,
164+
165+
/**
166+
* Array of keys to receive key up events for
167+
* For arrow keys, add "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown",
168+
*/
169+
validKeysUp?: ?Array<string>,
170+
// ]TODO(macOS GH#774)
171+
133172
/**
134173
* Either view styles or a function that receives a boolean reflecting whether
135174
* the component is currently pressed and returns view styles.
@@ -197,6 +236,12 @@ function Pressable(props: Props, forwardedRef): React.Node {
197236
onPress,
198237
onPressIn,
199238
onPressOut,
239+
// [TODO(macOS GH#774)
240+
onFocus,
241+
onBlur,
242+
onKeyDown,
243+
onKeyUp,
244+
// ]TODO(macOS GH#774)
200245
pressRetentionOffset,
201246
style,
202247
testOnly_pressed,
@@ -256,6 +301,12 @@ function Pressable(props: Props, forwardedRef): React.Node {
256301
onPressOut(event);
257302
}
258303
},
304+
// [TODO(macOS GH#774)
305+
onFocus,
306+
onBlur,
307+
onKeyDown,
308+
onKeyUp,
309+
// ]TODO(macOS GH#774)
259310
}),
260311
[
261312
android_disableSound,
@@ -271,6 +322,12 @@ function Pressable(props: Props, forwardedRef): React.Node {
271322
onPress,
272323
onPressIn,
273324
onPressOut,
325+
// [TODO(macOS GH#774)
326+
onFocus,
327+
onBlur,
328+
onKeyDown,
329+
onKeyUp,
330+
// ]TODO(macOS GH#774)
274331
pressRetentionOffset,
275332
setPressed,
276333
unstable_pressDelay,

Libraries/Pressability/Pressability.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,21 +475,6 @@ export default class Pressability {
475475
},
476476
};
477477

478-
const keyEventHandlers = {
479-
onKeyDown: (event: KeyEvent): void => {
480-
const {onKeyDown} = this._config;
481-
if (onKeyDown != null) {
482-
onKeyDown(event);
483-
}
484-
},
485-
onKeyUp: (event: KeyEvent): void => {
486-
const {onKeyUp} = this._config;
487-
if (onKeyUp != null) {
488-
onKeyUp(event);
489-
}
490-
},
491-
};
492-
493478
const responderEventHandlers = {
494479
onStartShouldSetResponder: (): boolean => {
495480
const {disabled} = this._config;
@@ -644,11 +629,28 @@ export default class Pressability {
644629
},
645630
};
646631

632+
// [TODO(macOS GH#774)
633+
const keyboardEventHandlers = {
634+
onKeyDown: (event: KeyEvent): void => {
635+
const {onKeyDown} = this._config;
636+
if (onKeyDown != null) {
637+
onKeyDown(event);
638+
}
639+
},
640+
onKeyUp: (event: KeyEvent): void => {
641+
const {onKeyUp} = this._config;
642+
if (onKeyUp != null) {
643+
onKeyUp(event);
644+
}
645+
},
646+
};
647+
// ]TODO(macOS GH#774)
648+
647649
return {
648650
...focusEventHandlers,
649651
...responderEventHandlers,
650652
...mouseEventHandlers,
651-
...keyEventHandlers,
653+
...keyboardEventHandlers, // [TODO(macOS GH#774)]
652654
};
653655
}
654656

packages/rn-tester/js/examples/Pressable/PressableExample.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ function PressableFeedbackEvents() {
9999
testID="pressable_feedback_events_button"
100100
accessibilityLabel="pressable feedback events"
101101
accessibilityRole="button"
102-
onHoverIn={() => appendEvent('hoverIn')} // [TODO(macOS GH#774)
103-
onHoverOut={() => appendEvent('hoverOut')} // ]TODO(macOS GH#774)
102+
// [TODO(macOS GH#774)
103+
onHoverIn={() => appendEvent('hoverIn')}
104+
onHoverOut={() => appendEvent('hoverOut')}
105+
onFocus={() => appendEvent('focus')}
106+
onBlur={() => appendEvent('blur')}
107+
// ]TODO(macOS GH#774)
104108
onPress={() => appendEvent('press')}
105109
onPressIn={() => appendEvent('pressIn')}
106110
onPressOut={() => appendEvent('pressOut')}

0 commit comments

Comments
 (0)