Skip to content

Commit b6bdac4

Browse files
Accept a numeric fontWeight in the Android TextInput native code
1 parent f4f8b6a commit b6bdac4

4 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎packages/react-native/ReactAndroid/api/ReactAndroid.api‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6365,7 +6365,7 @@ public class com/facebook/react/views/textinput/ReactTextInputManager : com/face
63656365
public final fun setFontStyle (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V
63666366
public final fun setFontVariant (Lcom/facebook/react/views/textinput/ReactEditText;Lcom/facebook/react/bridge/ReadableArray;)V
63676367
public final fun setFontVariationSettings (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V
6368-
public final fun setFontWeight (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V
6368+
public final fun setFontWeight (Lcom/facebook/react/views/textinput/ReactEditText;Lcom/facebook/react/bridge/Dynamic;)V
63696369
public final fun setImportantForAutofill (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V
63706370
public final fun setIncludeFontPadding (Lcom/facebook/react/views/textinput/ReactEditText;Z)V
63716371
public final fun setInlineImageLeft (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,16 @@ public open class ReactTextInputManager public constructor() :
242242
}
243243

244244
@ReactProp(name = ViewProps.FONT_WEIGHT)
245-
public fun setFontWeight(view: ReactEditText, fontWeight: String?) {
246-
view.setFontWeight(fontWeight)
245+
public fun setFontWeight(view: ReactEditText, fontWeight: Dynamic) {
246+
// Props 1.0 and 1.5 send the raw JS value, which can be a number (700). Props 2.0 sends
247+
// the string from C++, so the number case can go once TextInput uses only Props 2.0.
248+
view.setFontWeight(
249+
when (fontWeight.type) {
250+
ReadableType.Number -> fontWeight.asInt().toString()
251+
ReadableType.String -> fontWeight.asString()
252+
else -> null
253+
}
254+
)
247255
}
248256

249257
@ReactProp(name = ViewProps.FONT_STYLE)

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,19 @@ class ReactTextInputPropertyTest {
560560
assertThat(view.parsedFontVariationSettings).isNull()
561561
}
562562

563+
@Test
564+
fun testFontWeightAcceptsANumber() {
565+
manager.updateProperties(view, buildStyles())
566+
val defaultTypeface = view.typeface
567+
manager.updateProperties(view, buildStyles("fontWeight", "700"))
568+
val boldTypeface = view.typeface
569+
assertThat(boldTypeface).isNotEqualTo(defaultTypeface)
570+
571+
manager.updateProperties(view, buildStyles("fontWeight", null))
572+
manager.updateProperties(view, buildStyles("fontWeight", 700.0))
573+
assertThat(view.typeface).isEqualTo(boldTypeface)
574+
}
575+
563576
@Test
564577
fun testFontVariationSettingsOverrideFontWeightRegardlessOfPropOrder() {
565578
manager.updateProperties(

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ hasValue(const RawProps& rawProps, bool defaultValue, const char* name) {
3333
return false;
3434
}
3535

36+
// JS allows a numeric `fontWeight`, for example 700. This field is a string.
37+
static std::string fontWeightToString(double fontWeight) {
38+
return std::to_string(static_cast<int>(fontWeight));
39+
}
40+
41+
static std::string convertFontWeight(
42+
const PropsParserContext& context,
43+
const RawProps& rawProps,
44+
const std::string& sourceValue) {
45+
const auto* rawValue = rawProps.at("fontWeight");
46+
if (rawValue != nullptr && rawValue->hasType<double>()) {
47+
return fontWeightToString((double)*rawValue);
48+
}
49+
return convertRawProp(context, rawProps, "fontWeight", sourceValue, {});
50+
}
51+
3652
AndroidTextInputProps::AndroidTextInputProps(
3753
const PropsParserContext &context,
3854
const AndroidTextInputProps &sourceProps,
@@ -149,7 +165,7 @@ AndroidTextInputProps::AndroidTextInputProps(
149165
"includeFontPadding",
150166
sourceProps.includeFontPadding,
151167
{false})),
152-
fontWeight(convertRawProp(context, rawProps, "fontWeight", sourceProps.fontWeight, {})),
168+
fontWeight(convertFontWeight(context, rawProps, sourceProps.fontWeight)),
153169
fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
154170
// See AndroidTextInputComponentDescriptor for usage
155171
// TODO T63008435: can these, and this feature, be removed entirely?
@@ -225,13 +241,20 @@ void AndroidTextInputProps::setProp(
225241
RAW_SET_PROP_SWITCH_CASE_BASIC(fontSize);
226242
RAW_SET_PROP_SWITCH_CASE_BASIC(textAlign);
227243
RAW_SET_PROP_SWITCH_CASE_BASIC(includeFontPadding);
228-
RAW_SET_PROP_SWITCH_CASE_BASIC(fontWeight);
229244
RAW_SET_PROP_SWITCH_CASE_BASIC(fontFamily);
230245

231246
case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): {
232247
fromRawValue(context, value, this->value, {});
233248
return;
234249
}
250+
case CONSTEXPR_RAW_PROPS_KEY_HASH("fontWeight"): {
251+
if (value.hasType<double>()) {
252+
fontWeight = fontWeightToString((double)value);
253+
} else {
254+
fromRawValue(context, value, fontWeight, defaults.fontWeight);
255+
}
256+
return;
257+
}
235258

236259
// Paddings are not parsed at this level of the component (they're parsed in
237260
// ViewProps) but we do need to know if they're present or not. See

0 commit comments

Comments
 (0)