55 * LICENSE file in the root directory of this source tree.
66 *
77 * @format
8+ * @flow
89 */
910
1011'use strict' ;
@@ -21,9 +22,16 @@ import {
2122 requireNativeComponent
2223} from 'react-native' ;
2324
25+ import invariant from 'fbjs/lib/invariant' ;
2426import keyMirror from 'fbjs/lib/keyMirror' ;
2527
2628import WebViewShared from './WebViewShared' ;
29+ import type {
30+ WebViewErrorEvent ,
31+ WebViewEvent ,
32+ WebViewSharedProps ,
33+ WebViewSource ,
34+ } from './WebViewTypes' ;
2735
2836const resolveAssetSource = Image . resolveAssetSource ;
2937
@@ -41,10 +49,52 @@ const defaultRenderLoading = () => (
4149 </ View >
4250) ;
4351
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+
4494/**
4595 * Renders a native WebView.
4696 */
47- class WebView extends React . Component {
97+ class WebView extends React . Component < WebViewPropsAndroid , State > {
4898 static defaultProps = {
4999 javaScriptEnabled : true ,
50100 thirdPartyCookiesEnabled : true ,
@@ -55,7 +105,7 @@ class WebView extends React.Component {
55105
56106 state = {
57107 viewState : WebViewState . IDLE ,
58- lastErrorEvent : null ,
108+ lastErrorEvent : ( null : ? WebViewErrorEvent ) ,
59109 startInLoadingState : true ,
60110 } ;
61111
@@ -72,6 +122,7 @@ class WebView extends React.Component {
72122 otherView = ( this . props . renderLoading || defaultRenderLoading ) ( ) ;
73123 } else if ( this . state . viewState === WebViewState . ERROR ) {
74124 const errorEvent = this . state . lastErrorEvent ;
125+ invariant ( errorEvent != null , 'lastErrorEvent expected to be non-null' ) ;
75126 otherView =
76127 this . props . renderError &&
77128 this . props . renderError (
@@ -81,7 +132,7 @@ class WebView extends React.Component {
81132 ) ;
82133 } else if ( this . state . viewState !== WebViewState . IDLE ) {
83134 console . error (
84- 'RCTWebView invalid state encountered: ' + this . state . loading ,
135+ 'RCTWebView invalid state encountered: ' + this . state . viewState ,
85136 ) ;
86137 }
87138
@@ -94,11 +145,11 @@ class WebView extends React.Component {
94145 webViewStyles . push ( styles . hidden ) ;
95146 }
96147
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 } ;
102153 }
103154
104155 if ( source . method === 'POST' && source . headers ) {
@@ -198,7 +249,7 @@ class WebView extends React.Component {
198249 ) ;
199250 } ;
200251
201- postMessage = data => {
252+ postMessage = ( data : string ) => {
202253 UIManager . dispatchViewManagerCommand (
203254 this . getWebViewHandle ( ) ,
204255 UIManager . RCTWebView . Commands . postMessage ,
@@ -212,7 +263,7 @@ class WebView extends React.Component {
212263 * on pages with a Content Security Policy that disallows eval(). If you need that
213264 * functionality, look into postMessage/onMessage.
214265 */
215- injectJavaScript = data => {
266+ injectJavaScript = ( data : string ) => {
216267 UIManager . dispatchViewManagerCommand (
217268 this . getWebViewHandle ( ) ,
218269 UIManager . RCTWebView . Commands . injectJavaScript ,
@@ -224,7 +275,7 @@ class WebView extends React.Component {
224275 * We return an event with a bunch of fields including:
225276 * url, title, loading, canGoBack, canGoForward
226277 */
227- updateNavigationState = event => {
278+ updateNavigationState = ( event : WebViewEvent ) => {
228279 if ( this . props . onNavigationStateChange ) {
229280 this . props . onNavigationStateChange ( event . nativeEvent ) ;
230281 }
@@ -234,13 +285,13 @@ class WebView extends React.Component {
234285 return ReactNative . findNodeHandle ( this . refs [ RCT_WEBVIEW_REF ] ) ;
235286 } ;
236287
237- onLoadingStart = event => {
288+ onLoadingStart = ( event : WebViewEvent ) => {
238289 const onLoadStart = this . props . onLoadStart ;
239290 onLoadStart && onLoadStart ( event ) ;
240291 this . updateNavigationState ( event ) ;
241292 } ;
242293
243- onLoadingError = event => {
294+ onLoadingError = ( event : WebViewEvent ) => {
244295 event . persist ( ) ; // persist this event because we need to store it
245296 const { onError, onLoadEnd } = this . props ;
246297 onError && onError ( event ) ;
@@ -253,7 +304,7 @@ class WebView extends React.Component {
253304 } ) ;
254305 } ;
255306
256- onLoadingFinish = event => {
307+ onLoadingFinish = ( event : WebViewEvent ) => {
257308 const { onLoad, onLoadEnd } = this . props ;
258309 onLoad && onLoad ( event ) ;
259310 onLoadEnd && onLoadEnd ( event ) ;
@@ -263,7 +314,7 @@ class WebView extends React.Component {
263314 this . updateNavigationState ( event ) ;
264315 } ;
265316
266- onMessage = ( event ) => {
317+ onMessage = ( event : WebViewEvent ) => {
267318 const { onMessage } = this . props ;
268319 onMessage && onMessage ( event ) ;
269320 } ;
0 commit comments