Skip to content

Commit

Permalink
RN: Improve usePressability Return Type
Browse files Browse the repository at this point in the history
Summary:
Improves the Flow type of `usePressability` so that if the `config` argument is non-nullable, the return value is non-nullable. This helps reduce unnecessary null checks.

Changelog:
[Internal]

Differential Revision: D65908791
  • Loading branch information
yungsters authored and facebook-github-bot committed Nov 13, 2024
1 parent 177bf4d commit be2dc52
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ function InternalTextInput(props: Props): React.Node {

// TextInput handles onBlur and onFocus events
// so omitting onBlur and onFocus pressability handlers here.
const {onBlur, onFocus, ...eventHandlers} = usePressability(config) || {};
const {onBlur, onFocus, ...eventHandlers} = usePressability(config);

let _accessibilityState;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ module.exports = function TouchableWithoutFeedback(props: Props): React.Node {

// BACKWARD-COMPATIBILITY: Focus and blur events were never supported before
// adopting `Pressability`, so preserve that behavior.
const {onBlur, onFocus, ...eventHandlersWithoutBlurAndFocus} =
eventHandlers || {};
const {onBlur, onFocus, ...eventHandlersWithoutBlurAndFocus} = eventHandlers;

const elementProps: {[string]: mixed, ...} = {
...eventHandlersWithoutBlurAndFocus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import Pressability, {
} from './Pressability';
import {useEffect, useRef} from 'react';

declare function usePressability(config: PressabilityConfig): EventHandlers;
declare function usePressability(config: null | void): null | EventHandlers;

/**
* Creates a persistent instance of `Pressability` that automatically configures
* itself and resets. Accepts null `config` to support lazy initialization. Once
Expand All @@ -28,7 +31,7 @@ import {useEffect, useRef} from 'react';
*/
export default function usePressability(
config: ?PressabilityConfig,
): ?EventHandlers {
): null | EventHandlers {
const pressabilityRef = useRef<?Pressability>(null);
if (config != null && pressabilityRef.current == null) {
pressabilityRef.current = new Pressability(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7233,7 +7233,7 @@ exports[`public API should not change unintentionally Libraries/Pressability/Pre
exports[`public API should not change unintentionally Libraries/Pressability/usePressability.js 1`] = `
"declare export default function usePressability(
config: ?PressabilityConfig
): ?EventHandlers;
): null | EventHandlers;
"
`;

Expand Down

0 comments on commit be2dc52

Please sign in to comment.