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
This commit is contained in:
Zeya Peng
2024-10-28 10:17:02 -07:00
committed by Facebook GitHub Bot
parent db835e28d8
commit b5b65275c6
8 changed files with 37 additions and 50 deletions
@@ -78,7 +78,7 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
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 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
const auto &newTextInputProps = static_cast<const TextInputProps &>(*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 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
return;
}
const auto &props = static_cast<const TextInputProps &>(*_props);
if (props.traits.multiline && ![_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) {
if (props.multiline && ![_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) {
[self textInputDidChange];
_ignoreNextTextInputCall = YES;
}
@@ -785,14 +785,7 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
- (SubmitBehavior)getSubmitBehavior
{
const auto &props = static_cast<const TextInputProps &>(*_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
@@ -17,6 +17,7 @@
#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/components/image/conversions.h>
#include <react/renderer/components/textinput/baseConversions.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/graphics/Color.h>
@@ -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
@@ -9,6 +9,7 @@
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/components/text/BaseTextProps.h>
#include <react/renderer/components/textinput/basePrimitives.h>
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/graphics/Color.h>
@@ -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
@@ -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);
@@ -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{};
@@ -65,7 +65,7 @@ TextAttributes TextInputProps::getEffectiveTextAttributes(
ParagraphAttributes TextInputProps::getEffectiveParagraphAttributes() const {
auto result = paragraphAttributes;
if (!traits.multiline) {
if (!multiline) {
result.maximumNumberOfLines = 1;
}
@@ -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`.
@@ -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,