Skip to content

Commit cdab537

Browse files
haileyokfacebook-github-bot
authored andcommitted
fix (iOS): fix selectTextOnFocus in Fabric and non-Fabric by calling selectAll after becomeFirstResponder (facebook#44307)
Summary: Fixes facebook#41988 Hopefully even if this isn't the right way to go about solving this, it at least points in the right direction for a different fix! Currently - both on Paper and Fabric - the `selectTextOnFocus` prop does not work as expected on a single line text input. It seems that if the `UITextField` has not yet become the first responder, the text will be briefly selected but then deselected immediately afterward. This can be seen in the tester when running for either Fabric or Paper (video using Fabric) https://github.com/facebook/react-native/assets/153161762/aa9c609e-6eb8-4177-a41f-32aae53c06ac Instead, we can move the `selectAll` call to `reactFocus` in `RCTBaseTextInputView` or `focus` `RCTTextInputComponentView` - both of which first call `becomeFirstResponder` - to get the expected result. ## Changelog: [IOS] [FIXED] - fix selectTextOnFocus in Fabric and non-Fabric by calling selectAll after becomeFirstResponder Pull Request resolved: facebook#44307 Test Plan: * Test changes on RN Tester (iOS) https://pxl.cl/55kDc Reviewed By: cipolleschi Differential Revision: D56699773 Pulled By: fabriziocucci fbshipit-source-id: ed092835f3c602e2c50a4198357653a9cef942d9
1 parent db2c44a commit cdab537

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputView.mm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,6 @@ - (BOOL)textInputShouldBeginEditing
375375

376376
- (void)textInputDidBeginEditing
377377
{
378-
if (_clearTextOnFocus) {
379-
self.backedTextInputView.attributedText = [NSAttributedString new];
380-
}
381-
382-
if (_selectTextOnFocus) {
383-
[self.backedTextInputView selectAll:nil];
384-
}
385-
386378
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
387379
reactTag:self.reactTag
388380
text:[self.backedTextInputView.attributedText.string copy]
@@ -600,6 +592,14 @@ - (UIView *)reactAccessibilityElement
600592
- (void)reactFocus
601593
{
602594
[self.backedTextInputView reactFocus];
595+
596+
if (_clearTextOnFocus) {
597+
self.backedTextInputView.attributedText = [NSAttributedString new];
598+
}
599+
600+
if (_selectTextOnFocus) {
601+
[self.backedTextInputView selectAll:nil];
602+
}
603603
}
604604

605605
- (void)reactBlur

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,18 +280,6 @@ - (BOOL)textInputShouldBeginEditing
280280

281281
- (void)textInputDidBeginEditing
282282
{
283-
const auto &props = static_cast<const TextInputProps &>(*_props);
284-
285-
if (props.traits.clearTextOnFocus) {
286-
_backedTextInputView.attributedText = nil;
287-
[self textInputDidChange];
288-
}
289-
290-
if (props.traits.selectTextOnFocus) {
291-
[_backedTextInputView selectAll:nil];
292-
[self textInputDidChangeSelection];
293-
}
294-
295283
if (_eventEmitter) {
296284
static_cast<const TextInputEventEmitter &>(*_eventEmitter).onFocus([self _textInputMetrics]);
297285
}
@@ -431,6 +419,18 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
431419
- (void)focus
432420
{
433421
[_backedTextInputView becomeFirstResponder];
422+
423+
const auto &props = static_cast<const TextInputProps &>(*_props);
424+
425+
if (props.traits.clearTextOnFocus) {
426+
_backedTextInputView.attributedText = nil;
427+
[self textInputDidChange];
428+
}
429+
430+
if (props.traits.selectTextOnFocus) {
431+
[_backedTextInputView selectAll:nil];
432+
[self textInputDidChangeSelection];
433+
}
434434
}
435435

436436
- (void)blur

0 commit comments

Comments
 (0)