iou90 7 лет назад
Родитель
Сommit
5e367166e7
1 измененных файлов: 34 добавлений и 9 удалений
  1. 34
    9
      autoHeightWebView/index.android.js

+ 34
- 9
autoHeightWebView/index.android.js Просмотреть файл

@@ -11,10 +11,14 @@ import {
11 11
     WebView
12 12
 } from 'react-native';
13 13
 
14
+const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView);
15
+
14 16
 export default class AutoHeightWebView extends Component {
15 17
     constructor(props) {
16 18
         super(props);
17
-        this.handleNavigationStateChange = this.handleNavigationStateChange.bind(this);
19
+        this.onLoadingStart = this.onLoadingStart.bind(this);
20
+        this.onLoadingFinish = this.onLoadingStart.bind(this);
21
+        this.handleNavigationStateChanged = this.handleNavigationStateChanged.bind(this);
18 22
         const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
19 23
         this.state = {
20 24
             height: 0,
@@ -30,6 +34,18 @@ export default class AutoHeightWebView extends Component {
30 34
         this.setState({ script: currentScript });
31 35
     }
32 36
 
37
+    onLoadingStart(event) {
38
+        this.updateNavigationState(event);
39
+    }
40
+
41
+    onLoadingFinish(event) {
42
+        this.updateNavigationState(event);
43
+    }
44
+
45
+    updateNavigationState(event) {
46
+        this.handleNavigationStateChanged(event.nativeEvent);
47
+    }
48
+
33 49
     appendFilesToHead(files, script) {
34 50
         if (!files) {
35 51
             return script;
@@ -47,11 +63,13 @@ export default class AutoHeightWebView extends Component {
47 63
         return script;
48 64
     }
49 65
 
50
-    handleNavigationStateChange(navState) {
66
+    handleNavigationStateChanged(navState) {
51 67
         const height = Number(navState.title);
52
-        this.setState({ height });
53
-        if (this.props.onHeightUpdated) {
54
-            this.props.onHeightUpdated(height);
68
+        if (height) {
69
+            this.setState({ height });
70
+            if (this.props.onHeightUpdated) {
71
+                this.props.onHeightUpdated(height);
72
+            }
55 73
         }
56 74
     }
57 75
 
@@ -60,22 +78,26 @@ export default class AutoHeightWebView extends Component {
60 78
             html: this.props.html,
61 79
             baseUrl: 'file:///android_asset/web/'
62 80
         } : { html: this.props.html };
81
+        console.log(this.state.height + this.props.heightOffset);
63 82
         return (
64 83
             <View style={[{
65 84
                 height: this.state.height + this.props.heightOffset
66 85
             }, this.props.style]}>
67 86
                 <RCTAutoHeightWebView
87
+                    style={{ flex: 1 }}
88
+                    javaScriptEnabled={true}
68 89
                     injectedJavaScript={this.state.script + this.props.customScript}
69 90
                     scrollEnabled={false}
70 91
                     source={source}
71
-                    onNavigationStateChange={this.handleNavigationStateChange} />
92
+                    onLoadingStart={this.onLoadingStart}
93
+                    onLoadingFinish={this.onLoadingFinish} />
72 94
             </View>
73 95
         );
74 96
     }
75 97
 }
76 98
 
77 99
 AutoHeightWebView.propTypes = {
78
-    ...View.propTypes,
100
+    ...WebView.propTypes,
79 101
     html: PropTypes.string,
80 102
     onHeightUpdated: PropTypes.func,
81 103
     customScript: PropTypes.string,
@@ -83,7 +105,7 @@ AutoHeightWebView.propTypes = {
83 105
     heightOffset: PropTypes.number,
84 106
     // baseUrl not work in android 4.3 or below version
85 107
     enableBaseUrl: PropTypes.bool,
86
-    // add web/files... to android/app/src/assets/
108
+    // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
87 109
     files: PropTypes.arrayOf(PropTypes.shape({
88 110
         href: PropTypes.string,
89 111
         type: PropTypes.string,
@@ -91,7 +113,10 @@ AutoHeightWebView.propTypes = {
91 113
     }))
92 114
 }
93 115
 
94
-const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView);
116
+AutoHeightWebView.defaultProps = {
117
+    enableBaseUrl: false,
118
+    heightOffset: 25
119
+}
95 120
 
96 121
 const BaseScript =
97 122
     `