Skip to content

Commit c4aa411

Browse files
Nadiia Dfacebook-github-bot
Nadiia D
authored andcommitted
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
1 parent 099e7aa commit c4aa411

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

Libraries/Components/TextInput/TextInput.js

+36-33
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ const StyleSheet = require('../../StyleSheet/StyleSheet');
1717
const Text = require('../../Text/Text');
1818
const TextAncestor = require('../../Text/TextAncestor');
1919
const TextInputState = require('./TextInputState');
20-
const TouchableWithoutFeedback = require('../Touchable/TouchableWithoutFeedback');
2120

2221
const invariant = require('invariant');
2322
const nullthrows = require('nullthrows');
2423
const setAndForwardRef = require('../../Utilities/setAndForwardRef');
2524

25+
import usePressability from '../../Pressability/usePressability';
26+
2627
import type {TextStyleProp, ViewStyleProp} from '../../StyleSheet/StyleSheet';
2728
import type {ColorValue} from '../../StyleSheet/StyleSheet';
2829
import type {ViewProps} from '../View/ViewPropTypes';
@@ -1002,12 +1003,6 @@ function InternalTextInput(props: Props): React.Node {
10021003
},
10031004
});
10041005

1005-
const _onPress = (event: PressEvent) => {
1006-
if (props.editable || props.editable === undefined) {
1007-
nullthrows(inputRef.current).focus();
1008-
}
1009-
};
1010-
10111006
const _onChange = (event: ChangeEvent) => {
10121007
const text = event.nativeEvent.text;
10131008
props.onChange && props.onChange(event);
@@ -1061,18 +1056,38 @@ function InternalTextInput(props: Props): React.Node {
10611056
};
10621057

10631058
let textInput = null;
1064-
let additionalTouchableProps: {|
1065-
rejectResponderTermination?: $PropertyType<
1066-
Props,
1067-
'rejectResponderTermination',
1068-
>,
1069-
// This is a hack to let Flow know we want an exact object
1070-
|} = {...null};
10711059

10721060
// The default value for `blurOnSubmit` is true for single-line fields and
10731061
// false for multi-line fields.
10741062
const blurOnSubmit = props.blurOnSubmit ?? !props.multiline;
10751063

1064+
const accessible = props.accessible !== false;
1065+
const focusable = props.focusable !== false;
1066+
1067+
const config = React.useMemo(
1068+
() => ({
1069+
onPress: (event: PressEvent) => {
1070+
if (props.editable !== false) {
1071+
nullthrows(inputRef.current).focus();
1072+
}
1073+
},
1074+
onPressIn: props.onPressIn,
1075+
onPressOut: props.onPressOut,
1076+
cancelable:
1077+
Platform.OS === 'ios' ? !props.rejectResponderTermination : null,
1078+
}),
1079+
[
1080+
props.editable,
1081+
props.onPressIn,
1082+
props.onPressOut,
1083+
props.rejectResponderTermination,
1084+
],
1085+
);
1086+
1087+
// TextInput handles onBlur and onFocus events
1088+
// so omitting onBlur and onFocus pressability handlers here.
1089+
const {onBlur, onFocus, ...eventHandlers} = usePressability(config) || {};
1090+
10761091
if (Platform.OS === 'ios') {
10771092
const RCTTextInputView = props.multiline
10781093
? RCTMultilineTextInputView
@@ -1082,15 +1097,15 @@ function InternalTextInput(props: Props): React.Node {
10821097
? [styles.multilineInput, props.style]
10831098
: props.style;
10841099

1085-
additionalTouchableProps.rejectResponderTermination =
1086-
props.rejectResponderTermination;
1087-
10881100
textInput = (
10891101
<RCTTextInputView
10901102
ref={_setNativeRef}
10911103
{...props}
1104+
{...eventHandlers}
1105+
accessible={accessible}
10921106
blurOnSubmit={blurOnSubmit}
10931107
dataDetectorTypes={props.dataDetectorTypes}
1108+
focusable={focusable}
10941109
mostRecentEventCount={mostRecentEventCount}
10951110
onBlur={_onBlur}
10961111
onChange={_onChange}
@@ -1123,10 +1138,13 @@ function InternalTextInput(props: Props): React.Node {
11231138
<AndroidTextInput
11241139
ref={_setNativeRef}
11251140
{...props}
1141+
{...eventHandlers}
1142+
accessible={accessible}
11261143
autoCapitalize={autoCapitalize}
11271144
blurOnSubmit={blurOnSubmit}
11281145
children={children}
11291146
disableFullscreenUI={props.disableFullscreenUI}
1147+
focusable={focusable}
11301148
mostRecentEventCount={mostRecentEventCount}
11311149
onBlur={_onBlur}
11321150
onChange={_onChange}
@@ -1143,22 +1161,7 @@ function InternalTextInput(props: Props): React.Node {
11431161
);
11441162
}
11451163
return (
1146-
<TextAncestor.Provider value={true}>
1147-
<TouchableWithoutFeedback
1148-
onLayout={props.onLayout}
1149-
onPress={_onPress}
1150-
onPressIn={props.onPressIn}
1151-
onPressOut={props.onPressOut}
1152-
accessible={props.accessible}
1153-
accessibilityLabel={props.accessibilityLabel}
1154-
accessibilityRole={props.accessibilityRole}
1155-
accessibilityState={props.accessibilityState}
1156-
nativeID={props.nativeID}
1157-
testID={props.testID}
1158-
{...additionalTouchableProps}>
1159-
{textInput}
1160-
</TouchableWithoutFeedback>
1161-
</TextAncestor.Provider>
1164+
<TextAncestor.Provider value={true}>{textInput}</TextAncestor.Provider>
11621165
);
11631166
}
11641167

0 commit comments

Comments
 (0)