react-native-fast-image/FastImage.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

import React, { PropTypes, Component } from 'react'
2017-04-13 04:13:44 +00:00
import { requireNativeComponent, Image, View } from 'react-native'
const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource')
class FastImage extends Component {
setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps)
}
render() {
const { source, onError, onLoad, ...props } = this.props
// If there's no source or source uri just fallback to Image.
if (!source || !source.uri) {
return (
<Image {...props} source={source} onError={onError} onLoad={onLoad} />
)
}
const resolvedSource = resolveAssetSource(source)
2017-04-13 04:13:44 +00:00
return (
<FastImageView
ref={e => this._root = e}
{...props}
source={resolvedSource}
onFastImageError={onError}
onFastImageLoad={onLoad}
/>
2017-04-13 04:13:44 +00:00
)
}
}
FastImage.resizeMode = {
contain: 'contain',
cover: 'cover',
stretch: 'stretch',
center: 'center',
}
FastImage.priority = {
low: 'low',
normal: 'normal',
high: 'high',
}
const FastImageSourcePropType = PropTypes.shape({
uri: PropTypes.string,
headers: PropTypes.objectOf(PropTypes.string),
priority: PropTypes.oneOf(Object.keys(FastImage.priority)),
})
FastImage.propTypes = {
...View.propTypes,
source: FastImageSourcePropType,
2017-04-18 15:57:18 +00:00
onFastImageError: PropTypes.func,
onFastImageLoad: PropTypes.func,
2017-04-13 04:13:44 +00:00
}
FastImage.defaultProps = {
resizeMode: FastImage.resizeMode.cover,
onLoad: Function.prototype,
onError: Function.prototype,
}
const FastImageView = requireNativeComponent('FastImageView', FastImage, {
2017-04-18 15:57:18 +00:00
nativeOnly: { onFastImageError: true, onFastImageLoad: true },
2017-04-13 04:13:44 +00:00
})
export default FastImage