From f1c1f8116ba1cfa9d10c5b8c30b98b796047b9c2 Mon Sep 17 00:00:00 2001 From: ankit-tailor Date: Mon, 12 Sep 2022 02:12:23 -0700 Subject: [PATCH] Feat/fontweight number value (#34598) Summary: This PR adds support for number values for `fontWeight` as requested in https://github.com/facebook/react-native/issues/34425. ## Changelog [General] [Added] - Added support for number values in fontWeight. Pull Request resolved: https://github.com/facebook/react-native/pull/34598 Test Plan: ```js Hello World ``` Reviewed By: jacdebug Differential Revision: D39268920 Pulled By: cipolleschi fbshipit-source-id: 9bb711677bf173f9904b74f382659042856efd83 --- Libraries/StyleSheet/StyleSheetTypes.js | 38 ++++++++++++++++++- Libraries/Text/Text.js | 12 ++++-- .../js/examples/Text/TextExample.android.js | 9 +++++ .../js/examples/Text/TextExample.ios.js | 9 +++++ 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/Libraries/StyleSheet/StyleSheetTypes.js b/Libraries/StyleSheet/StyleSheetTypes.js index e667dddf911c4c..8537b6c96de1bb 100644 --- a/Libraries/StyleSheet/StyleSheetTypes.js +++ b/Libraries/StyleSheet/StyleSheetTypes.js @@ -566,6 +566,23 @@ export type ____ViewStyle_Internal = $ReadOnly<{ ...____ViewStyle_InternalOverrides, }>; +export type FontStyleType = { + fontFamily: string, + fontWeight: ____FontWeight_Internal, +}; + +export type FontStyleMap = { + ultraLight: FontStyleType, + thin: FontStyleType, + light: FontStyleType, + regular: FontStyleType, + medium: FontStyleType, + semibold: FontStyleType, + bold: FontStyleType, + heavy: FontStyleType, + black: FontStyleType, +}; + export type ____FontWeight_Internal = | 'normal' | 'bold' @@ -577,7 +594,26 @@ export type ____FontWeight_Internal = | '600' | '700' | '800' - | '900'; + | '900' + | 100 + | 200 + | 300 + | 400 + | 500 + | 600 + | 700 + | 800 + | 900 + | 'ultralight' + | 'thin' + | 'light' + | 'medium' + | 'regular' + | 'semibold' + | 'condensedBold' + | 'condensed' + | 'heavy' + | 'black'; export type ____TextStyle_InternalCore = $ReadOnly<{ ...$Exact<____ViewStyle_Internal>, diff --git a/Libraries/Text/Text.js b/Libraries/Text/Text.js index c971942a01376a..8539f92c422d1f 100644 --- a/Libraries/Text/Text.js +++ b/Libraries/Text/Text.js @@ -14,13 +14,13 @@ import Platform from '../Utilities/Platform'; import * as PressabilityDebug from '../Pressability/PressabilityDebug'; import usePressability from '../Pressability/usePressability'; import StyleSheet from '../StyleSheet/StyleSheet'; +import flattenStyle from '../StyleSheet/flattenStyle'; import processColor from '../StyleSheet/processColor'; import TextAncestor from './TextAncestor'; import {NativeText, NativeVirtualText} from './TextNativeComponent'; import {type TextProps} from './TextProps'; import * as React from 'react'; import {useContext, useMemo, useState} from 'react'; -import flattenStyle from '../StyleSheet/flattenStyle'; /** * Text is the fundamental component for displaying text. @@ -201,6 +201,12 @@ const Text: React.AbstractComponent< default: accessible, }); + let flattenedStyle = flattenStyle(style); + + if (typeof flattenedStyle?.fontWeight === 'number') { + flattenedStyle.fontWeight = flattenedStyle?.fontWeight.toString(); + } + return hasTextAncestor ? ( ) : ( @@ -232,7 +238,7 @@ const Text: React.AbstractComponent< nativeID={id ?? nativeID} numberOfLines={numberOfLines} selectionColor={selectionColor} - style={style} + style={flattenedStyle} ref={forwardedRef} /> diff --git a/packages/rn-tester/js/examples/Text/TextExample.android.js b/packages/rn-tester/js/examples/Text/TextExample.android.js index 45826991731aca..6779684096e8b8 100644 --- a/packages/rn-tester/js/examples/Text/TextExample.android.js +++ b/packages/rn-tester/js/examples/Text/TextExample.android.js @@ -392,6 +392,15 @@ class TextExample extends React.Component<{...}> { FONT WEIGHT 300 FONT WEIGHT 200 FONT WEIGHT 100 + FONT WEIGHT 900 + FONT WEIGHT 800 + FONT WEIGHT 700 + FONT WEIGHT 600 + FONT WEIGHT 500 + FONT WEIGHT 400 + FONT WEIGHT 300 + FONT WEIGHT 200 + FONT WEIGHT 100 Move fast and be italic diff --git a/packages/rn-tester/js/examples/Text/TextExample.ios.js b/packages/rn-tester/js/examples/Text/TextExample.ios.js index d24c35ca1638eb..9f92af41c71252 100644 --- a/packages/rn-tester/js/examples/Text/TextExample.ios.js +++ b/packages/rn-tester/js/examples/Text/TextExample.ios.js @@ -681,6 +681,15 @@ exports.examples = [ FONT WEIGHT 300 FONT WEIGHT 200 FONT WEIGHT 100 + FONT WEIGHT 900 + FONT WEIGHT 800 + FONT WEIGHT 700 + FONT WEIGHT 600 + FONT WEIGHT 500 + FONT WEIGHT 400 + FONT WEIGHT 300 + FONT WEIGHT 200 + FONT WEIGHT 100 ); },