react-native-fast-image/FastImage.js

110 lines
2.4 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react'
import PropTypes from 'prop-types'
2017-06-20 01:50:55 +00:00
import {
Image,
NativeModules,
requireNativeComponent,
2017-06-20 01:50:55 +00:00
View,
} from 'react-native'
2017-04-13 04:13:44 +00:00
const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource')
const FastImageViewNativeModule = NativeModules.FastImageView
class FastImage extends Component {
setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps)
}
render() {
const {
source,
onLoadStart,
onProgress,
onLoad,
onError,
onLoadEnd,
...props
} = this.props
// If there's no source or source uri just fallback to Image.
if (!source || !source.uri) {
return (
<Image
ref={e => (this._root = e)}
{...props}
source={source}
onLoadStart={onLoadStart}
2017-07-04 01:58:24 +00:00
onProgress={onProgress}
onLoad={onLoad}
onError={onError}
onLoadEnd={onLoadEnd}
/>
)
}
const resolvedSource = resolveAssetSource(source)
2017-04-13 04:13:44 +00:00
return (
<FastImageView
ref={e => (this._root = e)}
{...props}
source={resolvedSource}
onFastImageLoadStart={onLoadStart}
2017-07-04 01:58:24 +00:00
onFastImageProgress={onProgress}
onFastImageLoad={onLoad}
onFastImageError={onError}
onFastImageLoadEnd={onLoadEnd}
/>
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',
}
FastImage.preload = sources => {
FastImageViewNativeModule.preload(sources)
2017-06-08 21:13:19 +00:00
}
2017-04-13 04:13:44 +00:00
FastImage.defaultProps = {
resizeMode: FastImage.resizeMode.cover,
}
const FastImageSourcePropType = PropTypes.shape({
uri: PropTypes.string,
headers: PropTypes.objectOf(PropTypes.string),
priority: PropTypes.oneOf(Object.keys(FastImage.priority)),
})
FastImage.propTypes = {
...View.propTypes,
2017-08-02 14:25:09 +00:00
source: PropTypes.oneOfType([FastImageSourcePropType, PropTypes.number]),
onLoadStart: PropTypes.func,
onProgress: PropTypes.func,
onLoad: PropTypes.func,
onError: PropTypes.func,
onLoadEnd: PropTypes.func,
2017-04-13 04:13:44 +00:00
}
const FastImageView = requireNativeComponent('FastImageView', FastImage, {
2017-07-04 01:58:24 +00:00
nativeOnly: {
onFastImageLoadStart: true,
2017-07-04 01:58:24 +00:00
onFastImageProgress: true,
onFastImageLoad: true,
onFastImageError: true,
onFastImageLoadEnd: true,
2017-07-04 01:58:24 +00:00
},
2017-04-13 04:13:44 +00:00
})
export default FastImage