Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Text running flattenStyle multiple times #14041

Merged
merged 3 commits into from
Oct 30, 2024
Merged
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
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix flatten style running multiple times\"",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
81 changes: 23 additions & 58 deletions vnext/src-win/Libraries/Text/Text.windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @format
*/

import type {TextStyleProp} from '../StyleSheet/StyleSheet';
import type {____TextStyle_Internal as TextStyleInternal} from '../StyleSheet/StyleSheetTypes';
import type {PressEvent} from '../Types/CoreEventTypes';
import type {NativeTextProps} from './TextNativeComponent';
import type {PressRetentionOffset, TextProps} from './TextProps';
Expand All @@ -23,7 +25,7 @@ import * as React from 'react';
import {useContext, useMemo, useState} from 'react';

const View = require('../Components/View/View'); // [Windows]
import {type TextStyleProp, type ViewStyleProp} from '../StyleSheet/StyleSheet'; // [Windows]
import {type ViewStyleProp} from '../StyleSheet/StyleSheet'; // [Windows]

type TextForwardRef = React.ElementRef<
typeof NativeText | typeof NativeVirtualText,
Expand Down Expand Up @@ -144,25 +146,32 @@ const Text: React.AbstractComponent<TextProps, TextForwardRef> =

let _selectable = selectable;

const processedStyle = flattenStyle(_style);
let processedStyle = flattenStyle<TextStyleProp>(_style);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why moving to let instead of const? It seems like the variable isn't reassigned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, this is actually an upstream file: https://github.com/facebook/react-native/pull/45340/files, so this change comes from Meta!

if (processedStyle != null) {
let overrides: ?{...TextStyleInternal} = null;
if (typeof processedStyle.fontWeight === 'number') {
// $FlowFixMe[cannot-write]
processedStyle.fontWeight = processedStyle.fontWeight.toString();
overrides = overrides || ({}: {...TextStyleInternal});
overrides.fontWeight =
// $FlowFixMe[incompatible-cast]
(processedStyle.fontWeight.toString(): TextStyleInternal['fontWeight']);
}

if (processedStyle.userSelect != null) {
_selectable = userSelectToSelectableMap[processedStyle.userSelect];
// $FlowFixMe[cannot-write]
delete processedStyle.userSelect;
overrides = overrides || ({}: {...TextStyleInternal});
overrides.userSelect = undefined;
}

if (processedStyle.verticalAlign != null) {
// $FlowFixMe[cannot-write]
processedStyle.textAlignVertical =
overrides = overrides || ({}: {...TextStyleInternal});
overrides.textAlignVertical =
verticalAlignToTextAlignVerticalMap[processedStyle.verticalAlign];
// $FlowFixMe[cannot-write]
delete processedStyle.verticalAlign;
overrides.verticalAlign = undefined;
}

if (overrides != null) {
// $FlowFixMe[incompatible-type]
_style = [_style, overrides];
}
}

Expand All @@ -185,7 +194,7 @@ const Text: React.AbstractComponent<TextProps, TextForwardRef> =
numberOfLines: _numberOfLines,
selectable: _selectable,
selectionColor: _selectionColor,
style: processedStyle,
style: _style,
disabled: disabled,
children,
}}
Expand Down Expand Up @@ -222,7 +231,7 @@ const Text: React.AbstractComponent<TextProps, TextForwardRef> =
ref={forwardedRef}
selectable={_selectable}
selectionColor={_selectionColor}
style={processedStyle}
style={_style}
disabled={disabled}>
{children}
</NativeVirtualText>
Expand Down Expand Up @@ -269,7 +278,7 @@ const Text: React.AbstractComponent<TextProps, TextForwardRef> =
numberOfLines: _numberOfLines,
selectable: _selectable,
selectionColor: _selectionColor,
style: processedStyle,
style: _style,
children,
}}
textPressabilityProps={{
Expand Down Expand Up @@ -307,7 +316,7 @@ const Text: React.AbstractComponent<TextProps, TextForwardRef> =
ref={forwardedRef}
selectable={_selectable}
selectionColor={_selectionColor}
style={processedStyle}>
style={_style}>
{children}
</NativeText>
);
Expand All @@ -328,50 +337,6 @@ const Text: React.AbstractComponent<TextProps, TextForwardRef> =
styleProps.borderStartWidth != null ||
styleProps.borderTopWidth != null)
) {
let textStyleProps = Array.isArray(styleProps)
? // $FlowFixMe[underconstrained-implicit-instantiation]
flattenStyle(styleProps)
: styleProps;
let {
// $FlowFixMe[prop-missing]
margin,
// $FlowFixMe[prop-missing]
marginBottom,
// $FlowFixMe[prop-missing]
marginEnd,
// $FlowFixMe[prop-missing]
marginHorizontal,
// $FlowFixMe[prop-missing]
marginLeft,
// $FlowFixMe[prop-missing]
marginRight,
// $FlowFixMe[prop-missing]
marginStart,
// $FlowFixMe[prop-missing]
marginTop,
// $FlowFixMe[prop-missing]
marginVertical,
// $FlowFixMe[prop-missing]
padding,
// $FlowFixMe[prop-missing]
paddingBottom,
// $FlowFixMe[prop-missing]
paddingEnd,
// $FlowFixMe[prop-missing]
paddingHorizontal,
// $FlowFixMe[prop-missing]
paddingLeft,
// $FlowFixMe[prop-missing]
paddingRight,
// $FlowFixMe[prop-missing]
paddingStart,
// $FlowFixMe[prop-missing]
paddingTop,
// $FlowFixMe[prop-missing]
paddingVertical,
// $FlowFixMe[not-an-object]
...rest
} = textStyleProps != null ? textStyleProps : {};
return (
<View style={styleProps}>
<TextAncestor.Provider value={true}>
Expand Down
Loading