Browse Source

remove baseUrl prop

iou90kant 5 years ago
parent
commit
50211735af
2 changed files with 17 additions and 18 deletions
  1. 14
    12
      autoHeightWebView/index.js
  2. 3
    6
      autoHeightWebView/utils.js

+ 14
- 12
autoHeightWebView/index.js View File

12
 
12
 
13
 const AutoHeightWebView = React.memo(
13
 const AutoHeightWebView = React.memo(
14
   forwardRef((props, ref) => {
14
   forwardRef((props, ref) => {
15
+    const { style, onMessage, onSizeUpdated, source } = props;
16
+
17
+    if (!source) {
18
+      return null;
19
+    }
20
+
15
     let webView = useRef();
21
     let webView = useRef();
16
     useImperativeHandle(ref, () => ({
22
     useImperativeHandle(ref, () => ({
17
       stopLoading: () => webView.current.stopLoading(),
23
       stopLoading: () => webView.current.stopLoading(),
21
       injectJavaScript: script => webView.current.injectJavaScript(script)
27
       injectJavaScript: script => webView.current.injectJavaScript(script)
22
     }));
28
     }));
23
 
29
 
24
-    const { style, onMessage, onSizeUpdated, source, baseUrl, files, customStyle, customScript, zoomable } = props;
25
-
26
-    const [size, setSize] = useState(() => ({
30
+    const [size, setSize] = useState({
27
       height: style && style.height ? style.height : 0,
31
       height: style && style.height ? style.height : 0,
28
       width: getWidth(style)
32
       width: getWidth(style)
29
-    }));
30
-    const hanldeMessage = event => {
33
+    });
34
+    const handleMessage = event => {
31
       onMessage && onMessage(event);
35
       onMessage && onMessage(event);
32
       if (!event.nativeEvent) {
36
       if (!event.nativeEvent) {
33
         return;
37
         return;
49
         });
53
         });
50
     };
54
     };
51
 
55
 
52
-    const { currentSource, script } = reduceData({ source, baseUrl, files, customStyle, customScript, zoomable });
56
+    const { currentSource, script } = reduceData(props);
53
 
57
 
54
     const { width, height } = size;
58
     const { width, height } = size;
55
     useEffect(
59
     useEffect(
66
       <WebView
70
       <WebView
67
         {...props}
71
         {...props}
68
         ref={webView}
72
         ref={webView}
69
-        onMessage={hanldeMessage}
73
+        onMessage={handleMessage}
70
         style={[
74
         style={[
71
           styles.webView,
75
           styles.webView,
72
           {
76
           {
85
 
89
 
86
 AutoHeightWebView.propTypes = {
90
 AutoHeightWebView.propTypes = {
87
   onSizeUpdated: PropTypes.func,
91
   onSizeUpdated: PropTypes.func,
88
-  // 'web/' by default on iOS
89
-  // 'file:///android_asset/web/' by default on Android
90
-  baseUrl: PropTypes.string,
91
   // add baseUrl/files... to android/app/src/assets/ on android
92
   // add baseUrl/files... to android/app/src/assets/ on android
92
   // add baseUrl/files... to project root on iOS
93
   // add baseUrl/files... to project root on iOS
93
   files: PropTypes.arrayOf(
94
   files: PropTypes.arrayOf(
104
   // webview props
105
   // webview props
105
   originWhitelist: PropTypes.arrayOf(PropTypes.string),
106
   originWhitelist: PropTypes.arrayOf(PropTypes.string),
106
   onMessage: PropTypes.func,
107
   onMessage: PropTypes.func,
108
+  // baseUrl now contained by source
109
+  // 'web/' by default on iOS
110
+  // 'file:///android_asset/web/' by default on Android
107
   source: PropTypes.object
111
   source: PropTypes.object
108
 };
112
 };
109
 
113
 
111
   showsVerticalScrollIndicator: false,
115
   showsVerticalScrollIndicator: false,
112
   showsHorizontalScrollIndicator: false,
116
   showsHorizontalScrollIndicator: false,
113
   originWhitelist: ['*'],
117
   originWhitelist: ['*'],
114
-  baseUrl: 'web/',
115
   zoomable: true
118
   zoomable: true
116
 };
119
 };
117
 
120
 
118
 Platform.OS === 'android' &&
121
 Platform.OS === 'android' &&
119
   Object.assign(defaultProps, {
122
   Object.assign(defaultProps, {
120
-    baseUrl: 'file:///android_asset/web/',
121
     // if set to true may cause some layout issues (width of container will be than width of screen) on android
123
     // if set to true may cause some layout issues (width of container will be than width of screen) on android
122
     scalesPageToFit: false
124
     scalesPageToFit: false
123
   });
125
   });

+ 3
- 6
autoHeightWebView/utils.js View File

117
 };
117
 };
118
 
118
 
119
 export const reduceData = props => {
119
 export const reduceData = props => {
120
-  const { source, baseUrl } = props;
120
+  const { source } = props;
121
   const script = getScript(props);
121
   const script = getScript(props);
122
-  let currentSource = baseUrl ? { baseUrl } : {};
123
   if (source.html) {
122
   if (source.html) {
124
-    Object.assign(currentSource, { html: getInjectedSource({ html: source.html, script }) });
125
-    return { currentSource };
123
+    return { currentSource: { html: getInjectedSource({ html: source.html, script }) } };
126
   } else {
124
   } else {
127
-    Object.assign(currentSource, source);
128
     return {
125
     return {
129
-      currentSource,
126
+      currentSource: source,
130
       script
127
       script
131
     };
128
     };
132
   }
129
   }