Skip to content

fix(TextInput): prevent keyboard from opening when scrolling long text (#4666) #4698

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 35 additions & 2 deletions src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export type Props = React.ComponentPropsWithRef<typeof NativeTextInput> & {
* Example: `borderRadius`, `borderColor`
*/
underlineStyle?: StyleProp<ViewStyle>;
/**
* Pass true to disable input focus on scroll.
* See: https://github.com/callstack/react-native-paper/issues/4666
*/
disableFocusOnScroll?: boolean;
};

interface CompoundedComponent
Expand Down Expand Up @@ -223,7 +228,10 @@ const TextInput = forwardRef<TextInputHandles, Props>(
error: errorProp = false,
multiline = false,
editable = true,
disableFocusOnScroll = false,
contentStyle,
onScroll,
onTouchEnd,
render = DefaultRenderer,
theme: themeOverrides,
...rest
Expand Down Expand Up @@ -280,6 +288,7 @@ const TextInput = forwardRef<TextInputHandles, Props>(
width: null,
height: null,
});
const [isScrolling, setScrolling] = React.useState(false);

const timer = React.useRef<NodeJS.Timeout | undefined>();

Expand Down Expand Up @@ -403,6 +412,22 @@ const TextInput = forwardRef<TextInputHandles, Props>(
[rightLayout.height, rightLayout.width]
);

const handleTouchEnd = (args: any) => {
if (disableFocusOnScroll) {
setScrolling(false);
}
// also handle component specific event
onTouchEnd?.(args);
};

const handleScroll = (args: any) => {
if (disableFocusOnScroll) {
setScrolling(true);
}
// also handle component specific event
onScroll?.(args);
};

const handleFocus = (args: any) => {
if (disabled || !editable) {
return;
Expand Down Expand Up @@ -484,7 +509,12 @@ const TextInput = forwardRef<TextInputHandles, Props>(
disabled={disabled}
error={errorProp}
multiline={multiline}
editable={editable}
// if disabledFocusOnScroll is enabled
editable={
(disableFocusOnScroll ? !isScrolling : undefined) || editable
}
onTouchEnd={handleTouchEnd}
onScroll={handleScroll}
render={render}
{...rest}
theme={theme}
Expand Down Expand Up @@ -526,8 +556,11 @@ const TextInput = forwardRef<TextInputHandles, Props>(
disabled={disabled}
error={errorProp}
multiline={multiline}
editable={editable}
render={render}
// if disabledFocusOnScroll is enabled
editable={(disableFocusOnScroll ? !isScrolling : undefined) || editable}
onTouchEnd={handleTouchEnd}
onScroll={handleScroll}
{...rest}
theme={theme}
value={value}
Expand Down
18 changes: 18 additions & 0 deletions src/components/__tests__/__snapshots__/TextInput.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ exports[`call onPress when affix adornment pressed 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Enter your phone number"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -498,6 +500,8 @@ exports[`correctly applies a component as the text label 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -724,6 +728,8 @@ exports[`correctly applies cursorColor prop 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -950,6 +956,8 @@ exports[`correctly applies default textAlign based on default RTL 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -1208,6 +1216,8 @@ exports[`correctly applies height to multiline Outline TextInput 1`] = `
onChangeText={[Function]}
onFocus={[Function]}
onLayout={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type Something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -1435,6 +1445,8 @@ exports[`correctly applies paddingLeft from contentStyleProp 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -1663,6 +1675,8 @@ exports[`correctly applies textAlign center 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -1889,6 +1903,8 @@ exports[`correctly renders left-side affix adornment, and right-side icon adornm
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down Expand Up @@ -2315,6 +2331,8 @@ exports[`correctly renders left-side icon adornment, and right-side affix adornm
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
onScroll={[Function]}
onTouchEnd={[Function]}
placeholder="Type something"
placeholderTextColor="transparent"
selectionColor="rgba(103, 80, 164, 1)"
Expand Down