Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,54 @@ static std::string convertFontWeight(
return convertRawProp(context, rawProps, "fontWeight", sourceValue, {});
}

static AndroidTextInputEvents convertRawProp(
const PropsParserContext& context,
const RawProps& rawProps,
const AndroidTextInputEvents& sourceValue) {
using Offset = AndroidTextInputEvents::Offset;
AndroidTextInputEvents result{};
result.set(
Offset::KeyPress,
convertRawProp(
context,
rawProps,
"onKeyPress",
sourceValue[Offset::KeyPress],
false));
result.set(
Offset::SelectionChange,
convertRawProp(
context,
rawProps,
"onSelectionChange",
sourceValue[Offset::SelectionChange],
false));
result.set(
Offset::ContentSizeChange,
convertRawProp(
context,
rawProps,
"onContentSizeChange",
sourceValue[Offset::ContentSizeChange],
false));
result.set(
Offset::Scroll,
convertRawProp(
context, rawProps, "onScroll", sourceValue[Offset::Scroll], false));
return result;
}

static void updateEventProp(
folly::dynamic& result,
const AndroidTextInputEvents& newEvents,
const AndroidTextInputEvents& oldEvents,
AndroidTextInputEvents::Offset offset,
const char* name) {
if (newEvents[offset] != oldEvents[offset]) {
result[name] = newEvents[offset];
}
}

