react-native/packages
Abdelhafidh Belalia 550b0c0ed1 TextLayout: take full width if text wrapped (#47435)
Summary:
### Problem
The calculated width for a multiline text is based on the longest line. However it does not account for text that wraps.

**Example** if numberOfLines=1 and the text wraps

```
+---------------------------+
This is a long text that
will wrap
+---------------------------+
```

The TextView will render
```
+---------------------------+
This is a long text t...
+---------------------------+
```

because the `calculatedWidth` took the width of the first line.

Also see https://github.com/facebook/react-native/pull/41770#issuecomment-2453611554 for additional context.

### Solution

If the text wraps, take the whole width.

```
+---------------------------+
This is a long text that w...
+---------------------------+
```

Fixes https://github.com/facebook/react-native/issues/39722
Fixes https://github.com/facebook/yoga/issues/1730

## Changelog:

[GENERAL] [FIXED] - Fix text not taking full width

Pull Request resolved: https://github.com/facebook/react-native/pull/47435

Test Plan:
```tsx
        <Text
          numberOfLines={1}
          style={{
            backgroundColor: 'red',
            alignSelf: 'flex-start',
            color: 'white',
            fontSize: 34,
          }}>
          {'This is a long text that will wrap.'}
        </Text>
        <Text
          numberOfLines={3}
          style={{
            backgroundColor: 'red',
            alignSelf: 'flex-start',
            color: 'white',
            fontSize: 34,
          }}>
          {
            '1\n\ntest\nThis is a long text that will wrap.This is a long text that will wrap.This is a long text that will wrap.\n'
          }
        </Text>
        <Text
          numberOfLines={3}
          style={{
            backgroundColor: 'red',
            alignSelf: 'flex-start',
            color: 'white',
            fontSize: 34,
          }}>
          {
            '1\n\nThis is a long text that will wrap.This is a long text that will wrap.This is a long text that will wrap.\n'
          }
        </Text>
```
1. Verify that the first and third text take full width
2. Verify that the second text does not take full width

| Before | After |
|:------:|:-----:|
|   <img width="480" alt="Screenshot 2024-11-05 at 9 00 24 PM" src="https://github.com/user-attachments/assets/b8d765c0-f4b1-42c6-afc7-75862c52612a">     |    <img width="480" alt="Screenshot 2024-11-05 at 9 01 49 PM" src="https://github.com/user-attachments/assets/f1534c14-a56a-4d44-8edc-4d9f75166cb2">   |

Reviewed By: NickGerleman

Differential Revision: D65521732

Pulled By: realsoelynn

fbshipit-source-id: 0bb0bb306445e73e8b24ff4c02921739d15ee07e
2024-11-21 03:16:30 -08:00
..
assets replace '$TEMPORARY$string<>' with 'string' (#46876) 2024-10-07 19:18:14 -07:00
babel-plugin-codegen RN: Fix lint/sort-imports Errors (#47109) 2024-10-18 04:07:02 -07:00
community-cli-plugin fix/cli start (#47450) 2024-11-06 07:26:30 -08:00
core-cli-utils Bump main to 0.77 2024-09-12 09:23:23 -07:00
debugger-frontend Update debugger-frontend from ff343d8...b61aae3 (#47593) 2024-11-13 11:27:38 -08:00
dev-middleware dev-middleware: Only rewrite hostnames if they match device connection hosts (#47685) 2024-11-20 06:56:21 -08:00
eslint-config-react-native Revert to @babel/eslint-parser in eslint-config (#47333) 2024-11-01 05:31:37 -07:00
eslint-plugin-react-native Update hermes-parser and related packages in fbsource to 0.24.0 (#46802) 2024-10-03 08:20:16 -07:00
eslint-plugin-specs Update hermes-parser and related packages in fbsource to 0.24.0 (#46802) 2024-10-03 08:20:16 -07:00
gradle-plugin RNGP - Do not attempt to substring to 1024 while logging. (#47509) 2024-11-08 04:57:15 -08:00
helloworld update targetSdk to 35 (#47738) 2024-11-20 11:06:31 -08:00
hermes-inspector-msggen Bump main to 0.77 2024-09-12 09:23:23 -07:00
metro-config metro-config: Revert setting hermesParser: true in default Metro config (#47670) 2024-11-19 02:43:03 -08:00
normalize-color Bump main to 0.77 2024-09-12 09:23:23 -07:00
polyfills Implement always available js error handling (#47466) 2024-11-07 11:22:57 -08:00
react-native TextLayout: take full width if text wrapped (#47435) 2024-11-21 03:16:30 -08:00
react-native-babel-preset Update hermes-parser and related packages in fbsource to 0.24.0 (#46802) 2024-10-03 08:20:16 -07:00
react-native-babel-transformer Update hermes-parser and related packages in fbsource to 0.24.0 (#46802) 2024-10-03 08:20:16 -07:00
react-native-bots fix: mitigate DangerJS transpilation bug (#47192) 2024-10-24 20:07:20 -07:00
react-native-codegen Fix generated EventEmitter code for nested objects in arrays. (#47514) 2024-11-20 16:25:34 -08:00
react-native-codegen-typescript-test Bump main to 0.77 2024-09-12 09:23:23 -07:00
react-native-info Bump main to 0.77 2024-09-12 09:23:23 -07:00
react-native-popup-menu-android Convert com.facebook.react.ViewManagerOnDemandReactPackage to Kotlin (#47682) 2024-11-20 04:05:26 -08:00
react-native-test-library Let lib maintainer be explicit with componentProvider mapping (#47520) 2024-11-12 07:38:03 -08:00
react-native-test-renderer Refactor Performance and PerformanceObserver internals (#46693) 2024-10-02 03:13:24 -07:00
rn-tester Bump Podfile.lock for Folly (#47868) 2024-11-21 02:33:29 -08:00
typescript-config Bump main to 0.77 2024-09-12 09:23:23 -07:00
virtualized-lists turn on by default optimization for memoization of virtualized list cells (#47724) 2024-11-20 16:00:03 -08:00