forked from meliorence/react-native-render-html
/
HTMLImage.js
41 lines (33 loc) · 1.14 KB
/
HTMLImage.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
import React, { Component, PropTypes } from 'react';
import { Image } from 'react-native';
const DEFAULT_WIDTH = 100;
const DEFAULT_HEIGHT = 100;
export default class HTMLImage extends Component {
constructor (props) {
super(props);
this.state = {
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT
};
}
static propTypes = {
source: PropTypes.object.isRequired,
style: Image.propTypes.style,
imagesMaxWidth: PropTypes.number
}
componentDidMount () {
const { source, imagesMaxWidth } = this.props;
Image.getSize(source.uri, (width, height) => {
this.setState({
width: imagesMaxWidth && width > imagesMaxWidth ? imagesMaxWidth : width,
height: imagesMaxWidth && width > imagesMaxWidth ? height / (width / imagesMaxWidth) : height
});
});
}
render () {
const { source, style, passProps } = this.props;
return (
<Image source={source} style={[style, {width: this.state.width, height: this.state.height, resizeMode: 'cover'}]} {...passProps} />
);
}
}