Skip to content

Commit 1af17f1

Browse files
RSNarafacebook-github-bot
authored andcommitted
Implement 'dataDetectorTypes' prop
Summary: When text is rendered in `WKWebView` WebKit component, the component itself can detect things like phone numbers, flight numbers, links, etc. and render them with additional functionality. For example, when the text `apple.com` is detected, if the `link` data detector type is enabled, the web view will actually render a link that takes the user to the Apple home page. In this diff, I implement the `dataDetectorTypes` prop. The data detector types supported are: 1. phoneNumber 1. link 1. address 1. calendarEvent 1. trackingNumber 1. flightNumber 1. lookupSuggestion These enums are documented in the [[ https://developer.apple.com/documentation/webkit/wkdatadetectortypes | WKDataDetectorTypes docs ]]. Reviewed By: shergin Differential Revision: D6392546 fbshipit-source-id: 4dd373f0ac52f898163cd959eeef6672e55b42a6
1 parent 7217630 commit 1af17f1

File tree

7 files changed

+23
-3
lines changed

7 files changed

+23
-3
lines changed

Libraries/Components/WKWebView/WKWebView.ios.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ const RCTWKWebView = requireNativeComponent('RCTWKWebView');
1818
type RCTWKWebViewProps = {
1919
allowsInlineMediaPlayback?: boolean,
2020
mediaPlaybackRequiresUserAction?: boolean,
21+
dataDetectorTypes?: boolean,
2122
};
2223

2324
class WKWebView extends React.Component<RCTWKWebViewProps> {
2425
componentWillReceiveProps(nextProps: RCTWKWebViewProps) {
2526
this.showRedboxOnPropChanges(nextProps, 'allowsInlineMediaPlayback');
2627
this.showRedboxOnPropChanges(nextProps, 'mediaPlaybackRequiresUserAction');
28+
this.showRedboxOnPropChanges(nextProps, 'dataDetectorTypes');
2729
}
2830

2931
showRedboxOnPropChanges(nextProps: RCTWKWebViewProps, propName: string) {

React/Base/RCTConvert.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import <React/RCTPointerEvents.h>
1616
#import <React/RCTTextDecorationLineType.h>
1717
#import <yoga/Yoga.h>
18+
#import <WebKit/WebKit.h>
1819

1920
/**
2021
* This class provides a collection of conversion functions for mapping
@@ -68,6 +69,7 @@ typedef NSURL RCTFileURL;
6869
+ (UIReturnKeyType)UIReturnKeyType:(id)json;
6970
#if !TARGET_OS_TV
7071
+ (UIDataDetectorTypes)UIDataDetectorTypes:(id)json;
72+
+ (WKDataDetectorTypes)WKDataDetectorTypes:(id)json;
7173
#endif
7274

7375
+ (UIViewContentMode)UIViewContentMode:(id)json;

React/Base/RCTConvert.m

+12
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,18 @@ + (NSLocale *)NSLocale:(id)json
361361
@"none": @(UIDataDetectorTypeNone),
362362
@"all": @(UIDataDetectorTypeAll),
363363
}), UIDataDetectorTypePhoneNumber, unsignedLongLongValue)
364+
365+
RCT_MULTI_ENUM_CONVERTER(WKDataDetectorTypes, (@{
366+
@"phoneNumber": @(WKDataDetectorTypePhoneNumber),
367+
@"link": @(WKDataDetectorTypeLink),
368+
@"address": @(WKDataDetectorTypeAddress),
369+
@"calendarEvent": @(WKDataDetectorTypeCalendarEvent),
370+
@"trackingNumber": @(WKDataDetectorTypeTrackingNumber),
371+
@"flightNumber": @(WKDataDetectorTypeFlightNumber),
372+
@"lookupSuggestion": @(WKDataDetectorTypeLookupSuggestion),
373+
@"none": @(WKDataDetectorTypeNone),
374+
@"all": @(WKDataDetectorTypeAll),
375+
}), WKDataDetectorTypePhoneNumber, unsignedLongLongValue)
364376
#endif
365377

366378
RCT_ENUM_CONVERTER(UIKeyboardAppearance, (@{

React/Views/RCTWKWebView.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#import <React/RCTView.h>
11+
#import <WebKit/WebKit.h>
1112

1213
@class RCTWKWebView;
1314

@@ -30,6 +31,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
3031
@property (nonatomic, assign) BOOL allowsInlineMediaPlayback;
3132
@property (nonatomic, assign) BOOL bounces;
3233
@property (nonatomic, assign) BOOL mediaPlaybackRequiresUserAction;
34+
@property (nonatomic, assign) WKDataDetectorTypes dataDetectorTypes;
3335

3436
- (void)postMessage:(NSString *)message;
3537
- (void)injectJavaScript:(NSString *)script;

React/Views/RCTWKWebView.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#import "RCTWKWebView.h"
2-
#import <WebKit/WebKit.h>
32
#import <React/RCTConvert.h>
43
#import "RCTAutoInsetsProtocol.h"
54

@@ -43,6 +42,7 @@ - (void)didMoveToWindow
4342
wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
4443
? WKAudiovisualMediaTypeAll
4544
: WKAudiovisualMediaTypeNone;
45+
wkWebViewConfig.dataDetectorTypes = _dataDetectorTypes;
4646

4747
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
4848
_webView.scrollView.delegate = self;

React/Views/RCTWKWebViewManager.m

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ - (UIView *)view
2929
RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
3030
RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
3131
RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
32+
RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
33+
3234
/**
3335
* Expose methods to enable messaging the webview.
3436
*/

React/Views/RCTWebViewManager.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ - (UIView *)view
6363

6464
RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
6565
{
66-
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
67-
id view = viewRegistry[reactTag];
66+
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWebView *> *viewRegistry) {
67+
RCTWebView *view = viewRegistry[reactTag];
6868
if (![view isKindOfClass:[RCTWebView class]]) {
6969
RCTLogError(@"Invalid view returned from registry, expecting RCTWebView, got: %@", view);
7070
} else {

0 commit comments

Comments
 (0)