From fe941a8f4c92fa9b1f07d2dd94050c81d63c77e8 Mon Sep 17 00:00:00 2001 From: shubhamguptadream11 Date: Fri, 23 Aug 2024 11:13:33 -0700 Subject: [PATCH] feat(iOS): line break mode prop iOS updates to consume new cpp functions (#46129) Summary: Solves this issue: https://github.com/facebook/react-native/issues/44107 ## Changelog: [IOS] [ADDED] - Line break mode for TextInput components. **This includes iOS updates to consume new cpp functions.** This PR is a breakdown of [this](https://github.com/facebook/react-native/pull/45968) PR. Pull Request resolved: https://github.com/facebook/react-native/pull/46129 Test Plan: - Tested builds in new and old architecture mode. Reviewed By: andrewdacenko Differential Revision: D61656969 Pulled By: cipolleschi fbshipit-source-id: 4c6ed983ad15841ce52443bba13962d45c04e756 --- .../Text/BaseText/RCTBaseTextViewManager.mm | 1 + .../attributedstring/TextAttributes.cpp | 4 ++ .../renderer/attributedstring/conversions.h | 54 +++++++++++++++++++ .../components/text/BaseTextProps.cpp | 8 +++ 4 files changed, 67 insertions(+) diff --git a/packages/react-native/Libraries/Text/BaseText/RCTBaseTextViewManager.mm b/packages/react-native/Libraries/Text/BaseText/RCTBaseTextViewManager.mm index ece68768c40251..e9ce272b5f55fb 100644 --- a/packages/react-native/Libraries/Text/BaseText/RCTBaseTextViewManager.mm +++ b/packages/react-native/Libraries/Text/BaseText/RCTBaseTextViewManager.mm @@ -44,6 +44,7 @@ - (RCTShadowView *)shadowView RCT_REMAP_SHADOW_PROPERTY(textAlign, textAttributes.alignment, NSTextAlignment) RCT_REMAP_SHADOW_PROPERTY(writingDirection, textAttributes.baseWritingDirection, NSWritingDirection) RCT_REMAP_SHADOW_PROPERTY(lineBreakStrategyIOS, textAttributes.lineBreakStrategy, NSLineBreakStrategy) +RCT_REMAP_SHADOW_PROPERTY(lineBreakModeIOS, textAttributes.lineBreakMode, NSLineBreakMode) // Decoration RCT_REMAP_SHADOW_PROPERTY(textDecorationColor, textAttributes.textDecorationColor, UIColor) RCT_REMAP_SHADOW_PROPERTY(textDecorationStyle, textAttributes.textDecorationStyle, NSUnderlineStyle) diff --git a/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp b/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp index cd32048480f540..9b06f12948a654 100644 --- a/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp +++ b/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp @@ -68,6 +68,9 @@ void TextAttributes::apply(TextAttributes textAttributes) { lineBreakStrategy = textAttributes.lineBreakStrategy.has_value() ? textAttributes.lineBreakStrategy : lineBreakStrategy; + lineBreakMode = textAttributes.lineBreakMode.has_value() + ? textAttributes.lineBreakMode + : lineBreakMode; // Decoration textDecorationColor = textAttributes.textDecorationColor @@ -216,6 +219,7 @@ SharedDebugStringConvertibleList TextAttributes::getDebugProps() const { debugStringConvertibleItem("alignment", alignment), debugStringConvertibleItem("baseWritingDirection", baseWritingDirection), debugStringConvertibleItem("lineBreakStrategyIOS", lineBreakStrategy), + debugStringConvertibleItem("lineBreakModeIOS", lineBreakMode), // Decoration debugStringConvertibleItem("textDecorationColor", textDecorationColor), diff --git a/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h b/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h index d2a40f49765304..8e365c52523f9e 100644 --- a/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h @@ -589,6 +589,60 @@ inline std::string toString(const LineBreakStrategy& lineBreakStrategy) { return "none"; } +inline void fromRawValue( + const PropsParserContext& /*context*/, + const RawValue& value, + LineBreakMode& result) { + react_native_expect(value.hasType()); + if (value.hasType()) { + auto string = (std::string)value; + if (string == "wordWrapping") { + result = LineBreakMode::Word; + } else if (string == "char") { + result = LineBreakMode::Char; + } else if (string == "clip") { + result = LineBreakMode::Clip; + } else if (string == "head") { + result = LineBreakMode::Head; + } else if (string == "middle") { + result = LineBreakMode::Middle; + } else if (string == "tail") { + result = LineBreakMode::Tail; + } else { + LOG(ERROR) << "Unsupported LineBreakStrategy value: " << string; + react_native_expect(false); + // sane default for prod + result = LineBreakMode::Word; + } + return; + } + + LOG(ERROR) << "Unsupported LineBreakStrategy type"; + // sane default for prod + result = LineBreakMode::Word; +} + +inline std::string toString(const LineBreakMode& lineBreakMode) { + switch (lineBreakMode) { + case LineBreakMode::Word: + return "wordWrapping"; + case LineBreakMode::Char: + return "char"; + case LineBreakMode::Clip: + return "clip"; + case LineBreakMode::Head: + return "head"; + case LineBreakMode::Middle: + return "middle"; + case LineBreakMode::Tail: + return "tail"; + } + + LOG(ERROR) << "Unsupported LineBreakStrategy value"; + // sane default for prod + return "wordWrapping"; +} + inline void fromRawValue( const PropsParserContext& context, const RawValue& value, diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp index 2dfe49020d2eec..e5fd9513461417 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp @@ -123,6 +123,12 @@ static TextAttributes convertRawProp( "lineBreakStrategyIOS", sourceTextAttributes.lineBreakStrategy, defaultTextAttributes.lineBreakStrategy); + textAttributes.lineBreakMode = convertRawProp( + context, + rawProps, + "lineBreakModeIOS", + sourceTextAttributes.lineBreakMode, + defaultTextAttributes.lineBreakMode); // Decoration textAttributes.textDecorationColor = convertRawProp( @@ -286,6 +292,8 @@ void BaseTextProps::setProp( textAttributes, lineBreakStrategy, "lineBreakStrategyIOS"); + REBUILD_FIELD_SWITCH_CASE( + defaults, value, textAttributes, lineBreakMode, "lineBreakModeIOS"); REBUILD_FIELD_SWITCH_CASE( defaults, value,