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
TextInput > iOS > selectTextOnFocus not working as expected #41988
Comments
Related to this issue: #30585 This is still a bug, A working temporary solution is found here: #30585 (comment) (thanks @pascallapointe) <TextInput
value={value}
onFocus={e =>
// Workaround for selectTextOnFocus={true} not working
e.currentTarget.setNativeProps({
selection: { start: 0, end: value?.toString().length },
});
}
...
/> Setting |
I'm also having this issue and it seems to work intermittently in no predictable way |
Happening for me too. |
Happening to me too. I managed to fix it by also including the |
This option works, but is not very good from a UX point of view, because the text will be highlighted every time (also when editing). My temporal suggestion it's using ref for set selection on focus. const ref = useRef(null)
const [value, setValue] = useState('');
const onFocus = () => {
input.current?.setSelection(0, value.length);
}
return <TextInput value={value} onChangeText={setValue} ref={ref} onFocus={onFocus} /> |
I'm hitting this too. I think it's a bug. Does anyone have a burning desire to dig into the source code and figure out what's going on? |
I briefly investigated this and seem to have found the cause of the problem and a potential fix. Opened a PR here. Hopefully if it isn't the right fix it at least points in the right direction for someone to pick it up (especially since it also affects Fabric!) #44307 Right now, the underlying native code selects the text like so in non-fabric: - (void)textInputDidBeginEditing
{
// Removed some other code for clarity
if (_selectTextOnFocus) {
[self.backedTextInputView selectAll:nil];
}
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
reactTag:self.reactTag
text:[self.backedTextInputView.attributedText.string copy]
key:nil
eventCount:_nativeEventCount];
} This seems logical at first glance, and it actually is highlighting the text. Notice that we briefly sometimes see the selection before losing it. RocketSim_Recording_iPhone_15_Pro_6.1_2024-04-28_16.25.38.mp4A bit more investigation reveals that the method Screen.Recording.2024-04-28.at.5.29.57.PM.movHopefully if this isn't the appropriate fix it at least gives someone the right idea of where to go looking. |
Description
I created a fresh project using the
npx react-native@latest init textinputBug
and added a simple text input.Noticed 2 weird behaviors on iOS. I tested on both iOS
16.4
and17.2
.non-focused
text input, the text will be selected on the16.4
but not on the17.2
.Please watch the video for more information and let me know if you need more info
Steps to reproduce
npx react-native@latest init textinputBug
React Native Version
0.73.1
Affected Platforms
Runtime - iOS
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://github.com/AlirezaHadjar/textinputBug
Screenshots and Videos
2023-12-19_12-05-47.mp4
The text was updated successfully, but these errors were encountered: