fix TextInput dataDetectorTypes (#48952)

Summary:
Setting `dataDetectorTypes` has no effect. As I am new to the react native codebase, it seems like `dataDetectorTypes` has not implemented on new arch yet.

issue: https://github.com/facebook/react-native/issues/48951

## Changelog:

[IOS] [FIXED] - implement `dataDetectorTypes` in the same way as the old architecture

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

Test Plan: <img width="356" alt="image" src="https://github.com/user-attachments/assets/192a683c-f7b7-48ac-98cd-76866901f008" />

Reviewed By: javache

Differential Revision: D68715166

Pulled By: cipolleschi

fbshipit-source-id: 612119e7453da012e6f75e1fc3a22ddedcb569a4
This commit is contained in:
VidocqH
2025-01-28 05:07:26 -08:00
committed by Facebook GitHub Bot
parent 98b8f17811
commit 2ae45ec3ce
8 changed files with 63 additions and 0 deletions
@@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) BOOL caretHidden;
@property (nonatomic, assign) BOOL enablesReturnKeyAutomatically;
@property (nonatomic, assign) UITextFieldViewMode clearButtonMode;
@property (nonatomic, assign) UIDataDetectorTypes dataDetectorTypes;
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
@property (nonatomic, strong, nullable) NSString *inputAccessoryViewID;
@property (nonatomic, strong, nullable) NSString *inputAccessoryViewButtonLabel;
@@ -19,6 +19,10 @@
NSArray<UIBarButtonItemGroup *> *_initialValueTrailingBarButtonGroups;
}
// This should not be needed but internal build were failing without it.
// This variable is unused.
@synthesize dataDetectorTypes;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
@@ -194,6 +194,12 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
_backedTextInputView.editable = newTextInputProps.traits.editable;
}
if (newTextInputProps.multiline &&
newTextInputProps.traits.dataDetectorTypes != oldTextInputProps.traits.dataDetectorTypes) {
_backedTextInputView.dataDetectorTypes =
RCTUITextViewDataDetectorTypesFromStringVector(newTextInputProps.traits.dataDetectorTypes);
}
if (newTextInputProps.traits.enablesReturnKeyAutomatically !=
oldTextInputProps.traits.enablesReturnKeyAutomatically) {
_backedTextInputView.enablesReturnKeyAutomatically = newTextInputProps.traits.enablesReturnKeyAutomatically;
@@ -41,4 +41,6 @@ UITextInputPasswordRules *RCTUITextInputPasswordRulesFromString(const std::strin
UITextSmartInsertDeleteType RCTUITextSmartInsertDeleteTypeFromOptionalBool(std::optional<bool> smartInsertDelete);
UIDataDetectorTypes RCTUITextViewDataDetectorTypesFromStringVector(const std::vector<std::string> &dataDetectorTypes);
NS_ASSUME_NONNULL_END
@@ -269,3 +269,31 @@ UITextSmartInsertDeleteType RCTUITextSmartInsertDeleteTypeFromOptionalBool(std::
? (*smartInsertDelete ? UITextSmartInsertDeleteTypeYes : UITextSmartInsertDeleteTypeNo)
: UITextSmartInsertDeleteTypeDefault;
}
UIDataDetectorTypes RCTUITextViewDataDetectorTypesFromStringVector(const std::vector<std::string> &dataDetectorTypes)
{
static dispatch_once_t onceToken;
static NSDictionary<NSString *, NSNumber *> *dataDetectorTypesMap = nil;
dispatch_once(&onceToken, ^{
dataDetectorTypesMap = @{
@"link" : @(UIDataDetectorTypeLink),
@"phoneNumber" : @(UIDataDetectorTypePhoneNumber),
@"address" : @(UIDataDetectorTypeAddress),
@"calendarEvent" : @(UIDataDetectorTypeCalendarEvent),
@"trackingNumber" : @(UIDataDetectorTypeShipmentTrackingNumber),
@"flightNumber" : @(UIDataDetectorTypeFlightNumber),
@"lookupSuggestion" : @(UIDataDetectorTypeLookupSuggestion),
@"all" : @(UIDataDetectorTypeAll)
};
});
UIDataDetectorTypes ret = UIDataDetectorTypeNone;
for (const auto &dataType : dataDetectorTypes) {
NSNumber *val = dataDetectorTypesMap[RCTNSStringFromString(dataType)];
if (val) {
ret |= (UIDataDetectorTypes)val.unsignedIntValue;
}
}
return ret;
}
@@ -10,6 +10,7 @@
#include <react/renderer/components/textinput/basePrimitives.h>
#include <optional>
#include <string>
#include <vector>
namespace facebook::react {
@@ -192,6 +193,12 @@ class TextInputTraits final {
*/
bool selectTextOnFocus{false};
/*
* iOS-only
* Default value: `empty` (`null`).
*/
std::vector<std::string> dataDetectorTypes{};
/*
* iOS-only (inherently iOS-specific)
* Default value: `<empty string>` (default content type).
@@ -136,6 +136,13 @@ static TextInputTraits convertRawProp(
sourceTraits.smartInsertDelete,
defaultTraits.smartInsertDelete);
traits.dataDetectorTypes = convertRawProp(
context,
rawProps,
"dataDetectorTypes",
sourceTraits.dataDetectorTypes,
defaultTraits.dataDetectorTypes);
return traits;
}
@@ -633,6 +633,14 @@ const textInputExamples: Array<RNTesterModuleExample> = [
style={styles.multiline}
dataDetectorTypes="phoneNumber"
/>
<ExampleTextInput
dataDetectorTypes={['link', 'phoneNumber']}
defaultValue={
'link: http://reactnative.dev, photo number: 88888888'
}
multiline
editable={false}
/>
</View>
);
},