Browse Source

feat(android): fix overflow issues and match iOS default render error and loading behaviour

Thibault Malbranche 5 years ago
parent
commit
f6c821e9ea
4 changed files with 45 additions and 35 deletions
  1. 9
    11
      src/WebView.android.tsx
  2. 2
    20
      src/WebView.ios.tsx
  3. 5
    3
      src/WebView.styles.ts
  4. 29
    1
      src/WebViewShared.tsx

+ 9
- 11
src/WebView.android.tsx View File

@@ -1,7 +1,6 @@
1 1
 import React from 'react';
2 2
 
3 3
 import {
4
-  ActivityIndicator,
5 4
   Image,
6 5
   requireNativeComponent,
7 6
   UIManager as NotTypedUIManager,
@@ -17,6 +16,8 @@ import {
17 16
   defaultOriginWhitelist,
18 17
   createOnShouldStartLoadWithRequest,
19 18
   getViewManagerConfig,
19
+  defaultRenderError,
20
+  defaultRenderLoading,
20 21
 } from './WebViewShared';
21 22
 import {
22 23
   WebViewErrorEvent,
@@ -38,12 +39,6 @@ const RNCWebView = requireNativeComponent(
38 39
 ) as typeof NativeWebViewAndroid;
39 40
 const { resolveAssetSource } = Image;
40 41
 
41
-const defaultRenderLoading = () => (
42
-  <View style={styles.loadingView}>
43
-    <ActivityIndicator style={styles.loadingProgressBar} />
44
-  </View>
45
-);
46
-
47 42
 /**
48 43
  * Renders a native WebView.
49 44
  */
@@ -228,6 +223,7 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
228 223
       nativeConfig = {},
229 224
       ...otherProps
230 225
     } = this.props;
226
+
231 227
     let otherView = null;
232 228
 
233 229
     if (this.state.viewState === 'LOADING') {
@@ -235,16 +231,18 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
235 231
     } else if (this.state.viewState === 'ERROR') {
236 232
       const errorEvent = this.state.lastErrorEvent;
237 233
       invariant(errorEvent != null, 'lastErrorEvent expected to be non-null');
238
-      otherView
239
-        = renderError
240
-        && renderError(errorEvent.domain, errorEvent.code, errorEvent.description);
234
+      otherView = (renderError || defaultRenderError)(
235
+        errorEvent.domain,
236
+        errorEvent.code,
237
+        errorEvent.description,
238
+      );
241 239
     } else if (this.state.viewState !== 'IDLE') {
242 240
       console.error(
243 241
         `RNCWebView invalid state encountered: ${this.state.viewState}`,
244 242
       );
245 243
     }
246 244
 
247
-    const webViewStyles = [styles.container, style];
245
+    const webViewStyles = [styles.container, styles.webView, style];
248 246
     if (
249 247
       this.state.viewState === 'LOADING'
250 248
       || this.state.viewState === 'ERROR'

+ 2
- 20
src/WebView.ios.tsx View File

@@ -1,7 +1,5 @@
1 1
 import React from 'react';
2 2
 import {
3
-  ActivityIndicator,
4
-  Text,
5 3
   UIManager as NotTypedUIManager,
6 4
   View,
7 5
   requireNativeComponent,
@@ -16,6 +14,8 @@ import {
16 14
   defaultOriginWhitelist,
17 15
   createOnShouldStartLoadWithRequest,
18 16
   getViewManagerConfig,
17
+  defaultRenderError,
18
+  defaultRenderLoading,
19 19
 } from './WebViewShared';
20 20
 import {
21 21
   WebViewErrorEvent,
@@ -60,24 +60,6 @@ const RNCWKWebView: typeof NativeWebViewIOS = requireNativeComponent(
60 60
   'RNCWKWebView',
61 61
 );
62 62
 
63
-const defaultRenderLoading = () => (
64
-  <View style={styles.loadingView}>
65
-    <ActivityIndicator />
66
-  </View>
67
-);
68
-const defaultRenderError = (
69
-  errorDomain: string | undefined,
70
-  errorCode: number,
71
-  errorDesc: string,
72
-) => (
73
-  <View style={styles.errorContainer}>
74
-    <Text style={styles.errorTextTitle}>Error loading page</Text>
75
-    <Text style={styles.errorText}>{`Domain: ${errorDomain}`}</Text>
76
-    <Text style={styles.errorText}>{`Error Code: ${errorCode}`}</Text>
77
-    <Text style={styles.errorText}>{`Description: ${errorDesc}`}</Text>
78
-  </View>
79
-);
80
-
81 63
 class WebView extends React.Component<IOSWebViewProps, State> {
82 64
   static defaultProps = {
83 65
     useWebKit: true,

+ 5
- 3
src/WebView.styles.ts View File

@@ -11,26 +11,28 @@ interface Styles {
11 11
   loadingProgressBar: ViewStyle;
12 12
 }
13 13
 
14
-const BGWASH = 'rgba(255,255,255,0.8)';
15
-
16 14
 const styles = StyleSheet.create<Styles>({
17 15
   container: {
18 16
     flex: 1,
17
+    overflow: 'hidden',
18
+    backgroundColor: 'white',
19 19
   },
20 20
   errorContainer: {
21 21
     flex: 1,
22 22
     justifyContent: 'center',
23 23
     alignItems: 'center',
24
-    backgroundColor: BGWASH,
24
+    backgroundColor: 'white',
25 25
   },
26 26
   hidden: {
27 27
     height: 0,
28 28
     flex: 0, // disable 'flex:1' when hiding a View
29
+    display: 'none',
29 30
   },
30 31
   loadingView: {
31 32
     flex: 1,
32 33
     justifyContent: 'center',
33 34
     alignItems: 'center',
35
+    backgroundColor: 'white',
34 36
   },
35 37
   loadingProgressBar: {
36 38
     height: 20,

src/WebViewShared.ts → src/WebViewShared.tsx View File

@@ -1,10 +1,18 @@
1 1
 import escapeStringRegexp from 'escape-string-regexp';
2
-import { Linking, UIManager as NotTypedUIManager } from 'react-native';
2
+import React from 'react';
3
+import {
4
+  Linking,
5
+  UIManager as NotTypedUIManager,
6
+  View,
7
+  ActivityIndicator,
8
+  Text,
9
+} from 'react-native';
3 10
 import {
4 11
   WebViewNavigationEvent,
5 12
   OnShouldStartLoadWithRequest,
6 13
   CustomUIManager,
7 14
 } from './WebViewTypes';
15
+import styles from './WebView.styles';
8 16
 
9 17
 const UIManager = NotTypedUIManager as CustomUIManager;
10 18
 
@@ -66,8 +74,28 @@ const getViewManagerConfig = (
66 74
   return UIManager.getViewManagerConfig(viewManagerName);
67 75
 };
68 76
 
77
+const defaultRenderLoading = () => (
78
+  <View style={styles.loadingView}>
79
+    <ActivityIndicator />
80
+  </View>
81
+);
82
+const defaultRenderError = (
83
+  errorDomain: string | undefined,
84
+  errorCode: number,
85
+  errorDesc: string,
86
+) => (
87
+  <View style={styles.errorContainer}>
88
+    <Text style={styles.errorTextTitle}>Error loading page</Text>
89
+    <Text style={styles.errorText}>{`Domain: ${errorDomain}`}</Text>
90
+    <Text style={styles.errorText}>{`Error Code: ${errorCode}`}</Text>
91
+    <Text style={styles.errorText}>{`Description: ${errorDesc}`}</Text>
92
+  </View>
93
+);
94
+
69 95
 export {
70 96
   defaultOriginWhitelist,
71 97
   createOnShouldStartLoadWithRequest,
72 98
   getViewManagerConfig,
99
+  defaultRenderLoading,
100
+  defaultRenderError,
73 101
 };