This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
70 lines (64 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React, { PureComponent } from 'react';
import { View, Platform, WebView, ActivityIndicator, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
export default class SVGImage extends PureComponent {
static propTypes = {
style: PropTypes.any,
source: PropTypes.shape({
uri: PropTypes.string,
}).isRequired,
showWebviewLoader: PropTypes.bool,
height: PropTypes.number,
};
static defaultProps = {
style: {},
source: { uri: '' },
showWebviewLoader: Platform.OS === 'android',
height: null,
};
renderLoader = () => (
<View style={[this.props.style, { flex: 1, alignItems: 'center', justifyContent: 'center' }]}>
<ActivityIndicator />
</View>
);
render() {
const { showWebviewLoader, source: { uri }, style, ...restOfProps } = this.props;
const { height, width } = StyleSheet.flatten(style || []);
const html = `
<!DOCTYPE html>\n
<html>
<head>
<style type="text/css">
img {
width: 100%;
height: 100%;
margin: 0 auto;
}
div {
width: ${width ? width+'px' : 'auto'};
height: ${height ? height+'px' : 'auto'};
}
body {
margin: 0;
}
</style>
</head>
<body>
<div>
<img src="${uri}" align="middle" />
</div>
</body>
</html>
`;
return (
<WebView
startInLoadingState={showWebviewLoader}
renderLoading={showWebviewLoader ? this.renderLoader : null}
scalesPageToFit={false}
style={style}
{...restOfProps}
source={{ html }}
/>
);
}
}