Skip to content

[0.64] Add onFocus/onBlur/onKeyDown/onKeyUp to Pressable #968

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

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions Libraries/Components/Pressable/Pressable.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import type {
LayoutEvent,
MouseEvent,
PressEvent,
// [TODO(macOS GH#774)
FocusEvent,
BlurEvent,
KeyEvent,
// ]TODO(macOS GH#774)
} from '../../Types/CoreEventTypes';
import type {DraggedTypesType} from '../View/DraggedType'; // TODO(macOS GH#774)
import View from '../View/View';
Expand Down Expand Up @@ -130,6 +135,40 @@ type Props = $ReadOnly<{|
*/
onPressOut?: ?(event: PressEvent) => void,

// [TODO(macOS GH#774)
/**
* Called after the element is focused.
*/
onFocus?: ?(event: FocusEvent) => mixed,

/**
* Called after the element loses focus.
*/
onBlur?: ?(event: BlurEvent) => mixed,

/**
* Called after a key down event is detected.
*/
onKeyDown?: ?(event: KeyEvent) => mixed,

/**
* Called after a key up event is detected.
*/
onKeyUp?: ?(event: KeyEvent) => mixed,

/**
* Array of keys to receive key down events for
* For arrow keys, add "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown",
*/
validKeysDown?: ?Array<string>,

/**
* Array of keys to receive key up events for
* For arrow keys, add "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown",
*/
validKeysUp?: ?Array<string>,
// ]TODO(macOS GH#774)

/**
* Either view styles or a function that receives a boolean reflecting whether
* the component is currently pressed and returns view styles.
Expand Down Expand Up @@ -197,6 +236,12 @@ function Pressable(props: Props, forwardedRef): React.Node {
onPress,
onPressIn,
onPressOut,
// [TODO(macOS GH#774)
onFocus,
onBlur,
onKeyDown,
onKeyUp,
// ]TODO(macOS GH#774)
pressRetentionOffset,
style,
testOnly_pressed,
Expand Down Expand Up @@ -256,6 +301,12 @@ function Pressable(props: Props, forwardedRef): React.Node {
onPressOut(event);
}
},
// [TODO(macOS GH#774)
onFocus,
onBlur,
onKeyDown,
onKeyUp,
// ]TODO(macOS GH#774)
}),
[
android_disableSound,
Expand All @@ -271,6 +322,12 @@ function Pressable(props: Props, forwardedRef): React.Node {
onPress,
onPressIn,
onPressOut,
// [TODO(macOS GH#774)
onFocus,
onBlur,
onKeyDown,
onKeyUp,
// ]TODO(macOS GH#774)
pressRetentionOffset,
setPressed,
unstable_pressDelay,
Expand Down
34 changes: 18 additions & 16 deletions Libraries/Pressability/Pressability.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,21 +475,6 @@ export default class Pressability {
},
};

const keyEventHandlers = {
onKeyDown: (event: KeyEvent): void => {
const {onKeyDown} = this._config;
if (onKeyDown != null) {
onKeyDown(event);
}
},
onKeyUp: (event: KeyEvent): void => {
const {onKeyUp} = this._config;
if (onKeyUp != null) {
onKeyUp(event);
}
},
};

const responderEventHandlers = {
onStartShouldSetResponder: (): boolean => {
const {disabled} = this._config;
Expand Down Expand Up @@ -644,11 +629,28 @@ export default class Pressability {
},
};

// [TODO(macOS GH#774)
const keyboardEventHandlers = {
onKeyDown: (event: KeyEvent): void => {
const {onKeyDown} = this._config;
if (onKeyDown != null) {
onKeyDown(event);
}
},
onKeyUp: (event: KeyEvent): void => {
const {onKeyUp} = this._config;
if (onKeyUp != null) {
onKeyUp(event);
}
},
};
// ]TODO(macOS GH#774)

return {
...focusEventHandlers,
...responderEventHandlers,
...mouseEventHandlers,
...keyEventHandlers,
...keyboardEventHandlers, // [TODO(macOS GH#774)]
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ function PressableFeedbackEvents() {
testID="pressable_feedback_events_button"
accessibilityLabel="pressable feedback events"
accessibilityRole="button"
onHoverIn={() => appendEvent('hoverIn')} // [TODO(macOS GH#774)
onHoverOut={() => appendEvent('hoverOut')} // ]TODO(macOS GH#774)
// [TODO(macOS GH#774)
onHoverIn={() => appendEvent('hoverIn')}
onHoverOut={() => appendEvent('hoverOut')}
onFocus={() => appendEvent('focus')}
onBlur={() => appendEvent('blur')}
// ]TODO(macOS GH#774)
onPress={() => appendEvent('press')}
onPressIn={() => appendEvent('pressIn')}
onPressOut={() => appendEvent('pressOut')}
Expand Down