From b5b65275c6f97fae5e21c57bf2fd985e71cb751f Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Mon, 28 Oct 2024 10:17:02 -0700 Subject: [PATCH] move `multiline` and `submitBehavior` down to BaseTextInputProps (#47205) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/47205 move `multiline` and `submitBehavior` from ios and android respectively to BaseTextInputProps Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D64494449 fbshipit-source-id: ac737af27ba21ee886c92667f6c51945738bfc02 --- .../TextInput/RCTTextInputComponentView.mm | 17 ++++--------- .../textinput/BaseTextInputProps.cpp | 25 ++++++++++++++++++- .../components/textinput/BaseTextInputProps.h | 7 ++++++ .../AndroidTextInputProps.cpp | 10 -------- .../androidtextinput/AndroidTextInputProps.h | 2 -- .../iostextinput/TextInputProps.cpp | 2 +- .../components/iostextinput/primitives.h | 12 --------- .../iostextinput/propsConversions.h | 12 --------- 8 files changed, 37 insertions(+), 50 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index e74500f08a3ae3..00cf7563d35492 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -78,7 +78,7 @@ - (instancetype)initWithFrame:(CGRect)frame const auto &defaultProps = TextInputShadowNode::defaultSharedProps(); _props = defaultProps; - _backedTextInputView = defaultProps->traits.multiline ? [RCTUITextView new] : [RCTUITextField new]; + _backedTextInputView = defaultProps->multiline ? [RCTUITextView new] : [RCTUITextField new]; _backedTextInputView.textInputDelegate = self; _ignoreNextTextInputCall = NO; _comingFromJS = NO; @@ -166,8 +166,8 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared & const auto &newTextInputProps = static_cast(*props); // Traits: - if (newTextInputProps.traits.multiline != oldTextInputProps.traits.multiline) { - [self _setMultiline:newTextInputProps.traits.multiline]; + if (newTextInputProps.multiline != oldTextInputProps.multiline) { + [self _setMultiline:newTextInputProps.multiline]; } if (newTextInputProps.traits.autocapitalizationType != oldTextInputProps.traits.autocapitalizationType) { @@ -448,7 +448,7 @@ - (void)textInputDidChangeSelection return; } const auto &props = static_cast(*_props); - if (props.traits.multiline && ![_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) { + if (props.multiline && ![_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) { [self textInputDidChange]; _ignoreNextTextInputCall = YES; } @@ -785,14 +785,7 @@ - (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldTe - (SubmitBehavior)getSubmitBehavior { const auto &props = static_cast(*_props); - const SubmitBehavior submitBehaviorDefaultable = props.traits.submitBehavior; - - // We should always have a non-default `submitBehavior`, but in case we don't, set it based on multiline. - if (submitBehaviorDefaultable == SubmitBehavior::Default) { - return props.traits.multiline ? SubmitBehavior::Newline : SubmitBehavior::BlurAndSubmit; - } - - return submitBehaviorDefaultable; + return props.getNonDefaultSubmitBehavior(); } @end diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.cpp index 3285754ea20f97..80cbdc55dddb4f 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -113,7 +114,19 @@ BaseTextInputProps::BaseTextInputProps( rawProps, "readOnly", sourceProps.readOnly, - {})) {} + {})), + submitBehavior(convertRawProp( + context, + rawProps, + "submitBehavior", + sourceProps.submitBehavior, + {})), + multiline(convertRawProp( + context, + rawProps, + "multiline", + sourceProps.multiline, + {false})) {} void BaseTextInputProps::setProp( const PropsParserContext& context, @@ -193,7 +206,17 @@ void BaseTextInputProps::setProp( RAW_SET_PROP_SWITCH_CASE_BASIC(autoCapitalize); RAW_SET_PROP_SWITCH_CASE_BASIC(editable); RAW_SET_PROP_SWITCH_CASE_BASIC(readOnly); + RAW_SET_PROP_SWITCH_CASE_BASIC(submitBehavior); + RAW_SET_PROP_SWITCH_CASE_BASIC(multiline); } } +SubmitBehavior BaseTextInputProps::getNonDefaultSubmitBehavior() const { + if (submitBehavior == SubmitBehavior::Default) { + return multiline ? SubmitBehavior::Newline : SubmitBehavior::BlurAndSubmit; + } + + return submitBehavior; +} + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.h index d6476254d7a1eb..36c199b3515a83 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputProps.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -66,6 +67,12 @@ class BaseTextInputProps : public ViewProps, public BaseTextProps { bool editable{true}; bool readOnly{false}; + + SubmitBehavior submitBehavior{SubmitBehavior::Default}; + + bool multiline{false}; + + SubmitBehavior getNonDefaultSubmitBehavior() const; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp index 256d7679c653a6..4ce03856fab1c8 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp @@ -96,10 +96,6 @@ AndroidTextInputProps::AndroidTextInputProps( "returnKeyType", sourceProps.returnKeyType, {})), - multiline(ReactNativeFeatureFlags::enableCppPropsIteratorSetter()? sourceProps.multiline : convertRawProp(context, rawProps, - "multiline", - sourceProps.multiline, - {false})), secureTextEntry(ReactNativeFeatureFlags::enableCppPropsIteratorSetter()? sourceProps.secureTextEntry : convertRawProp(context, rawProps, "secureTextEntry", sourceProps.secureTextEntry, @@ -109,10 +105,6 @@ AndroidTextInputProps::AndroidTextInputProps( "selectTextOnFocus", sourceProps.selectTextOnFocus, {false})), - submitBehavior(ReactNativeFeatureFlags::enableCppPropsIteratorSetter()? sourceProps.submitBehavior : convertRawProp(context, rawProps, - "submitBehavior", - sourceProps.submitBehavior, - {})), caretHidden(ReactNativeFeatureFlags::enableCppPropsIteratorSetter()? sourceProps.caretHidden : convertRawProp(context, rawProps, "caretHidden", sourceProps.caretHidden, @@ -224,10 +216,8 @@ void AndroidTextInputProps::setProp( RAW_SET_PROP_SWITCH_CASE_BASIC(maxFontSizeMultiplier); RAW_SET_PROP_SWITCH_CASE_BASIC(keyboardType); RAW_SET_PROP_SWITCH_CASE_BASIC(returnKeyType); - RAW_SET_PROP_SWITCH_CASE_BASIC(multiline); RAW_SET_PROP_SWITCH_CASE_BASIC(secureTextEntry); RAW_SET_PROP_SWITCH_CASE_BASIC(selectTextOnFocus); - RAW_SET_PROP_SWITCH_CASE_BASIC(submitBehavior); RAW_SET_PROP_SWITCH_CASE_BASIC(caretHidden); RAW_SET_PROP_SWITCH_CASE_BASIC(contextMenuHidden); RAW_SET_PROP_SWITCH_CASE_BASIC(textShadowColor); diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h index 487d25685e891d..f52eb13221f33c 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h @@ -90,11 +90,9 @@ class AndroidTextInputProps final : public BaseTextInputProps { Float maxFontSizeMultiplier{0.0}; std::string keyboardType{}; std::string returnKeyType{}; - bool multiline{false}; bool secureTextEntry{false}; std::string value{}; bool selectTextOnFocus{false}; - SubmitBehavior submitBehavior{}; bool caretHidden{false}; bool contextMenuHidden{false}; SharedColor textShadowColor{}; diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/TextInputProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/TextInputProps.cpp index e6e849382310c0..ac858630490913 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/TextInputProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/TextInputProps.cpp @@ -65,7 +65,7 @@ TextAttributes TextInputProps::getEffectiveTextAttributes( ParagraphAttributes TextInputProps::getEffectiveParagraphAttributes() const { auto result = paragraphAttributes; - if (!traits.multiline) { + if (!multiline) { result.maximumNumberOfLines = 1; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/primitives.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/primitives.h index ce977821f7275f..a6e6dfd2a49c41 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/primitives.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/primitives.h @@ -87,12 +87,6 @@ class Selection final { */ class TextInputTraits final { public: - /* - * iOS & Android - * Default value: `false`. - */ - bool multiline{false}; - /* * iOS & Android * Default value: `Sentences`. @@ -168,12 +162,6 @@ class TextInputTraits final { */ bool secureTextEntry{false}; - /* - * iOS & Android - * Default value: `Default`. - */ - SubmitBehavior submitBehavior{SubmitBehavior::Default}; - /* * iOS-only (implemented only on iOS for now) * Default value: `false`. diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/propsConversions.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/propsConversions.h index 109f5a1237be0a..ccbbe207736728 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/propsConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/ios/react/renderer/components/iostextinput/propsConversions.h @@ -21,12 +21,6 @@ static TextInputTraits convertRawProp( const TextInputTraits& defaultTraits) { auto traits = TextInputTraits{}; - traits.multiline = convertRawProp( - context, - rawProps, - "multiline", - sourceTraits.multiline, - defaultTraits.multiline); traits.autocapitalizationType = convertRawProp( context, rawProps, @@ -93,12 +87,6 @@ static TextInputTraits convertRawProp( "secureTextEntry", sourceTraits.secureTextEntry, defaultTraits.secureTextEntry); - traits.submitBehavior = convertRawProp( - context, - rawProps, - "submitBehavior", - sourceTraits.submitBehavior, - defaultTraits.submitBehavior); traits.clearTextOnFocus = convertRawProp( context, rawProps,