Browse Source

Clean up flow types

empyrical 6 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
 
14
 import { requireNativeComponent } from 'react-native';
14
 import { requireNativeComponent } from 'react-native';
15
 
15
 
16
+import type {DataDetectorTypes} from './WebViewTypes';
17
+
16
 const RCTWKWebView = requireNativeComponent('RCTWKWebView');
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
 class WKWebView extends React.Component<RCTWKWebViewProps> {
28
 class WKWebView extends React.Component<RCTWKWebViewProps> {
25
   componentWillReceiveProps(nextProps: RCTWKWebViewProps) {
29
   componentWillReceiveProps(nextProps: RCTWKWebViewProps) {

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

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

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

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

+ 55
- 20
js/WebViewTypes.js View File

10
 
10
 
11
 'use strict';
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
 import type {EdgeInsetsProp} from 'EdgeInsetsPropType';
15
 import type {EdgeInsetsProp} from 'EdgeInsetsPropType';
15
 import type {ViewStyleProp} from 'StyleSheet';
16
 import type {ViewStyleProp} from 'StyleSheet';
16
 import type {ViewProps} from 'ViewPropTypes';
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
 export type DataDetectorTypes =
61
 export type DataDetectorTypes =
27
   | 'phoneNumber'
62
   | 'phoneNumber'
34
   | 'none'
69
   | 'none'
35
   | 'all';
70
   | 'all';
36
 
71
 
37
-export type WebViewSourceUri = {|
72
+export type WebViewSourceUri = $ReadOnly<{|
38
   /**
73
   /**
39
    * The URI to load in the `WebView`. Can be a local or remote file.
74
    * The URI to load in the `WebView`. Can be a local or remote file.
40
    */
75
    */
59
    * NOTE: On Android, this can only be used with POST requests.
94
    * NOTE: On Android, this can only be used with POST requests.
60
    */
95
    */
61
   body?: string,
96
   body?: string,
62
-|};
97
+|}>;
63
 
98
 
64
-export type WebViewSourceHtml = {|
99
+export type WebViewSourceHtml = $ReadOnly<{|
65
   /**
100
   /**
66
    * A static HTML page to display in the WebView.
101
    * A static HTML page to display in the WebView.
67
    */
102
    */
70
    * The base URL to be used for any relative links in the HTML.
105
    * The base URL to be used for any relative links in the HTML.
71
    */
106
    */
72
   baseUrl?: ?string,
107
   baseUrl?: ?string,
73
-|};
108
+|}>;
74
 
109
 
75
 export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
110
 export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
76
 
111
 
78
   /*
113
   /*
79
    * The native component used to render the WebView.
114
    * The native component used to render the WebView.
80
    */
115
    */
81
-  component?: ?any,
116
+  component?: ComponentType<WebViewSharedProps>,
82
   /*
117
   /*
83
    * Set props directly on the native component WebView. Enables custom props which the
118
    * Set props directly on the native component WebView. Enables custom props which the
84
    * original WebView doesn't pass through.
119
    * original WebView doesn't pass through.
116
   /**
151
   /**
117
    * Function that returns a view to show if there's an error.
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
    * Function that returns a loading indicator.
157
    * Function that returns a loading indicator.
126
   /**
161
   /**
127
    * Function that is invoked when the `WebView` has finished loading.
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
    * Function that is invoked when the `WebView` load succeeds or fails.
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
    * Function that is invoked when the `WebView` starts loading.
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
    * Function that is invoked when the `WebView` load fails.
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
    * Boolean value that determines whether the web view bounces
182
    * Boolean value that determines whether the web view bounces
187
   /**
222
   /**
188
    * Function that is invoked when the `WebView` loading starts or ends.
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
    * A function that is invoked when the webview calls `window.postMessage`.
228
    * A function that is invoked when the webview calls `window.postMessage`.
198
    * available on the event object, `event.nativeEvent.data`. `data`
233
    * available on the event object, `event.nativeEvent.data`. `data`
199
    * must be a string.
234
    * must be a string.
200
    */
235
    */
201
-  onMessage?: (event: WebViewEvent) => any,
236
+  onMessage?: (event: WebViewMessageEvent) => mixed,
202
 
237
 
203
   /**
238
   /**
204
    * Boolean value that forces the `WebView` to show the loading view
239
    * Boolean value that forces the `WebView` to show the loading view
281
    * to stop loading.
316
    * to stop loading.
282
    * @platform ios
317
    * @platform ios
283
    */
318
    */
284
-  onShouldStartLoadWithRequest?: (event: WebViewEvent) => any,
319
+  onShouldStartLoadWithRequest?: (event: WebViewEvent) => mixed,
285
 
320
 
286
   /**
321
   /**
287
    * Boolean that determines whether HTML5 videos play inline or use the
322
    * Boolean that determines whether HTML5 videos play inline or use the