Skip to content

Commit c85581d

Browse files
committed
WIP: view controller switch to viewDidAppear
1 parent 4099a15 commit c85581d

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#import <React/RCTUITextField.h>
1818
#import <React/RCTUITextView.h>
1919
#import <React/RCTUtils.h>
20+
#import <React/UIView+React.h>
21+
#import <React/UIViewController+React.h>
2022

2123
#import "RCTConversions.h"
2224
#import "RCTTextInputNativeCommands.h"
@@ -32,6 +34,7 @@
3234

3335
@interface RCTTextInputComponentView () <
3436
RCTBackedTextInputDelegate,
37+
RCTViewControllerAppearanceListener,
3538
RCTTextInputViewProtocol
3639
#if !TARGET_OS_TV
3740
,
@@ -70,6 +73,8 @@ @implementation RCTTextInputComponentView {
7073
*/
7174
BOOL _comingFromJS;
7275
BOOL _didMoveToWindow;
76+
BOOL _didAutoFocus;
77+
__weak UIViewController *_viewController;
7378

7479
/*
7580
* Newly initialized default typing attributes contain a no-op NSParagraphStyle and NSShadow. These cause inequality
@@ -95,6 +100,7 @@ - (instancetype)initWithFrame:(CGRect)frame
95100
_ignoreNextTextInputCall = NO;
96101
_comingFromJS = NO;
97102
_didMoveToWindow = NO;
103+
_didAutoFocus = NO;
98104
_originalTypingAttributes = [_backedTextInputView.typingAttributes copy];
99105
_previousContentSize = CGSizeZero;
100106

@@ -121,12 +127,11 @@ - (void)didMoveToWindow
121127
{
122128
[super didMoveToWindow];
123129

130+
[_viewController reactRemoveViewControllerAppearanceListener:self];
131+
_viewController = self.window ? [self reactViewController] : nil;
132+
[_viewController reactAddViewControllerAppearanceListener:self];
133+
124134
if (self.window && !_didMoveToWindow) {
125-
const auto &props = static_cast<const TextInputProps &>(*_props);
126-
if (props.autoFocus) {
127-
[_backedTextInputView becomeFirstResponder];
128-
[self scrollCursorIntoView];
129-
}
130135
_didMoveToWindow = YES;
131136
[self initializeReturnKeyType];
132137
}
@@ -384,12 +389,27 @@ - (void)prepareForRecycle
384389
_lastStringStateWasUpdatedWith = nil;
385390
_ignoreNextTextInputCall = NO;
386391
_didMoveToWindow = NO;
392+
_didAutoFocus = NO;
393+
[_viewController reactRemoveViewControllerAppearanceListener:self];
394+
_viewController = nil;
387395
_backedTextInputView.inputAccessoryViewID = nil;
388396
_backedTextInputView.inputAccessoryView = nil;
389397
_hasInputAccessoryView = false;
390398
[_backedTextInputView resignFirstResponder];
391399
}
392400

401+
#pragma mark - RCTViewControllerAppearanceListener
402+
403+
- (void)reactViewControllerDidAppear:(UIViewController *)viewController animated:(BOOL)animated
404+
{
405+
const auto &props = static_cast<const TextInputProps &>(*_props);
406+
if (props.autoFocus && !_didAutoFocus) {
407+
_didAutoFocus = YES;
408+
[_backedTextInputView becomeFirstResponder];
409+
[self scrollCursorIntoView];
410+
}
411+
}
412+
393413
#pragma mark - RCTBackedTextInputDelegate
394414

395415
- (BOOL)textInputShouldBeginEditing

0 commit comments

Comments
 (0)