Skip to content

Commit

Permalink
Make textInputProps.submitBehavior on android an enum type (facebook#…
Browse files Browse the repository at this point in the history
…47206)

Summary:
Pull Request resolved: facebook#47206

Share definition of `SubmitBehavior` enum on ios, and use it on android

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D64718150

fbshipit-source-id: a078c9be44c625cc5e9001e4ae25d32eaaf5d8e4
  • Loading branch information
zeyap authored and facebook-github-bot committed Oct 28, 2024
1 parent 8b9535f commit db835e2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <react/renderer/components/textinput/basePrimitives.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawValue.h>
#include <string>

namespace facebook::react {

inline void fromRawValue(
const PropsParserContext& /*context*/,
const RawValue& value,
SubmitBehavior& result) {
auto string = static_cast<std::string>(value);
if (string == "newline") {
result = SubmitBehavior::Newline;
} else if (string == "submit") {
result = SubmitBehavior::Submit;
} else if (string == "blurAndSubmit") {
result = SubmitBehavior::BlurAndSubmit;
} else {
abort();
}
}

inline folly::dynamic toDynamic(const SubmitBehavior& value) {
switch (value) {
case SubmitBehavior::Newline:
return "newline";
case SubmitBehavior::Submit:
return "submit";
case SubmitBehavior::BlurAndSubmit:
return "blurAndSubmit";
case SubmitBehavior::Default:
return {nullptr};
}
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

namespace facebook::react {

enum class SubmitBehavior {
Default,
Submit,
BlurAndSubmit,
Newline,
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "AndroidTextInputProps.h"
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/components/image/conversions.h>
#include <react/renderer/components/textinput/baseConversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>

Expand Down Expand Up @@ -322,7 +323,7 @@ folly::dynamic AndroidTextInputProps::getDynamic() const {
props["value"] = value;
props["defaultValue"] = defaultValue;
props["selectTextOnFocus"] = selectTextOnFocus;
props["submitBehavior"] = submitBehavior;
props["submitBehavior"] = toDynamic(submitBehavior);
props["caretHidden"] = caretHidden;
props["contextMenuHidden"] = contextMenuHidden;
props["textShadowColor"] = toAndroidRepr(textShadowColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/components/textinput/BaseTextInputProps.h>
#include <react/renderer/components/textinput/basePrimitives.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/propsConversions.h>
#include <react/renderer/graphics/Color.h>
Expand Down Expand Up @@ -93,7 +94,7 @@ class AndroidTextInputProps final : public BaseTextInputProps {
bool secureTextEntry{false};
std::string value{};
bool selectTextOnFocus{false};
std::string submitBehavior{};
SubmitBehavior submitBehavior{};
bool caretHidden{false};
bool contextMenuHidden{false};
SharedColor textShadowColor{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/components/iostextinput/propsConversions.h>
#include <react/renderer/components/textinput/baseConversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,6 @@ inline void fromRawValue(
abort();
}

inline void fromRawValue(
const PropsParserContext& context,
const RawValue& value,
SubmitBehavior& result) {
auto string = (std::string)value;
if (string == "newline") {
result = SubmitBehavior::Newline;
return;
}
if (string == "submit") {
result = SubmitBehavior::Submit;
return;
}
if (string == "blurAndSubmit") {
result = SubmitBehavior::BlurAndSubmit;
return;
}
abort();
}

inline void fromRawValue(
const PropsParserContext& context,
const RawValue& value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <react/renderer/components/textinput/basePrimitives.h>
#include <optional>
#include <string>

Expand Down Expand Up @@ -47,14 +48,6 @@ enum class ReturnKeyType {
Continue,
};

// iOS & Android.
enum class SubmitBehavior {
Default,
Submit,
BlurAndSubmit,
Newline,
};

// iOS-only
enum class TextInputAccessoryVisibilityMode {
Never,
Expand Down

0 comments on commit db835e2

Please sign in to comment.