Browse Source

feat(typings): Add typescript definitions (#95)

Typescript definitions are based on flow
Tested them a bit in my project
I think couple of event types are screwed in flow definitions (synthetic/non-synthetic are mixed), tried my best to test which events are really received.
doomsower 6 years ago
parent
commit
715fe61cc5
3 changed files with 445 additions and 0 deletions
  1. 3
    0
      package.json
  2. 420
    0
      typings/index.d.ts
  3. 22
    0
      yarn.lock

+ 3
- 0
package.json View File

@@ -2,6 +2,7 @@
2 2
   "name": "react-native-webview",
3 3
   "description": "React Native WebView component for iOS, Android, and Windows 10 (coming soon)",
4 4
   "main": "index.js",
5
+  "typings": "typings/index.d.ts",
5 6
   "author": "Jamon Holmgren <jamon@infinite.red>",
6 7
   "contributors": [
7 8
     "Thibault Malbranche <malbranche.thibault@gmail.com>"
@@ -27,6 +28,8 @@
27 28
   },
28 29
   "devDependencies": {
29 30
     "@semantic-release/git": "7.0.5",
31
+    "@types/react": "^16.4.18",
32
+    "@types/react-native": "^0.57.6",
30 33
     "flow-bin": "^0.80.0",
31 34
     "react-native": "^0.57",
32 35
     "semantic-release": "15.10.3"

+ 420
- 0
typings/index.d.ts View File

@@ -0,0 +1,420 @@
1
+import { ComponentType, ReactElement, ReactNode, Component } from 'react';
2
+import { Insets, NativeSyntheticEvent, StyleProp, ViewProps, ViewStyle } from 'react-native';
3
+
4
+export interface WebViewNativeEvent {
5
+  readonly url: string;
6
+  readonly loading: boolean;
7
+  readonly title: string;
8
+  readonly canGoBack: boolean;
9
+  readonly canGoForward: boolean;
10
+}
11
+
12
+export interface WebViewProgressEvent extends WebViewNativeEvent {
13
+  readonly progress: number;
14
+}
15
+
16
+export interface WebViewNavigation extends WebViewNativeEvent {
17
+  readonly navigationType:
18
+    | 'click'
19
+    | 'formsubmit'
20
+    | 'backforward'
21
+    | 'reload'
22
+    | 'formresubmit'
23
+    | 'other';
24
+}
25
+
26
+export interface WebViewMessage extends WebViewNativeEvent {
27
+  readonly data: string;
28
+}
29
+
30
+export interface WebViewError extends WebViewNativeEvent {
31
+  readonly domain?: string;
32
+  readonly code: number;
33
+  readonly description: string;
34
+}
35
+
36
+export type WebViewEvent = NativeSyntheticEvent<WebViewNativeEvent>;
37
+
38
+export type WebViewNavigationEvent = NativeSyntheticEvent<WebViewNavigation>;
39
+
40
+export type WebViewMessageEvent = NativeSyntheticEvent<WebViewMessage>;
41
+
42
+export type WebViewErrorEvent = NativeSyntheticEvent<WebViewError>;
43
+
44
+export type DataDetectorTypes =
45
+  | 'phoneNumber'
46
+  | 'link'
47
+  | 'address'
48
+  | 'calendarEvent'
49
+  | 'trackingNumber'
50
+  | 'flightNumber'
51
+  | 'lookupSuggestion'
52
+  | 'none'
53
+  | 'all';
54
+
55
+export type OverScrollModeType = 'always' | 'content' | 'never';
56
+
57
+export interface WebViewSourceUri {
58
+  /**
59
+   * The URI to load in the `WebView`. Can be a local or remote file.
60
+   */
61
+  uri?: string;
62
+
63
+  /**
64
+   * The HTTP Method to use. Defaults to GET if not specified.
65
+   * NOTE: On Android, only GET and POST are supported.
66
+   */
67
+  method?: string;
68
+
69
+  /**
70
+   * Additional HTTP headers to send with the request.
71
+   * NOTE: On Android, this can only be used with GET requests.
72
+   */
73
+  headers?: {[key: string]: string};
74
+
75
+  /**
76
+   * The HTTP body to send with the request. This must be a valid
77
+   * UTF-8 string, and will be sent exactly as specified, with no
78
+   * additional encoding (e.g. URL-escaping or base64) applied.
79
+   * NOTE: On Android, this can only be used with POST requests.
80
+   */
81
+  body?: string;
82
+}
83
+
84
+export interface WebViewSourceHtml {
85
+  /**
86
+   * A static HTML page to display in the WebView.
87
+   */
88
+  html?: string;
89
+  /**
90
+   * The base URL to be used for any relative links in the HTML.
91
+   */
92
+  baseUrl?: string;
93
+}
94
+
95
+export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
96
+
97
+export interface WebViewNativeConfig {
98
+  /*
99
+    * The native component used to render the WebView.
100
+    */
101
+  component?: ComponentType<WebViewSharedProps>;
102
+  /*
103
+    * Set props directly on the native component WebView. Enables custom props which the
104
+    * original WebView doesn't pass through.
105
+    */
106
+  props?: any;
107
+  /*
108
+    * Set the ViewManager to use for communication with the native side.
109
+    * @platform ios
110
+    */
111
+  viewManager?: any;
112
+}
113
+
114
+export interface IOSWebViewProps {
115
+  /**
116
+   * If true, use WKWebView instead of UIWebView.
117
+   * @platform ios
118
+   */
119
+  useWebKit?: boolean;
120
+
121
+  /**
122
+   * Boolean value that determines whether the web view bounces
123
+   * when it reaches the edge of the content. The default value is `true`.
124
+   * @platform ios
125
+   */
126
+  bounces?: boolean;
127
+
128
+  /**
129
+   * A floating-point number that determines how quickly the scroll view
130
+   * decelerates after the user lifts their finger. You may also use the
131
+   * string shortcuts `"normal"` and `"fast"` which match the underlying iOS
132
+   * settings for `UIScrollViewDecelerationRateNormal` and
133
+   * `UIScrollViewDecelerationRateFast` respectively:
134
+   *
135
+   *   - normal: 0.998
136
+   *   - fast: 0.99 (the default for iOS web view)
137
+   * @platform ios
138
+   */
139
+  decelerationRate?: 'fast' | 'normal' | number;
140
+
141
+  /**
142
+   * Boolean value that determines whether scrolling is enabled in the
143
+   * `WebView`. The default value is `true`.
144
+   * @platform ios
145
+   */
146
+  scrollEnabled?: boolean;
147
+
148
+  /**
149
+   * The amount by which the web view content is inset from the edges of
150
+   * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
151
+   * @platform ios
152
+   */
153
+  contentInset?: Insets;
154
+
155
+  /**
156
+   * Determines the types of data converted to clickable URLs in the web view's content.
157
+   * By default only phone numbers are detected.
158
+   *
159
+   * You can provide one type or an array of many types.
160
+   *
161
+   * Possible values for `dataDetectorTypes` are:
162
+   *
163
+   * - `'phoneNumber'`
164
+   * - `'link'`
165
+   * - `'address'`
166
+   * - `'calendarEvent'`
167
+   * - `'none'`
168
+   * - `'all'`
169
+   *
170
+   * With the new WebKit implementation, we have three new values:
171
+   * - `'trackingNumber'`,
172
+   * - `'flightNumber'`,
173
+   * - `'lookupSuggestion'`,
174
+   *
175
+   * @platform ios
176
+   */
177
+  dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
178
+
179
+  /**
180
+   * Function that allows custom handling of any web view requests. Return
181
+   * `true` from the function to continue loading the request and `false`
182
+   * to stop loading.
183
+   * @platform ios
184
+   */
185
+  onShouldStartLoadWithRequest?: (event: WebViewNativeEvent) => any;
186
+
187
+  /**
188
+   * Boolean that determines whether HTML5 videos play inline or use the
189
+   * native full-screen controller. The default value is `false`.
190
+   *
191
+   * **NOTE** : In order for video to play inline, not only does this
192
+   * property need to be set to `true`, but the video element in the HTML
193
+   * document must also include the `webkit-playsinline` attribute.
194
+   * @platform ios
195
+   */
196
+  allowsInlineMediaPlayback?: boolean;
197
+  /**
198
+   * Hide the accessory view when the keyboard is open. Default is false to be
199
+   * backward compatible.
200
+   */
201
+  hideKeyboardAccessoryView?: boolean;
202
+}
203
+
204
+export interface AndroidWebViewProps {
205
+  onNavigationStateChange?: (event: WebViewNavigation) => any;
206
+  onContentSizeChange?: (event: WebViewEvent) => any;
207
+
208
+  /**
209
+   * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
210
+   * Sets the overScrollMode. Possible values are:
211
+   *
212
+   * - `'always'` (default)
213
+   * - `'content'`
214
+   * - `'never'`
215
+   *
216
+   * @platform android
217
+   */
218
+  overScrollMode?: OverScrollModeType;
219
+
220
+  /**
221
+   * Sets whether Geolocation is enabled. The default is false.
222
+   * @platform android
223
+   */
224
+  geolocationEnabled?: boolean;
225
+
226
+  /**
227
+   * Boolean that sets whether JavaScript running in the context of a file
228
+   * scheme URL should be allowed to access content from any origin.
229
+   * Including accessing content from other file scheme URLs
230
+   * @platform android
231
+   */
232
+  allowUniversalAccessFromFileURLs?: boolean;
233
+
234
+  /**
235
+   * Sets whether the webview allow access to file system.
236
+   * @platform android
237
+   */
238
+  allowFileAccess?: boolean;
239
+
240
+  /**
241
+   * Used on Android only, controls whether form autocomplete data should be saved
242
+   * @platform android
243
+   */
244
+  saveFormDataDisabled?: boolean;
245
+
246
+  /*
247
+    * Used on Android only, controls whether the given list of URL prefixes should
248
+    * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
249
+    * default activity intent for those URL instead of loading it within the webview.
250
+    * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
251
+    * @platform android
252
+    */
253
+  urlPrefixesForDefaultIntent?: string[];
254
+
255
+  /**
256
+   * Boolean value to enable JavaScript in the `WebView`. Used on Android only
257
+   * as JavaScript is enabled by default on iOS. The default value is `true`.
258
+   * @platform android
259
+   */
260
+  javaScriptEnabled?: boolean;
261
+
262
+  /**
263
+   * Boolean value to enable third party cookies in the `WebView`. Used on
264
+   * Android Lollipop and above only as third party cookies are enabled by
265
+   * default on Android Kitkat and below and on iOS. The default value is `true`.
266
+   * @platform android
267
+   */
268
+  thirdPartyCookiesEnabled?: boolean;
269
+
270
+  /**
271
+   * Boolean value to control whether DOM Storage is enabled. Used only in
272
+   * Android.
273
+   * @platform android
274
+   */
275
+  domStorageEnabled?: boolean;
276
+
277
+  /**
278
+   * Sets the user-agent for the `WebView`.
279
+   * @platform android
280
+   */
281
+  userAgent?: string;
282
+
283
+  /**
284
+   * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.
285
+   *
286
+   * Possible values for `mixedContentMode` are:
287
+   *
288
+   * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin.
289
+   * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure.
290
+   * - `'compatibility'` -  WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content.
291
+   * @platform android
292
+   */
293
+  mixedContentMode?: 'never' | 'always' | 'compatibility';
294
+}
295
+
296
+export interface WebViewSharedProps extends ViewProps, IOSWebViewProps, AndroidWebViewProps {
297
+  /**
298
+   * @Deprecated. Use `source` instead.
299
+   */
300
+  url?: string;
301
+  /**
302
+   * @Deprecated. Use `source` instead.
303
+   */
304
+  html?: string;
305
+
306
+  /**
307
+   * Loads static html or a uri (with optional headers) in the WebView.
308
+   */
309
+  source?: WebViewSource;
310
+
311
+  /**
312
+   * Function that returns a view to show if there's an error.
313
+   */
314
+  renderError?: (errorDomain: string | undefined, errorCode: number, errorDesc: string) => ReactElement<any>; // view to show if there's an error
315
+
316
+  /**
317
+   * Function that returns a loading indicator.
318
+   */
319
+  renderLoading?: () => ReactElement<any>;
320
+
321
+  /**
322
+   * Function that is invoked when the `WebView` has finished loading.
323
+   */
324
+  onLoad?: (event: WebViewNavigationEvent) => any;
325
+
326
+  /**
327
+   * Function that is invoked when the `WebView` load succeeds or fails.
328
+   */
329
+  onLoadEnd?: (event: WebViewNavigationEvent | WebViewErrorEvent) => any;
330
+
331
+  /**
332
+   * Function that is invoked when the `WebView` starts loading.
333
+   */
334
+  onLoadStart?: (event: WebViewNavigationEvent) => any;
335
+
336
+  /**
337
+   * Function that is invoked when the `WebView` load fails.
338
+   */
339
+  onError?: (event: WebViewErrorEvent) => any;
340
+
341
+  /**
342
+   * Controls whether to adjust the content inset for web views that are
343
+   * placed behind a navigation bar, tab bar, or toolbar. The default value
344
+   * is `true`.
345
+   */
346
+  automaticallyAdjustContentInsets?: boolean;
347
+
348
+  /**
349
+   * Function that is invoked when the `WebView` loading starts or ends.
350
+   */
351
+  onNavigationStateChange?: (event: WebViewNavigation) => any;
352
+
353
+  /**
354
+   * A function that is invoked when the webview calls `window.postMessage`.
355
+   * Setting this property will inject a `postMessage` global into your
356
+   * webview, but will still call pre-existing values of `postMessage`.
357
+   *
358
+   * `window.postMessage` accepts one argument, `data`, which will be
359
+   * available on the event object, `event.nativeEvent.data`. `data`
360
+   * must be a string.
361
+   */
362
+  onMessage?: (event: WebViewMessageEvent) => any;
363
+
364
+  /**
365
+   * Function that is invoked when the `WebView` is loading.
366
+   */
367
+  onLoadProgress?: (event: NativeSyntheticEvent<WebViewProgressEvent>) => any;
368
+
369
+  /**
370
+   * Boolean value that forces the `WebView` to show the loading view
371
+   * on the first load.
372
+   */
373
+  startInLoadingState?: string;
374
+
375
+  /**
376
+   * Set this to provide JavaScript that will be injected into the web page
377
+   * when the view loads.
378
+   */
379
+  injectedJavaScript?: string;
380
+
381
+  /**
382
+   * Boolean that controls whether the web content is scaled to fit
383
+   * the view and enables the user to change the scale. The default value
384
+   * is `true`.
385
+   *
386
+   * On iOS, when `useWebKit=true`, this prop will not work.
387
+   */
388
+  scalesPageToFit?: boolean;
389
+
390
+  /**
391
+   * Boolean that determines whether HTML5 audio and video requires the user
392
+   * to tap them before they start playing. The default value is `true`.
393
+   */
394
+  mediaPlaybackRequiresUserAction?: boolean;
395
+
396
+  /**
397
+   * List of origin strings to allow being navigated to. The strings allow
398
+   * wildcards and get matched against *just* the origin (not the full URL).
399
+   * If the user taps to navigate to a new page but the new page is not in
400
+   * this whitelist, we will open the URL in Safari.
401
+   * The default whitelisted origins are "http://*" and "https://*".
402
+   */
403
+  originWhitelist?: string[];
404
+
405
+  /**
406
+   * Override the native component used to render the WebView. Enables a custom native
407
+   * WebView which uses the same JavaScript as the original WebView.
408
+   */
409
+  nativeConfig?: WebViewNativeConfig;
410
+
411
+  style?: StyleProp<ViewStyle>;
412
+  children?: ReactNode;
413
+}
414
+
415
+export class WebView extends Component<WebViewSharedProps> {
416
+  public goForward: () => void;
417
+  public goBack: () => void;
418
+  public reload: () => void;
419
+  public stopLoading: () => void;
420
+}

+ 22
- 0
yarn.lock View File

@@ -739,6 +739,24 @@
739 739
     into-stream "^4.0.0"
740 740
     lodash "^4.17.4"
741 741
 
742
+"@types/prop-types@*":
743
+  version "15.5.6"
744
+  resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.6.tgz#9c03d3fed70a8d517c191b7734da2879b50ca26c"
745
+
746
+"@types/react-native@^0.57.6":
747
+  version "0.57.6"
748
+  resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.57.6.tgz#b53afeebcb19f7ba353f0937b6778d39c2cf10d0"
749
+  dependencies:
750
+    "@types/prop-types" "*"
751
+    "@types/react" "*"
752
+
753
+"@types/react@*", "@types/react@^16.4.18":
754
+  version "16.4.18"
755
+  resolved "https://registry.yarnpkg.com/@types/react/-/react-16.4.18.tgz#2e28a2e7f92d3fa7d6a65f2b73275c3e3138a13d"
756
+  dependencies:
757
+    "@types/prop-types" "*"
758
+    csstype "^2.2.0"
759
+
742 760
 JSONStream@^1.0.4, JSONStream@^1.3.4:
743 761
   version "1.3.5"
744 762
   resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
@@ -2082,6 +2100,10 @@ crypto-random-string@^1.0.0:
2082 2100
   version "1.0.0"
2083 2101
   resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
2084 2102
 
2103
+csstype@^2.2.0:
2104
+  version "2.5.7"
2105
+  resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.7.tgz#bf9235d5872141eccfb2d16d82993c6b149179ff"
2106
+
2085 2107
 currently-unhandled@^0.4.1:
2086 2108
   version "0.4.1"
2087 2109
   resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"