5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
7
* @format
8
+ * @flow
8
9
*/
9
10
10
11
'use strict' ;
@@ -21,9 +22,16 @@ import {
21
22
requireNativeComponent
22
23
} from 'react-native' ;
23
24
25
+ import invariant from 'fbjs/lib/invariant' ;
24
26
import keyMirror from 'fbjs/lib/keyMirror' ;
25
27
26
28
import WebViewShared from './WebViewShared' ;
29
+ import type {
30
+ WebViewErrorEvent ,
31
+ WebViewEvent ,
32
+ WebViewSharedProps ,
33
+ WebViewSource ,
34
+ } from './WebViewTypes' ;
27
35
28
36
const resolveAssetSource = Image . resolveAssetSource ;
29
37
@@ -41,10 +49,52 @@ const defaultRenderLoading = () => (
41
49
</ View >
42
50
) ;
43
51
52
+ type State = { |
53
+ viewState : WebViewState ,
54
+ lastErrorEvent : ?WebViewErrorEvent ,
55
+ startInLoadingState : boolean ,
56
+ | } ;
57
+
58
+ type WebViewPropsAndroid = $ReadOnly < { |
59
+ ...WebViewSharedProps ,
60
+ onNavigationStateChange ?: ( event : WebViewEvent ) => any ,
61
+ onContentSizeChange ?: ( event : WebViewEvent ) => any ,
62
+
63
+ /**
64
+ * Sets whether Geolocation is enabled. The default is false.
65
+ * @platform android
66
+ */
67
+ geolocationEnabled ?: ?boolean ,
68
+
69
+ /**
70
+ * Boolean that sets whether JavaScript running in the context of a file
71
+ * scheme URL should be allowed to access content from any origin.
72
+ * Including accessing content from other file scheme URLs
73
+ * @platform android
74
+ */
75
+ allowUniversalAccessFromFileURLs ?: ?boolean ,
76
+
77
+ /**
78
+ * Used on Android only, controls whether form autocomplete data should be saved
79
+ * @platform android
80
+ */
81
+ saveFormDataDisabled ?: ?boolean ,
82
+
83
+ /*
84
+ * Used on Android only, controls whether the given list of URL prefixes should
85
+ * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
86
+ * default activity intent for those URL instead of loading it within the webview.
87
+ * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
88
+ * @platform android
89
+ */
90
+ urlPrefixesForDefaultIntent ?: $ReadOnlyArray < string > ,
91
+
92
+ | } > ;
93
+
44
94
/**
45
95
* Renders a native WebView.
46
96
*/
47
- class WebView extends React . Component {
97
+ class WebView extends React . Component < WebViewPropsAndroid , State > {
48
98
static defaultProps = {
49
99
javaScriptEnabled : true ,
50
100
thirdPartyCookiesEnabled : true ,
@@ -55,7 +105,7 @@ class WebView extends React.Component {
55
105
56
106
state = {
57
107
viewState : WebViewState . IDLE ,
58
- lastErrorEvent : null ,
108
+ lastErrorEvent : ( null : ? WebViewErrorEvent ) ,
59
109
startInLoadingState : true ,
60
110
} ;
61
111
@@ -72,6 +122,7 @@ class WebView extends React.Component {
72
122
otherView = ( this . props . renderLoading || defaultRenderLoading ) ( ) ;
73
123
} else if ( this . state . viewState === WebViewState . ERROR ) {
74
124
const errorEvent = this . state . lastErrorEvent ;
125
+ invariant ( errorEvent != null , 'lastErrorEvent expected to be non-null' ) ;
75
126
otherView =
76
127
this . props . renderError &&
77
128
this . props . renderError (
@@ -81,7 +132,7 @@ class WebView extends React.Component {
81
132
) ;
82
133
} else if ( this . state . viewState !== WebViewState . IDLE ) {
83
134
console . error (
84
- 'RCTWebView invalid state encountered: ' + this . state . loading ,
135
+ 'RCTWebView invalid state encountered: ' + this . state . viewState ,
85
136
) ;
86
137
}
87
138
@@ -94,11 +145,11 @@ class WebView extends React.Component {
94
145
webViewStyles . push ( styles . hidden ) ;
95
146
}
96
147
97
- const source = this . props . source || { } ;
98
- if ( this . props . html ) {
99
- source . html = this . props . html ;
100
- } else if ( this . props . url ) {
101
- source . uri = this . props . url ;
148
+ let source = this . props . source || ( { } : WebViewSource ) ;
149
+ if ( ! this . props . source && this . props . html ) {
150
+ source = { html : this . props . html } ;
151
+ } else if ( ! this . props . source && this . props . url ) {
152
+ source = { uri : this . props . url } ;
102
153
}
103
154
104
155
if ( source . method === 'POST' && source . headers ) {
@@ -198,7 +249,7 @@ class WebView extends React.Component {
198
249
) ;
199
250
} ;
200
251
201
- postMessage = data => {
252
+ postMessage = ( data : string ) => {
202
253
UIManager . dispatchViewManagerCommand (
203
254
this . getWebViewHandle ( ) ,
204
255
UIManager . RCTWebView . Commands . postMessage ,
@@ -212,7 +263,7 @@ class WebView extends React.Component {
212
263
* on pages with a Content Security Policy that disallows eval(). If you need that
213
264
* functionality, look into postMessage/onMessage.
214
265
*/
215
- injectJavaScript = data => {
266
+ injectJavaScript = ( data : string ) => {
216
267
UIManager . dispatchViewManagerCommand (
217
268
this . getWebViewHandle ( ) ,
218
269
UIManager . RCTWebView . Commands . injectJavaScript ,
@@ -224,7 +275,7 @@ class WebView extends React.Component {
224
275
* We return an event with a bunch of fields including:
225
276
* url, title, loading, canGoBack, canGoForward
226
277
*/
227
- updateNavigationState = event => {
278
+ updateNavigationState = ( event : WebViewEvent ) => {
228
279
if ( this . props . onNavigationStateChange ) {
229
280
this . props . onNavigationStateChange ( event . nativeEvent ) ;
230
281
}
@@ -234,13 +285,13 @@ class WebView extends React.Component {
234
285
return ReactNative . findNodeHandle ( this . refs [ RCT_WEBVIEW_REF ] ) ;
235
286
} ;
236
287
237
- onLoadingStart = event => {
288
+ onLoadingStart = ( event : WebViewEvent ) => {
238
289
const onLoadStart = this . props . onLoadStart ;
239
290
onLoadStart && onLoadStart ( event ) ;
240
291
this . updateNavigationState ( event ) ;
241
292
} ;
242
293
243
- onLoadingError = event => {
294
+ onLoadingError = ( event : WebViewEvent ) => {
244
295
event . persist ( ) ; // persist this event because we need to store it
245
296
const { onError, onLoadEnd } = this . props ;
246
297
onError && onError ( event ) ;
@@ -253,7 +304,7 @@ class WebView extends React.Component {
253
304
} ) ;
254
305
} ;
255
306
256
- onLoadingFinish = event => {
307
+ onLoadingFinish = ( event : WebViewEvent ) => {
257
308
const { onLoad, onLoadEnd } = this . props ;
258
309
onLoad && onLoad ( event ) ;
259
310
onLoadEnd && onLoadEnd ( event ) ;
@@ -263,7 +314,7 @@ class WebView extends React.Component {
263
314
this . updateNavigationState ( event ) ;
264
315
} ;
265
316
266
- onMessage = ( event ) => {
317
+ onMessage = ( event : WebViewEvent ) => {
267
318
const { onMessage } = this . props ;
268
319
onMessage && onMessage ( event ) ;
269
320
} ;
0 commit comments