From c4aa411ee374f2320343b900f1f8b24a47b633c9 Mon Sep 17 00:00:00 2001 From: Nadiia D Date: Mon, 22 Feb 2021 15:34:50 -0800 Subject: [PATCH] Replace Touchable with usePressability hook Summary: Changelog: [General][Changed] textInput component changes: - use Pressability hook directly - no more cloning the component Reviewed By: yungsters, kacieb Differential Revision: D26573762 fbshipit-source-id: 17b47c8b0b9af22796d6e1528e8e3c16b5ed5d51 --- Libraries/Components/TextInput/TextInput.js | 69 +++++++++++---------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 261e11b82b59d8..62fda5a726b0bb 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -17,12 +17,13 @@ const StyleSheet = require('../../StyleSheet/StyleSheet'); const Text = require('../../Text/Text'); const TextAncestor = require('../../Text/TextAncestor'); const TextInputState = require('./TextInputState'); -const TouchableWithoutFeedback = require('../Touchable/TouchableWithoutFeedback'); const invariant = require('invariant'); const nullthrows = require('nullthrows'); const setAndForwardRef = require('../../Utilities/setAndForwardRef'); +import usePressability from '../../Pressability/usePressability'; + import type {TextStyleProp, ViewStyleProp} from '../../StyleSheet/StyleSheet'; import type {ColorValue} from '../../StyleSheet/StyleSheet'; import type {ViewProps} from '../View/ViewPropTypes'; @@ -1002,12 +1003,6 @@ function InternalTextInput(props: Props): React.Node { }, }); - const _onPress = (event: PressEvent) => { - if (props.editable || props.editable === undefined) { - nullthrows(inputRef.current).focus(); - } - }; - const _onChange = (event: ChangeEvent) => { const text = event.nativeEvent.text; props.onChange && props.onChange(event); @@ -1061,18 +1056,38 @@ function InternalTextInput(props: Props): React.Node { }; let textInput = null; - let additionalTouchableProps: {| - rejectResponderTermination?: $PropertyType< - Props, - 'rejectResponderTermination', - >, - // This is a hack to let Flow know we want an exact object - |} = {...null}; // The default value for `blurOnSubmit` is true for single-line fields and // false for multi-line fields. const blurOnSubmit = props.blurOnSubmit ?? !props.multiline; + const accessible = props.accessible !== false; + const focusable = props.focusable !== false; + + const config = React.useMemo( + () => ({ + onPress: (event: PressEvent) => { + if (props.editable !== false) { + nullthrows(inputRef.current).focus(); + } + }, + onPressIn: props.onPressIn, + onPressOut: props.onPressOut, + cancelable: + Platform.OS === 'ios' ? !props.rejectResponderTermination : null, + }), + [ + props.editable, + props.onPressIn, + props.onPressOut, + props.rejectResponderTermination, + ], + ); + + // TextInput handles onBlur and onFocus events + // so omitting onBlur and onFocus pressability handlers here. + const {onBlur, onFocus, ...eventHandlers} = usePressability(config) || {}; + if (Platform.OS === 'ios') { const RCTTextInputView = props.multiline ? RCTMultilineTextInputView @@ -1082,15 +1097,15 @@ function InternalTextInput(props: Props): React.Node { ? [styles.multilineInput, props.style] : props.style; - additionalTouchableProps.rejectResponderTermination = - props.rejectResponderTermination; - textInput = ( - - {textInput} - - + {textInput} ); }