Skip to content

Commit

Permalink
Factor out common parts of TextInputProps between Android/iOS
Browse files Browse the repository at this point in the history
Summary:
## Changelog:
[Internal] - 

This takes all the common props for TextInput between Android and iOS and factors them out into a single, platform independent props data structure, `BaseTextInputProps`.

This way it's both easier to manage the corresponding props, but also making this easier to be used on other platforms.

Differential Revision: D54764898
  • Loading branch information
rshest authored and facebook-github-bot committed Mar 12, 2024
1 parent bdca260 commit 2a73a33
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 234 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* 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 "BaseTextInputProps.h"

#include <react/renderer/core/propsConversions.h>

#include <react/renderer/core/Props.h>
#include <react/renderer/core/PropsMacros.h>

#include <react/renderer/graphics/Color.h>

#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/components/image/conversions.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/imagemanager/primitives.h>
#include <react/utils/CoreFeatures.h>

namespace facebook::react {

BaseTextInputProps::BaseTextInputProps(
const PropsParserContext& context,
const BaseTextInputProps& sourceProps,
const RawProps& rawProps)
: ViewProps(context, sourceProps, rawProps),
BaseTextProps(context, sourceProps, rawProps),
paragraphAttributes(convertRawProp(
context,
rawProps,
sourceProps.paragraphAttributes,
{})),
defaultValue(convertRawProp(
context,
rawProps,
"defaultValue",
sourceProps.defaultValue,
{})),
placeholder(convertRawProp(
context,
rawProps,
"placeholder",
sourceProps.placeholder,
{})),
placeholderTextColor(convertRawProp(
context,
rawProps,
"placeholderTextColor",
sourceProps.placeholderTextColor,
{})),
cursorColor(convertRawProp(
context,
rawProps,
"cursorColor",
sourceProps.cursorColor,
{})),
selectionColor(convertRawProp(
context,
rawProps,
"selectionColor",
sourceProps.selectionColor,
{})),
selectionHandleColor(convertRawProp(
context,
rawProps,
"selectionHandleColor",
sourceProps.selectionHandleColor,
{})),
underlineColorAndroid(convertRawProp(
context,
rawProps,
"underlineColorAndroid",
sourceProps.underlineColorAndroid,
{})),
maxLength(convertRawProp(
context,
rawProps,
"maxLength",
sourceProps.maxLength,
{})),
text(convertRawProp(context, rawProps, "text", sourceProps.text, {})),
mostRecentEventCount(convertRawProp(
context,
rawProps,
"mostRecentEventCount",
sourceProps.mostRecentEventCount,
{})),
autoFocus(convertRawProp(
context,
rawProps,
"autoFocus",
sourceProps.autoFocus,
{})){};

void BaseTextInputProps::setProp(
const PropsParserContext& context,
RawPropsPropNameHash hash,
const char* propName,
const RawValue& value) {
ViewProps::setProp(context, hash, propName, value);
BaseTextProps::setProp(context, hash, propName, value);

static auto defaults = BaseTextInputProps{};

// ParagraphAttributes has its own switch statement - to keep all
// of these fields together, and because there are some collisions between
// propnames parsed here and outside of ParagraphAttributes. For example,
// textBreakStrategy is duplicated.
// This code is also duplicated in ParagraphProps.
static auto paDefaults = ParagraphAttributes{};
switch (hash) {
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
maximumNumberOfLines,
"numberOfLines");
REBUILD_FIELD_SWITCH_CASE(
paDefaults, value, paragraphAttributes, ellipsizeMode, "ellipsizeMode");
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
textBreakStrategy,
"textBreakStrategy");
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
adjustsFontSizeToFit,
"adjustsFontSizeToFit");
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
minimumFontSize,
"minimumFontSize");
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
maximumFontSize,
"maximumFontSize");
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
includeFontPadding,
"includeFontPadding");
REBUILD_FIELD_SWITCH_CASE(
paDefaults,
value,
paragraphAttributes,
android_hyphenationFrequency,
"android_hyphenationFrequency");
}

switch (hash) {
RAW_SET_PROP_SWITCH_CASE_BASIC(underlineColorAndroid);
RAW_SET_PROP_SWITCH_CASE_BASIC(autoFocus);
RAW_SET_PROP_SWITCH_CASE_BASIC(maxLength);
RAW_SET_PROP_SWITCH_CASE_BASIC(placeholder);
RAW_SET_PROP_SWITCH_CASE_BASIC(placeholderTextColor);
RAW_SET_PROP_SWITCH_CASE_BASIC(selectionColor);
RAW_SET_PROP_SWITCH_CASE_BASIC(selectionHandleColor);
RAW_SET_PROP_SWITCH_CASE_BASIC(defaultValue);
RAW_SET_PROP_SWITCH_CASE_BASIC(cursorColor);
RAW_SET_PROP_SWITCH_CASE_BASIC(text);
RAW_SET_PROP_SWITCH_CASE_BASIC(mostRecentEventCount);
}
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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/attributedstring/ParagraphAttributes.h>
#include <react/renderer/components/text/BaseTextProps.h>
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/graphics/Color.h>
#include <string>

namespace facebook::react {

class BaseTextInputProps : public ViewProps, public BaseTextProps {
public:
BaseTextInputProps() = default;
BaseTextInputProps(
const PropsParserContext& context,
const BaseTextInputProps& sourceProps,
const RawProps& rawProps);

void setProp(
const PropsParserContext& context,
RawPropsPropNameHash hash,
const char* propName,
const RawValue& value);

#pragma mark - Props

/*
* Contains all prop values that affect visual representation of the
* paragraph.
*/
ParagraphAttributes paragraphAttributes{};

std::string defaultValue{};

std::string placeholder{};
SharedColor placeholderTextColor{};

/*
* Tint colors
*/
SharedColor cursorColor{};
SharedColor selectionColor{};
SharedColor selectionHandleColor{};
// TODO: Rename to `tintColor` and make universal.
SharedColor underlineColorAndroid{};

int maxLength{};

/*
* "Private" (only used by TextInput.js) props
*/
std::string text{};
int mostRecentEventCount{0};

bool autoFocus{false};
};

} // namespace facebook::react
Loading

0 comments on commit 2a73a33

Please sign in to comment.