Skip to content

Commit 1a749c9

Browse files
committed
Add support for any source with method/headers when newWindow is falsy
1 parent 9057c53 commit 1a749c9

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

docs/stories/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import * as html from './html';
33
import * as uri from './uri';
4+
import * as method from './method';
45

56
import { storiesOf } from '@storybook/react';
67

@@ -9,3 +10,5 @@ storiesOf('HTML source', module).add('basic', html.basic);
910
storiesOf('URI source', module)
1011
.add('basic', uri.basic)
1112
.add('onMessage', uri.onMessage);
13+
14+
storiesOf('With method', module).add('basic', method.basic);

docs/stories/method.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import WebView from 'WebView';
3+
import { text } from '@storybook/addon-knobs';
4+
5+
export const basic = () => (
6+
<WebView
7+
source={{
8+
uri: text('URI', 'https://react-native-web-community.github.io/react-native-web-webview/'),
9+
method: text('Method', 'GET'),
10+
}}
11+
/>
12+
);

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import React, { Component } from 'react';
33
import { StyleSheet, View, ActivityIndicator } from 'react-native';
44

55
export default class extends Component {
6+
state = { html: null };
7+
68
constructor(props) {
79
super(props);
810

@@ -36,6 +38,12 @@ export default class extends Component {
3638
source.method
3739
);
3840
}
41+
} else {
42+
const { uri, ...options } = source;
43+
const baseUrl = uri.substr(0, uri.lastIndexOf('/') + 1);
44+
fetch(uri, options)
45+
.then(response => response.text())
46+
.then(html => this.setState({ html: `<base href="${baseUrl}" />` + html }));
3947
}
4048
}
4149
}
@@ -63,10 +71,11 @@ export default class extends Component {
6371
);
6472
}
6573

74+
const { source } = this.props;
6675
return (
6776
<iframe
68-
src={this.props.source.uri}
69-
srcDoc={this.props.source.html}
77+
src={!source.method ? source.uri : undefined}
78+
srcDoc={this.state.html || source.html}
7079
style={{ width: '100%', height: '100%', border: 0 }}
7180
allowFullScreen
7281
allowpaymentrequest="true"

0 commit comments

Comments
 (0)