From 4870e1f06ad8649dfd73f3ba3a0f3fffc717eef6 Mon Sep 17 00:00:00 2001 From: wiscat Date: Thu, 22 Nov 2018 15:58:07 +0300 Subject: [PATCH] feat(WKWebview): [ios] Add 'pagingEnabled' property to the iOS WKWebview (#165) --- docs/Reference.md | 11 +++++++++++ ios/RNCWKWebView.h | 1 + ios/RNCWKWebView.m | 7 ++++--- ios/RNCWKWebViewManager.m | 1 + js/WebView.ios.js | 1 + js/WebViewTypes.js | 8 ++++++++ typings/index.d.ts | 8 ++++++++ 7 files changed, 34 insertions(+), 3 deletions(-) diff --git a/docs/Reference.md b/docs/Reference.md index 181fffe..fc4a5aa 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -46,6 +46,7 @@ This document lays out the current public properties and methods for the React N - [`allowsBackForwardNavigationGestures`](Reference.md#allowsbackforwardnavigationgestures) - [`allowFileAccess`](Reference.md#allowFileAccess) - [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled) +- [`pagingEnabled`](Reference.md#pagingEnabled) ## Methods Index @@ -517,6 +518,16 @@ Sets whether the WebView should disable saving form data. The default value is ` | ------- | -------- | -------- | | boolean | No | Android | +--- + +### `pagingEnabled` + +If the value of this property is true, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value is false. + +| Type | Required | Platform | +| ------- | -------- | -------- | +| boolean | No | iOS | + ## Methods ### `extraNativeComponentConfig()` diff --git a/ios/RNCWKWebView.h b/ios/RNCWKWebView.h index ea15bda..85239fd 100644 --- a/ios/RNCWKWebView.h +++ b/ios/RNCWKWebView.h @@ -26,6 +26,7 @@ shouldStartLoadForRequest:(NSMutableDictionary *)request @property (nonatomic, assign) BOOL messagingEnabled; @property (nonatomic, copy) NSString *injectedJavaScript; @property (nonatomic, assign) BOOL scrollEnabled; +@property (nonatomic, assign) BOOL pagingEnabled; @property (nonatomic, assign) CGFloat decelerationRate; @property (nonatomic, assign) BOOL allowsInlineMediaPlayback; @property (nonatomic, assign) BOOL bounces; diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index 7afe599..abd7d8d 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -70,7 +70,7 @@ static NSString *const MessageHanderName = @"ReactNative"; _automaticallyAdjustContentInsets = YES; _contentInset = UIEdgeInsetsZero; } - + // Workaround for a keyboard dismissal bug present in iOS 12 // https://openradar.appspot.com/radar?id=5018321736957952 if (@available(iOS 12.0, *)) { @@ -111,6 +111,7 @@ static NSString *const MessageHanderName = @"ReactNative"; _webView.UIDelegate = self; _webView.navigationDelegate = self; _webView.scrollView.scrollEnabled = _scrollEnabled; + _webView.scrollView.pagingEnabled = _pagingEnabled; _webView.scrollView.bounces = _bounces; [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil]; _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures; @@ -137,7 +138,7 @@ static NSString *const MessageHanderName = @"ReactNative"; [_webView removeFromSuperview]; _webView = nil; } - + [super removeFromSuperview]; } @@ -146,7 +147,7 @@ static NSString *const MessageHanderName = @"ReactNative"; keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false]; [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes]; } - + -(void)keyboardWillShow { if (keyboardTimer != nil) { diff --git a/ios/RNCWKWebViewManager.m b/ios/RNCWKWebViewManager.m index 2a1ce3a..6e5b36d 100644 --- a/ios/RNCWKWebViewManager.m +++ b/ios/RNCWKWebViewManager.m @@ -45,6 +45,7 @@ RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets) RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL) RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL) RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL) +RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL) RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString) /** diff --git a/js/WebView.ios.js b/js/WebView.ios.js index e442344..f8d62b9 100644 --- a/js/WebView.ios.js +++ b/js/WebView.ios.js @@ -269,6 +269,7 @@ class WebView extends React.Component { injectedJavaScript={this.props.injectedJavaScript} bounces={this.props.bounces} scrollEnabled={this.props.scrollEnabled} + pagingEnabled={this.props.pagingEnabled} decelerationRate={decelerationRate} contentInset={this.props.contentInset} automaticallyAdjustContentInsets={ diff --git a/js/WebViewTypes.js b/js/WebViewTypes.js index a7bdbd7..bb2dc92 100644 --- a/js/WebViewTypes.js +++ b/js/WebViewTypes.js @@ -168,6 +168,14 @@ export type IOSWebViewProps = $ReadOnly<{| */ scrollEnabled?: ?boolean, + /** + * If the value of this property is true, the scroll view stops on multiples + * of the scroll view’s bounds when the user scrolls. + * The default value is false. + * @platform ios + */ + pagingEnabled?: ?boolean, + /** * The amount by which the web view content is inset from the edges of * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}. diff --git a/typings/index.d.ts b/typings/index.d.ts index 5f7f326..c599acb 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -145,6 +145,14 @@ export interface IOSWebViewProps { */ scrollEnabled?: boolean; + /** + * If the value of this property is true, the scroll view stops on multiples + * of the scroll view’s bounds when the user scrolls. + * The default value is false. + * @platform ios + */ + pagingEnabled?: boolean, + /** * The amount by which the web view content is inset from the edges of * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.