From ccf5c86bd761944dd65cffab3cde2ef61e75e652 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Sat, 13 Jun 2020 11:02:47 -0700 Subject: [PATCH] Implement autoFocus in TextInput Summary: Changelog: [Internal] Prop autoFocus was not implemented in Fabric's TextInput. Reviewed By: mdvacca Differential Revision: D22019333 fbshipit-source-id: 03f043b93e1079a5d0bff55b08ebc9d2f973c55b --- .../TextInput/RCTTextInputComponentView.mm | 16 ++++++++++++++++ .../textinput/iostextinput/TextInputProps.cpp | 4 +++- .../textinput/iostextinput/TextInputProps.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index 8d936feba22..35a2217e9dd 100644 --- a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -54,6 +54,7 @@ using namespace facebook::react; * In multiline text input this is undesirable as we don't want to be sending events for changes that JS triggered. */ BOOL _comingFromJS; + BOOL _didMoveToWindow; } - (instancetype)initWithFrame:(CGRect)frame @@ -67,12 +68,26 @@ using namespace facebook::react; _backedTextInputView.textInputDelegate = self; _ignoreNextTextInputCall = NO; _comingFromJS = NO; + _didMoveToWindow = NO; [self addSubview:_backedTextInputView]; } return self; } +- (void)didMoveToWindow +{ + [super didMoveToWindow]; + + if (self.window && !_didMoveToWindow) { + auto const &props = *std::static_pointer_cast(_props); + if (props.autoFocus) { + [_backedTextInputView becomeFirstResponder]; + } + _didMoveToWindow = YES; + } +} + #pragma mark - RCTComponentViewProtocol + (ComponentDescriptorProvider)componentDescriptorProvider @@ -237,6 +252,7 @@ using namespace facebook::react; _comingFromJS = NO; _lastStringStateWasUpdatedWith = nil; _ignoreNextTextInputCall = NO; + _didMoveToWindow = NO; } #pragma mark - RCTComponentViewProtocol diff --git a/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.cpp b/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.cpp index d98ac9d966d..91f4b61817a 100644 --- a/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.cpp +++ b/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.cpp @@ -54,7 +54,9 @@ TextInputProps::TextInputProps( rawProps, "mostRecentEventCount", sourceProps.mostRecentEventCount, - {})){}; + {})), + autoFocus( + convertRawProp(rawProps, "autoFocus", sourceProps.autoFocus, {})){}; TextAttributes TextInputProps::getEffectiveTextAttributes() const { auto result = TextAttributes::defaultTextAttributes(); diff --git a/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.h b/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.h index fa8d1365c42..440dbd9f10a 100644 --- a/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.h +++ b/ReactCommon/fabric/components/textinput/iostextinput/TextInputProps.h @@ -53,6 +53,8 @@ class TextInputProps final : public ViewProps, public BaseTextProps { std::string const text{}; int const mostRecentEventCount{0}; + bool autoFocus{false}; + /* * Accessors */