-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[Android] TextInput doesn't scroll properly when restricting the height on an expanding TextInput #12799
Comments
I have the same issue in RN 0.46.4 |
@shergin (in response to 😕 ) Try out this snack on an android device: https://snack.expo.io/BkhOoGtLZ. I used a Galaxy S6. |
Confirmed on RN 0.46.4, with:
|
confirmed ios doesn't have this issue only android |
any progress on this issue? |
@guysegal use workaround as posted |
Yes, I am still working on this. |
@Stoffern which workaround are you referring to? I didn't find a workaround for the case where max height is defined |
This bug wasn't happening when we were using RN 0.38.0, should be some changes afterwards |
I have the same issue in RN 0.48.3 |
By looking at the code in react-native-autogrow-input, I was able to create a Component that solves this problem for me (I've only tested it on Android). This is a messy hack because I use two TextInput. The user sees only one of them. The hidden TextInput handles onContentSizeChange() while the visible one receives user input, grows & scrolls appropriately. The two TextInput communicate with each other via the component state. The component requires two props:
|
I only get the textbox to grow on an How can we get autogrowing to work with wrapping? I'm using the exact example from @jonasb at the top of this thread. |
This issue also exists with RN 0.49.0 with 'autoGrow' option and 'maxHeight' property set to imitate the same behavior as with using onContentSizeChange which shouldn't surprise as internally auto-growing feature uses onContentSizeChange. |
Same issue here. I was able to work around this by setting Hoping for a fix sometime soon, my TextInput component is looking pretty nasty with all of these if/else workarounds for Android lately. |
Hey there, However, I still don't know how to fix this issue. 😢 |
My workaround no longer works in RN 0.49.1. 😢 |
having the same issue for RN 0.49.1 :S |
RN 0.49.1 - Android Now automatic scrolling for |
I remember a time (around this commit) where TextInputs on Android were working as expected: auto-expand, cursor tracking, the whole lot. Now it seems multiline inputs where the amount of text is expected to exceed the height are pretty much useless. What happened?! 😭 |
@shergin I have taken a look at your changes in c550f27 on the RNTester app. Is there a way to get the screen to scroll to the line where the cursor is located (rather than having the user manually scroll)? This was done automatically in an earlier version of react-native, ~0.46 if I remember correctly. Also, what is the problem you are facing exactly when trying to get auto-scrolling working again? |
@Palisand Well, I just tried to fix/enable it, but I failed. So, yes, the current state is: layout is working, (auto)scrolling is not (and I don't know how to fix that, yet). |
I was specifically referring to the scrolling of a parent view of a growable TextInput rather than the input itself. It's good to hear that you think the latter should be easily fixable as this is pretty critical functionality. I personally don't even know where to begin making changes to get this working ( |
@shergin @Palisand I've tracked the issue basing on RN 0.49-stable branch and this is a guilty method: https://github.com/facebook/react-native/blob/0.49-stable/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java#L121 I've created my own implementation of ReactEditText which looks like this: public class CustomTextInput extends ReactEditText {
public CustomTextInput(Context context) {
super(context);
}
@Override
public boolean isLayoutRequested() {
return false;
}
} Then I've overrided ReactTextInputManager like this: public class CustomTextInputManager extends ReactTextInputManager {
@Override
public String getName() {
return "CustomTextInput";
}
@Override
public CustomTextInput createViewInstance(ThemedReactContext context) {
CustomTextInput editText = new CustomTextInput(context);
int inputType = editText.getInputType();
editText.setInputType(inputType & (~InputType.TYPE_TEXT_FLAG_MULTI_LINE));
editText.setReturnKeyType("done");
editText.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
(int) Math.ceil(PixelUtil.toPixelFromSP(ViewDefaults.FONT_SIZE_SP)));
return editText;
}
} and then I've copy-pasted the TextInput class and put my CustomTextInput here https://github.com/facebook/react-native/blob/0.49-stable/Libraries/Components/TextInput/TextInput.js#L42 And the issue is gone! UPDATE the above method has some drawbacks, please continue reading if you need a better workaround (you may don't need it) This is surely a pretty short workaround but it may require proper implementation and testing on master RN branch. I'm not sure why someone would not want the automatic scroll on the TextInput so I don't know what is the purpose of the original implementation and which use cases it handles. I thought that maybe setting initial very long text on TextInput with restricted height would cause the TextInput to be scrolled to the bottom but public class CustomTextInputManager extends ReactTextInputManager {
@Override
public String getName() {
return "CustomTextInput";
}
@Override
public CustomTextInput createViewInstance(ThemedReactContext context) {
CustomTextInput editText = new CustomTextInput(context);
int inputType = editText.getInputType();
editText.setInputType(inputType & (~InputType.TYPE_TEXT_FLAG_MULTI_LINE));
editText.setReturnKeyType("done");
editText.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
(int) Math.ceil(PixelUtil.toPixelFromSP(ViewDefaults.FONT_SIZE_SP)));
return editText;
}
@ReactProp(name = "autoScroll", defaultBoolean = false)
public void setAutoScroll(CustomTextInput view, boolean autoScroll) {
view.setAutoScroll(autoScroll);
}
} public class CustomTextInput extends ReactEditText {
private boolean autoScroll = false;
public CustomTextInput(Context context) {
super(context);
}
private boolean isMultiline() {
return (getInputType() & InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0;
}
@Override
public boolean isLayoutRequested() {
if (isMultiline() && !autoScroll) {
return true;
}
return false;
}
public void setAutoScroll(boolean autoScroll) {
this.autoScroll = autoScroll;
}
} render() {
return (
<TextInputComponent
multiline
onFocus={() => {
if (this.inputField && Platform.OS === 'android') {
this.inputField.setNativeProps({
autoScroll: true,
});
}
}}
onBlur={() => {
if (this.inputField && Platform.OS === 'android') {
this.inputField.setNativeProps({
autoScroll: false,
});
}
}}
ref={(ref) => {
this.inputField = ref;
}}
/>
)
} |
@konradkierus @shergin Just tested this by changing If anyone is interested in how to extract the cursor's position so that you can |
Still there for me as well on 0.50.3 |
I found a solution that works for my purposes so figured I'd share This code works for React Native >=50.0:
This code works for React Native <50.0:
There are some nested ScrollViews since KeyboardAvoidingView is a ScrollView but it works pretty well as a workaround without having to do native code. The key is using the ScrollView's scrollToEnd anytime the content size changes. I only use KeyboardAvoidingView because I'm building a chat app and the message box is at the bottom of the screen. |
@esdrasetrenne hi! |
Hi @dead23angel I dont know about that. It could require fixing the actual library code? Somebody pointed out a flaw in my code though so here is an updated version that works in case a user scrolls up and is editing part of the text they wrote: https://snack.expo.io/rJHCrEYef
This is obviously pretty messy and I actually like the simplicity of @konradkierus version, but since I am using Expo for react native I do not have the luxury of using native code. It needs to be 100% javascript. The key here is updating the component's state using the ScrollView's After that, anytime the Since this is only an Android problem, I will probably only use this component for my Android rendering and let my iOS rendering stay the same. I am kind of concerned about what performance issues if any could come from calling setState too much An upstream fix would be super ideal 👍 |
Summary: This feature was disabled for multiline textinputs in D3528202 ... seems without a good reason. The broken autoscroll-to-cursor feature is terribly harmful and counter-intuitive in ALL cases. I also add a contentSize tracking in the example app to make sure that it is unaffected by this change. https://pxl.cl/9RHP #12799 #15778 Special thanks to konradkierus! Reviewed By: sahrens Differential Revision: D6405985 fbshipit-source-id: 337a390a9db7b3528200ef66c4a079b87608294e
Finally, 0bef872 should fix this. |
@shergin thank you, how can i implemented it in RN 48.2? |
Summary: This feature was disabled for multiline textinputs in D3528202 ... seems without a good reason. The broken autoscroll-to-cursor feature is terribly harmful and counter-intuitive in ALL cases. I also add a contentSize tracking in the example app to make sure that it is unaffected by this change. https://pxl.cl/9RHP #12799 #15778 Special thanks to konradkierus! Reviewed By: sahrens Differential Revision: D6405985 fbshipit-source-id: 337a390a9db7b3528200ef66c4a079b87608294e
@shergin, I've confirmed that this fixed the issue (by cherry-picking patch atop v0.50.3). Thanks! |
Summary: This feature was disabled for multiline textinputs in D3528202 ... seems without a good reason. The broken autoscroll-to-cursor feature is terribly harmful and counter-intuitive in ALL cases. I also add a contentSize tracking in the example app to make sure that it is unaffected by this change. https://pxl.cl/9RHP facebook#12799 facebook#15778 Special thanks to konradkierus! Reviewed By: sahrens Differential Revision: D6405985 fbshipit-source-id: 337a390a9db7b3528200ef66c4a079b87608294e
FYI - we've cherry-picked this into 0.51-stable (RC with this patch still not released but it will go out with the final 0.51.0 release assuming it's not reverted). |
Summary: This feature was disabled for multiline textinputs in D3528202 ... seems without a good reason. The broken autoscroll-to-cursor feature is terribly harmful and counter-intuitive in ALL cases. I also add a contentSize tracking in the example app to make sure that it is unaffected by this change. https://pxl.cl/9RHP facebook/react-native#12799 facebook/react-native#15778 Special thanks to konradkierus! Reviewed By: sahrens Differential Revision: D6405985 fbshipit-source-id: 337a390a9db7b3528200ef66c4a079b87608294e
seriously @shergin thank you so so much. This is such a save! |
@ide Any idea when the 0.51 will be released? Thanks! |
Hey @mannol, cherry-picking Android fixes is a fairly involved process because you first need to setup your project to build Android from source. It's been a long time since I set up the Android build in my project, but all I remember is that it took a while to get working. If you are building Android from source, the fastest way to 'cherry-pick' is to actually just copy/paste the changes to |
0.51 is planned to be released sometime today (though unexpected things can happen, so give it a few extra days of buffer room). |
❤️ |
Summary: This feature was disabled for multiline textinputs in D3528202 ... seems without a good reason. The broken autoscroll-to-cursor feature is terribly harmful and counter-intuitive in ALL cases. I also add a contentSize tracking in the example app to make sure that it is unaffected by this change. https://pxl.cl/9RHP facebook#12799 facebook#15778 Special thanks to konradkierus! Reviewed By: sahrens Differential Revision: D6405985 fbshipit-source-id: 337a390a9db7b3528200ef66c4a079b87608294e
Summary: This feature was disabled for multiline textinputs in D3528202 ... seems without a good reason. The broken autoscroll-to-cursor feature is terribly harmful and counter-intuitive in ALL cases. I also add a contentSize tracking in the example app to make sure that it is unaffected by this change. https://pxl.cl/9RHP facebook#12799 facebook#15778 Special thanks to konradkierus! Reviewed By: sahrens Differential Revision: D6405985 fbshipit-source-id: 337a390a9db7b3528200ef66c4a079b87608294e
I see it's fixed in 0.52 instead: 0bef872 |
Add Comment - Add new AddOrEditCommentScreen - Add redux and api support - Add navigation support - Add storybook story Other - For ejected app, update to RN 0.52 to fix issue with auto-scrolling TextInput facebook/react-native#12799. We'll update the expo app once 0.52 is supported for expo / crna - Fix issue with debug signing config for Android. Still need to follow up on this. For now, use same signing config as release, to avoid install issues for debug build. - Minor polish - Minor refactor - Update some comments
Hi, |
i had this problem
|
Still there for me as well on 0.50.4 |
Description
I'm creating a InputText where I want to restrict the height to a maximum. When I do it the TextInput doesn't automatically scroll when I write enough to add another line of text. That means I have to manually scroll down to see what I'm typing.
style={{height: 200}}
)style={{ height: Math.max(35, this.state.height) }}
)Reproduction
Solution
?
Additional Information
The text was updated successfully, but these errors were encountered: