Browse Source

Clean up flow types

empyrical 5 years ago
parent
commit
795dffb6a7
No account linked to committer's email address
4 changed files with 91 additions and 45 deletions
  1. 9
    5
      js/WKWebView.ios.js
  2. 15
    11
      js/WebView.android.js
  3. 12
    9
      js/WebView.ios.js
  4. 55
    20
      js/WebViewTypes.js

+ 9
- 5
js/WKWebView.ios.js View File

@@ -13,13 +13,17 @@ import React from 'react';
13 13
 
14 14
 import { requireNativeComponent } from 'react-native';
15 15
 
16
+import type {DataDetectorTypes} from './WebViewTypes';
17
+
16 18
 const RCTWKWebView = requireNativeComponent('RCTWKWebView');
17 19
 
18
-type RCTWKWebViewProps = {
19
-  allowsInlineMediaPlayback?: boolean,
20
-  mediaPlaybackRequiresUserAction?: boolean,
21
-  dataDetectorTypes?: boolean,
22
-};
20
+type RCTWKWebViewProps = $ReadOnly<{|
21
+  allowsInlineMediaPlayback?: ?boolean,
22
+  mediaPlaybackRequiresUserAction?: ?boolean,
23
+  dataDetectorTypes?:
24
+    | ?DataDetectorTypes
25
+    | $ReadOnlyArray<DataDetectorTypes>,
26
+|}>;
23 27
 