AndroidTextInputProps::AndroidTextInputProps(
const PropsParserContext &context,
const AndroidTextInputProps &sourceProps,
Expand Down Expand Up @@ -167,6 +215,7 @@ AndroidTextInputProps::AndroidTextInputProps(
{false})),
fontWeight(convertFontWeight(context, rawProps, sourceProps.fontWeight)),
fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
textInputEvents(convertRawProp(context, rawProps, sourceProps.textInputEvents)),
// See AndroidTextInputComponentDescriptor for usage
// TODO T63008435: can these, and this feature, be removed entirely?
hasPadding(hasValue(rawProps, sourceProps.hasPadding, "padding")),
Expand Down Expand Up @@ -198,6 +247,16 @@ AndroidTextInputProps::AndroidTextInputProps(
hasPaddingEnd(hasValue(rawProps, sourceProps.hasPaddingEnd, "paddingEnd")) {
}

#define TEXT_INPUT_EVENT_CASE(eventType) \
case CONSTEXPR_RAW_PROPS_KEY_HASH("on" #eventType): { \
bool res = false; \
if (value.hasValue()) { \
fromRawValue(context, value, res); \
} \
textInputEvents.set(AndroidTextInputEvents::Offset::eventType, res); \
return; \
}

void AndroidTextInputProps::setProp(
const PropsParserContext& context,
RawPropsPropNameHash hash,
Expand Down Expand Up @@ -242,6 +301,10 @@ void AndroidTextInputProps::setProp(
RAW_SET_PROP_SWITCH_CASE_BASIC(textAlign);
RAW_SET_PROP_SWITCH_CASE_BASIC(includeFontPadding);
RAW_SET_PROP_SWITCH_CASE_BASIC(fontFamily);
TEXT_INPUT_EVENT_CASE(KeyPress);
TEXT_INPUT_EVENT_CASE(SelectionChange);
TEXT_INPUT_EVENT_CASE(ContentSizeChange);
TEXT_INPUT_EVENT_CASE(Scroll);

case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): {
fromRawValue(context, value, this->value, {});
Expand Down Expand Up @@ -651,6 +714,27 @@ folly::dynamic AndroidTextInputProps::getDiffProps(
: folly::dynamic(nullptr);
}

if (textInputEvents != oldProps->textInputEvents) {
using Offset = AndroidTextInputEvents::Offset;
const auto& oldEvents = oldProps->textInputEvents;
updateEventProp(
result, textInputEvents, oldEvents, Offset::KeyPress, "onKeyPress");
updateEventProp(
result,
textInputEvents,
oldEvents,
Offset::SelectionChange,
"onSelectionChange");
updateEventProp(
result,
textInputEvents,
oldEvents,
Offset::ContentSizeChange,
"onContentSizeChange");
updateEventProp(
result, textInputEvents, oldEvents, Offset::Scroll, "onScroll");
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/components/textinput/BaseTextInputProps.h>
#include <react/renderer/components/textinput/basePrimitives.h>
#include <cstdint>
#include <unordered_map>

namespace facebook::react {
Expand Down Expand Up @@ -60,6 +61,35 @@ inline folly::dynamic toDynamic(const AndroidTextInputTextShadowOffsetStruct &va
return dynamicValue;
}

/*
* Whether JS listens to these events. ReactTextInputManager only sets up the
* native watchers for them when their props are sent. Stored in a uint8_t
* because a std::bitset takes at least a full machine word, even for 4 bits.
*/
struct AndroidTextInputEvents {
uint8_t bits{};

enum class Offset : uint8_t {
KeyPress = 0,
SelectionChange = 1,
ContentSizeChange = 2,
Scroll = 3,
};

constexpr bool operator[](const Offset offset) const
{
return ((bits >> static_cast<uint8_t>(offset)) & 1) != 0;
}

constexpr void set(const Offset offset, const bool value)
{
const auto mask = static_cast<uint8_t>(1 << static_cast<uint8_t>(offset));
bits = static_cast<uint8_t>(value ? (bits | mask) : (bits & ~mask));
}

bool operator==(const AndroidTextInputEvents &rhs) const = default;
};

class AndroidTextInputProps final : public BaseTextInputProps {
public:
AndroidTextInputProps() = default;
Expand Down Expand Up @@ -109,6 +139,9 @@ class AndroidTextInputProps final : public BaseTextInputProps {
std::string fontWeight{};
std::string fontFamily{};

// Named so it doesn't hide BaseViewProps::events.
AndroidTextInputEvents textInputEvents{};

/**
* Auxiliary information to detect if these props are set or not.
* See AndroidTextInputComponentDescriptor for usage.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

#include <gtest/gtest.h>

#if defined(ANDROID) && defined(RN_SERIALIZABLE_STATE)

#include <react/renderer/components/androidtextinput/AndroidTextInputProps.h>
#include <react/test_utils/PropsPayloads.h>

namespace facebook::react {

// ReactTextInputManager only sets up the event watchers when these props are
// sent. JS sends a handler as `true`, and false resets it like null does.
TEST(AndroidTextInputPropsDiffTest, eventHandlerPropsAreSent) {
using folly::dynamic;
expectPayloadsMatchRawProps<AndroidTextInputProps>({
{dynamic::object("onKeyPress", true)("onSelectionChange", true)(
"onContentSizeChange", true)("onScroll", true)},
{dynamic::object("onKeyPress", nullptr)("onSelectionChange", nullptr)(
"onContentSizeChange", nullptr)("onScroll", nullptr),
dynamic::object("onKeyPress", false)("onSelectionChange", false)(
"onContentSizeChange", false)("onScroll", false)},
});
}

} // namespace facebook::react

#endif
15 changes: 15 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@ class facebook::react::AndroidTextInputProps : public facebook::react::BaseTextI
public bool secureTextEntry;
public bool selectTextOnFocus;
public bool showSoftInputOnFocus;
public facebook::react::AndroidTextInputEvents textInputEvents;
public facebook::react::AndroidTextInputTextShadowOffsetStruct textShadowOffset;
public facebook::react::Float fontSize;
public facebook::react::Float letterSpacing;
Expand Down Expand Up @@ -5565,6 +5566,20 @@ struct facebook::react::ActivePointer {
public facebook::react::PointerEvent event;
}

struct facebook::react::AndroidTextInputEvents {
public bool operator==(const facebook::react::AndroidTextInputEvents& rhs) const = default;
public constexpr bool operator[](const facebook::react::AndroidTextInputEvents::Offset offset) const;
public constexpr void set(const facebook::react::AndroidTextInputEvents::Offset offset, const bool value);
public uint8_t bits;
}

enum facebook::react::AndroidTextInputEvents::Offset : uint8_t {
ContentSizeChange,
KeyPress,
Scroll,
SelectionChange,
}

struct facebook::react::AndroidTextInputTextShadowOffsetStruct {
public double height;
public double width;
Expand Down
Loading