瀏覽代碼

feat(new prop): containerStyle (#912)

* feat(new prop): containerStyle

* chore(docs): Add documentation for `containerStyle`
Marco Marinangeli 5 年之前
父節點
當前提交
902d3d1e7f
共有 4 個文件被更改,包括 39 次插入4 次删除
  1. 26
    2
      docs/Reference.md
  2. 3
    1
      src/WebView.android.tsx
  3. 3
    1
      src/WebView.ios.tsx
  4. 7
    0
      src/WebViewTypes.ts

+ 26
- 2
docs/Reference.md 查看文件

@@ -25,6 +25,7 @@ This document lays out the current public properties and methods for the React N
25 25
 - [`onShouldStartLoadWithRequest`](Reference.md#onshouldstartloadwithrequest)
26 26
 - [`startInLoadingState`](Reference.md#startinloadingstate)
27 27
 - [`style`](Reference.md#style)
28
+- [`containerStyle`](Reference.md#containerStyle)
28 29
 - [`decelerationRate`](Reference.md#decelerationrate)
29 30
 - [`domStorageEnabled`](Reference.md#domstorageenabled)
30 31
 - [`javaScriptEnabled`](Reference.md#javascriptenabled)
@@ -343,6 +344,7 @@ url
343 344
 ### `onHttpError`
344 345
 
345 346
 Function that is invoked when the `WebView` receives an http error.
347
+
346 348
 > **_Note_**
347 349
 > Android API minimum level 23.
348 350
 
@@ -357,7 +359,10 @@ Example:
357 359
   source={{ uri: 'https://facebook.github.io/react-native' }}
358 360
   onHttpError={syntheticEvent => {
359 361
     const { nativeEvent } = syntheticEvent;
360
-    console.warn('WebView received error status code: ', nativeEvent.statusCode);
362
+    console.warn(
363
+      'WebView received error status code: ',
364
+      nativeEvent.statusCode,
365
+    );
361 366
   }}
362 367
 />
363 368
 ```
@@ -446,7 +451,7 @@ Example:
446 451
   onContentProcessDidTerminate={syntheticEvent => {
447 452
     const { nativeEvent } = syntheticEvent;
448 453
     console.warn('Content process terminated, reloading', nativeEvent);
449
-    this.refs.webview.reload()
454
+    this.refs.webview.reload();
450 455
   }}
451 456
 />
452 457
 ```
@@ -602,6 +607,25 @@ Example:
602 607
 
603 608
 ---
604 609
 
610
+### `containerStyle`
611
+
612
+A style object that allow you to customize the `WebView` container style. Please note that there are default styles (example: you need to add `flex: 0` to the style if you want to use `height` property).
613
+
614
+| Type  | Required |
615
+| ----- | -------- |
616
+| style | No       |
617
+
618
+Example:
619
+
620
+```jsx
621
+<WebView
622
+  source={{ uri: 'https://facebook.github.io/react-native' }}
623
+  containerStyle={{ marginTop: 20 }}
624
+/>
625
+```
626
+
627
+---
628
+
605 629
 ### `decelerationRate`
606 630
 
607 631
 A floating-point number that determines how quickly the scroll view decelerates after the user lifts their finger. You may also use the string shortcuts `"normal"` and `"fast"` which match the underlying iOS settings for `UIScrollViewDecelerationRateNormal` and `UIScrollViewDecelerationRateFast` respectively:

+ 3
- 1
src/WebView.android.tsx 查看文件

@@ -252,6 +252,7 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
252 252
       renderLoading,
253 253
       source,
254 254
       style,
255
+      containerStyle,
255 256
       nativeConfig = {},
256 257
       ...otherProps
257 258
     } = this.props;
@@ -275,6 +276,7 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
275 276
     }
276 277
 
277 278
     const webViewStyles = [styles.container, styles.webView, style];
279
+    const webViewContainerStyle = [styles.container, containerStyle];
278 280
 
279 281
     if (source && 'method' in source) {
280 282
       if (source.method === 'POST' && source.headers) {
@@ -317,7 +319,7 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
317 319
     );
318 320
 
319 321
     return (
320
-      <View style={styles.container}>
322
+      <View style={webViewContainerStyle}>
321 323
         {webView}
322 324
         {otherView}
323 325
       </View>

+ 3
- 1
src/WebView.ios.tsx 查看文件

@@ -291,6 +291,7 @@ class WebView extends React.Component<IOSWebViewProps, State> {
291 291
       renderError,
292 292
       renderLoading,
293 293
       style,
294
+      containerStyle,
294 295
       ...otherProps
295 296
     } = this.props;
296 297
 
@@ -313,6 +314,7 @@ class WebView extends React.Component<IOSWebViewProps, State> {
313 314
     }
314 315
 
315 316
     const webViewStyles = [styles.container, styles.webView, style];
317
+    const webViewContainerStyle = [styles.container, containerStyle];
316 318
 
317 319
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
318 320
       this.onShouldStartLoadWithRequestCallback,
@@ -351,7 +353,7 @@ class WebView extends React.Component<IOSWebViewProps, State> {
351 353
     );
352 354
 
353 355
     return (
354
-      <View style={styles.container}>
356
+      <View style={webViewContainerStyle}>
355 357
         {webView}
356 358
         {otherView}
357 359
       </View>

+ 7
- 0
src/WebViewTypes.ts 查看文件

@@ -4,6 +4,8 @@ import { ReactElement, Component } from 'react';
4 4
 import {
5 5
   NativeSyntheticEvent,
6 6
   ViewProps,
7
+  StyleProp,
8
+  ViewStyle,
7 9
   NativeMethodsMixin,
8 10
   Constructor,
9 11
   UIManagerStatic,
@@ -576,6 +578,11 @@ export interface WebViewSharedProps extends ViewProps {
576 578
    */
577 579
   javaScriptEnabled?: boolean;
578 580
 
581
+  /**
582
+   * Stylesheet object to set the style of the container view.
583
+   */
584
+  containerStyle?: StyleProp<ViewStyle>;
585
+
579 586
   /**
580 587
    * Function that returns a view to show if there's an error.
581 588
    */