24 28
 class WKWebView extends React.Component<RCTWKWebViewProps> {
25 29
   componentWillReceiveProps(nextProps: RCTWKWebViewProps) {

+ 15
- 11
js/WebView.android.js View File

@@ -27,8 +27,12 @@ import keyMirror from 'fbjs/lib/keyMirror';
27 27
 
28 28
 import WebViewShared from './WebViewShared';
29 29
 import type {
30
-  WebViewErrorEvent,
31 30
   WebViewEvent,
31
+  WebViewError,
32
+  WebViewErrorEvent,
33
+  WebViewMessageEvent,
34
+  WebViewNavigation,
35
+  WebViewNavigationEvent,
32 36
   WebViewSharedProps,
33 37
   WebViewSource,
34 38
 } from './WebViewTypes';
@@ -51,14 +55,14 @@ const defaultRenderLoading = () => (
51 55
 
52 56
 type State = {|
53 57
   viewState: WebViewState,
54
-  lastErrorEvent: ?WebViewErrorEvent,
58
+  lastErrorEvent: ?WebViewError,
55 59
   startInLoadingState: boolean,
56 60
 |};
57 61
 
58 62
 type WebViewPropsAndroid = $ReadOnly<{|
59 63
   ...WebViewSharedProps,
60
-  onNavigationStateChange?: (event: WebViewEvent) => any,
61
-  onContentSizeChange?: (event: WebViewEvent) => any,
64
+  onNavigationStateChange?: (event: WebViewNavigation) => mixed,
65
+  onContentSizeChange?: (event: WebViewEvent) => mixed,
62 66
 
63 67
   /**
64 68
    * Sets whether Geolocation is enabled. The default is false.
@@ -105,7 +109,7 @@ class WebView extends React.Component<WebViewPropsAndroid, State> {
105 109
 
106 110
   state = {
107 111
     viewState: WebViewState.IDLE,
108
-    lastErrorEvent: (null: ?WebViewErrorEvent),
112
+    lastErrorEvent: null,
109 113
     startInLoadingState: true,
110 114
   };
111 115
 
@@ -145,7 +149,7 @@ class WebView extends React.Component<WebViewPropsAndroid, State> {
145 149
       webViewStyles.push(styles.hidden);
146 150
     }
147 151
 
148
-    let source = this.props.source || ({}: WebViewSource);
152
+    let source: WebViewSource = this.props.source || {};
149 153
     if (!this.props.source && this.props.html) {
150 154
       source = { html: this.props.html };
151 155
     } else if (!this.props.source && this.props.url) {
@@ -275,7 +279,7 @@ class WebView extends React.Component<WebViewPropsAndroid, State> {
275 279
    * We return an event with a bunch of fields including:
276 280
    *  url, title, loading, canGoBack, canGoForward
277 281
    */
278
-  updateNavigationState = (event: WebViewEvent) => {
282
+  updateNavigationState = (event: WebViewNavigationEvent) => {
279 283
     if (this.props.onNavigationStateChange) {
280 284
       this.props.onNavigationStateChange(event.nativeEvent);
281 285
     }
@@ -285,13 +289,13 @@ class WebView extends React.Component<WebViewPropsAndroid, State> {
285 289
     return ReactNative.findNodeHandle(this.refs[RCT_WEBVIEW_REF]);
286 290
   };
287 291
 
288
-  onLoadingStart = (event: WebViewEvent) => {
292
+  onLoadingStart = (event: WebViewNavigationEvent) => {
289 293
     const onLoadStart = this.props.onLoadStart;
290 294
     onLoadStart && onLoadStart(event);
291 295
     this.updateNavigationState(event);
292 296
   };
293 297
 
294
-  onLoadingError = (event: WebViewEvent) => {
298
+  onLoadingError = (event: WebViewErrorEvent) => {
295 299
     event.persist(); // persist this event because we need to store it
296 300
     const { onError, onLoadEnd } = this.props;
297 301
     onError && onError(event);
@@ -304,7 +308,7 @@ class WebView extends React.Component<WebViewPropsAndroid, State> {
304 308
     });
305 309
   };
306 310
 
307
-  onLoadingFinish = (event: WebViewEvent) => {
311
+  onLoadingFinish = (event: WebViewNavigationEvent) => {
308 312
     const { onLoad, onLoadEnd } = this.props;
309 313
     onLoad && onLoad(event);
310 314
     onLoadEnd && onLoadEnd(event);
@@ -314,7 +318,7 @@ class WebView extends React.Component<WebViewPropsAndroid, State> {
314 318
     this.updateNavigationState(event);
315 319
   };
316 320
 
317
-  onMessage = (event: WebViewEvent) => {
321
+  onMessage = (event: WebViewMessageEvent) => {
318 322
     const { onMessage } = this.props;
319 323
     onMessage && onMessage(event);
320 324
   };

+ 12
- 9
js/WebView.ios.js View File

@@ -30,8 +30,11 @@ import keyMirror from 'fbjs/lib/keyMirror';
30 30
 
31 31
 import WebViewShared from './WebViewShared';
32 32
 import type {
33
-  WebViewErrorEvent,
34 33
   WebViewEvent,
34
+  WebViewError,
35
+  WebViewErrorEvent,
36
+  WebViewMessageEvent,
37
+  WebViewNavigationEvent,
35 38
   WebViewSharedProps,
36 39
   WebViewSource,
37 40
 } from './WebViewTypes';
@@ -73,7 +76,7 @@ const JSNavigationScheme = 'react-js-navigation';
73 76
 
74 77
 type State = {|
75 78
   viewState: WebViewState,
76
-  lastErrorEvent: ?WebViewErrorEvent,
79
+  lastErrorEvent: ?WebViewError,
77 80
   startInLoadingState: boolean,
78 81
 |};
79 82
 
@@ -136,7 +139,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
136 139
 
137 140
   state = {
138 141
     viewState: WebViewState.IDLE,
139
-    lastErrorEvent: (null: ?WebViewErrorEvent),
142
+    lastErrorEvent: null,
140 143
     startInLoadingState: true,
141 144
   };
142 145
 
@@ -232,7 +235,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
232 235
       this.props.decelerationRate,
233 236
     );
234 237
 
235
-    let source = this.props.source || ({}: WebViewSource);
238
+    let source: WebViewSource = this.props.source || {};
236 239
     if (!this.props.source && this.props.html) {
237 240
       source = { html: this.props.html };
238 241
     } else if (!this.props.source && this.props.url) {
@@ -376,7 +379,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
376 379
    * We return an event with a bunch of fields including:
377 380
    *  url, title, loading, canGoBack, canGoForward
378 381
    */
379
-  _updateNavigationState = (event: WebViewEvent) => {
382
+  _updateNavigationState = (event: WebViewNavigationEvent) => {
380 383
     if (this.props.onNavigationStateChange) {
381 384
       this.props.onNavigationStateChange(event.nativeEvent);
382 385
     }
@@ -389,13 +392,13 @@ class WebView extends React.Component<WebViewSharedProps, State> {
389 392
     return ReactNative.findNodeHandle(this.refs[RCT_WEBVIEW_REF]);
390 393
   };
391 394
 
392
-  _onLoadingStart = (event: WebViewEvent) => {
395
+  _onLoadingStart = (event: WebViewNavigationEvent) => {
393 396
     const onLoadStart = this.props.onLoadStart;
394 397
     onLoadStart && onLoadStart(event);
395 398
     this._updateNavigationState(event);
396 399
   };
397 400
 
398
-  _onLoadingError = (event: WebViewEvent) => {
401
+  _onLoadingError = (event: WebViewErrorEvent) => {
399 402
     event.persist(); // persist this event because we need to store it
400 403
     const { onError, onLoadEnd } = this.props;
401 404
     onError && onError(event);
@@ -408,7 +411,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
408 411
     });
409 412
   };
410 413
 
411
-  _onLoadingFinish = (event: WebViewEvent) => {
414
+  _onLoadingFinish = (event: WebViewNavigationEvent) => {
412 415
     const { onLoad, onLoadEnd } = this.props;
413 416
     onLoad && onLoad(event);
414 417
     onLoadEnd && onLoadEnd(event);
@@ -418,7 +421,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
418 421
     this._updateNavigationState(event);
419 422
   };
420 423
 
421
-  _onMessage = (event: WebViewEvent) => {
424
+  _onMessage = (event: WebViewMessageEvent) => {
422 425
     const { onMessage } = this.props;
423 426
     onMessage && onMessage(event);
424 427
   };

+ 55
- 20
js/WebViewTypes.js View File

@@ -10,18 +10,53 @@
10 10
 
11 11
 'use strict';
12 12
 
13
-import type {Node, Element} from 'react';
13
+import type {Node, Element, ComponentType} from 'react';
14
+import type {SyntheticEvent} from 'CoreEventTypes';
14 15
 import type {EdgeInsetsProp} from 'EdgeInsetsPropType';
15 16
 import type {ViewStyleProp} from 'StyleSheet';
16 17
 import type {ViewProps} from 'ViewPropTypes';
17 18
 
18
-export type WebViewErrorEvent = {
19
-  domain: any,
20
-  code: any,
21
-  description: any,
22
-};
19
+export type WebViewNativeEvent = $ReadOnly<{|
20
+  url: string,
21
+  loading: boolean,
22
+  title: string,
23
+  canGoBack: boolean,
24
+  canGoForward: boolean,
25
+|}>;
26
+
27
+export type WebViewNavigation = $ReadOnly<{|
28
+  ...WebViewNativeEvent,
29
+  navigationType:
30
+    | 'click'
31
+    | 'formsubmit'
32
+    | 'backforward'
33
+    | 'reload'
34
+    | 'formresubmit'
35
+    | 'other',
36
+|}>;
37
+
38
+export type WebViewMessage = $ReadOnly<{|
39
+  ...WebViewNativeEvent,
40
+  data: string,
41
+|}>;
42
+
43
+export type WebViewError = $ReadOnly<{|
44
+  ...WebViewNativeEvent,
45
+  /**
46
+   * `domain` is only used on iOS
47
+   */
48
+  domain: ?string,
49
+  code: number,
50
+  description: string,
51
+|}>;
52
+
53
+export type WebViewEvent = SyntheticEvent<WebViewNativeEvent>;
54
+
55
+export type WebViewNavigationEvent = SyntheticEvent<WebViewNavigation>;
23 56
 
24
-export type WebViewEvent = Object;
57
+export type WebViewMessageEvent = SyntheticEvent<WebViewMessage>;
58
+
59
+export type WebViewErrorEvent = SyntheticEvent<WebViewError>;
25 60
 
26 61
 export type DataDetectorTypes =
27 62
   | 'phoneNumber'
@@ -34,7 +69,7 @@ export type DataDetectorTypes =
34 69
   | 'none'
35 70
   | 'all';
36 71
 
37
-export type WebViewSourceUri = {|
72
+export type WebViewSourceUri = $ReadOnly<{|
38 73
   /**
39 74
    * The URI to load in the `WebView`. Can be a local or remote file.
40 75
    */
@@ -59,9 +94,9 @@ export type WebViewSourceUri = {|
59 94
    * NOTE: On Android, this can only be used with POST requests.
60 95
    */
61 96
   body?: string,
62
-|};
97
+|}>;
63 98
 
64
-export type WebViewSourceHtml = {|
99
+export type WebViewSourceHtml = $ReadOnly<{|
65 100
   /**
66 101
    * A static HTML page to display in the WebView.
67 102
    */
@@ -70,7 +105,7 @@ export type WebViewSourceHtml = {|
70 105
    * The base URL to be used for any relative links in the HTML.
71 106
    */
72 107
   baseUrl?: ?string,
73
-|};
108
+|}>;
74 109
 
75 110
 export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
76 111
 
@@ -78,7 +113,7 @@ export type WebViewNativeConfig = $ReadOnly<{|
78 113
   /*
79 114
    * The native component used to render the WebView.
80 115
    */
81
-  component?: ?any,
116
+  component?: ComponentType<WebViewSharedProps>,
82 117
   /*
83 118
    * Set props directly on the native component WebView. Enables custom props which the
84 119
    * original WebView doesn't pass through.
@@ -116,7 +151,7 @@ export type WebViewSharedProps =  $ReadOnly<{|
116 151
   /**
117 152
    * Function that returns a view to show if there's an error.
118 153
    */
119
-  renderError: (errorDomain: any, errorCode: any, errorDesc: any) => Element<any>, // view to show if there's an error
154
+  renderError: (errorDomain: ?string, errorCode: number, errorDesc: string) => Element<any>, // view to show if there's an error
120 155
 
121 156
   /**
122 157
    * Function that returns a loading indicator.
@@ -126,22 +161,22 @@ export type WebViewSharedProps =  $ReadOnly<{|
126 161
   /**
127 162
    * Function that is invoked when the `WebView` has finished loading.
128 163
    */
129
-  onLoad: (event: WebViewEvent) => any,
164
+  onLoad: (event: WebViewNavigationEvent) => mixed,
130 165
 
131 166
   /**
132 167
    * Function that is invoked when the `WebView` load succeeds or fails.
133 168
    */
134
-  onLoadEnd: (event: WebViewEvent) => any,
169
+  onLoadEnd: (event: WebViewNavigationEvent | WebViewErrorEvent) => mixed,
135 170
 
136 171
   /**
137 172
    * Function that is invoked when the `WebView` starts loading.
138 173
    */
139
-  onLoadStart: (event: WebViewEvent) => any,
174
+  onLoadStart: (event: WebViewNavigationEvent) => mixed,
140 175
 
141 176
   /**
142 177
    * Function that is invoked when the `WebView` load fails.
143 178
    */
144
-  onError: (event: WebViewEvent) => any,
179
+  onError: (event: WebViewErrorEvent) => mixed,
145 180
 
146 181
   /**
147 182
    * Boolean value that determines whether the web view bounces
@@ -187,7 +222,7 @@ export type WebViewSharedProps =  $ReadOnly<{|
187 222
   /**
188 223
    * Function that is invoked when the `WebView` loading starts or ends.
189 224
    */
190
-  onNavigationStateChange?: (event: WebViewEvent) => any,
225
+  onNavigationStateChange?: (event: WebViewNavigation) => mixed,
191 226
 
192 227
   /**
193 228
    * A function that is invoked when the webview calls `window.postMessage`.
@@ -198,7 +233,7 @@ export type WebViewSharedProps =  $ReadOnly<{|
198 233
    * available on the event object, `event.nativeEvent.data`. `data`
199 234
    * must be a string.
200 235
    */
201
-  onMessage?: (event: WebViewEvent) => any,
236
+  onMessage?: (event: WebViewMessageEvent) => mixed,
202 237
 
203 238
   /**
204 239
    * Boolean value that forces the `WebView` to show the loading view
@@ -281,7 +316,7 @@ export type WebViewSharedProps =  $ReadOnly<{|
281 316
    * to stop loading.
282 317
    * @platform ios
283 318
    */
284
-  onShouldStartLoadWithRequest?: (event: WebViewEvent) => any,
319
+  onShouldStartLoadWithRequest?: (event: WebViewEvent) => mixed,
285 320
 
286 321
   /**
287 322
    * Boolean that determines whether HTML5 videos play inline or use the