1
1
import _ from 'lodash' ;
2
- import PropTypes from 'prop-types ' ;
3
- import React from 'react' ;
2
+ import React , { PureComponent } from 'react ' ;
3
+ // @ts -ignore
4
4
import hoistNonReactStatic from 'hoist-non-react-statics' ;
5
- import { Image as RNImage , StyleSheet , ImageBackground } from 'react-native' ;
6
- import { Constants } from '../../helpers' ;
7
- import { PureBaseComponent } from '../../commons' ;
5
+ import { Image as RNImage , ImageProps as RNImageProps , StyleSheet , ImageBackground } from 'react-native' ;
6
+ import Constants from '../../helpers/Constants' ;
7
+ import { asBaseComponent , forwardRef , ForwardRefInjectedProps } from '../../commons/new' ;
8
+ // @ts -ignore
8
9
import Assets from '../../assets' ;
9
- import Overlay from '../overlay' ;
10
+ import Overlay , { OverlayTypeType } from '../overlay' ;
11
+
12
+ type ImageProps = RNImageProps & {
13
+ /**
14
+ * custom source transform handler for manipulating the image source (great for size control)
15
+ */
16
+ sourceTransformer ?: Function ;
17
+ /**
18
+ * if provided image source will be driven from asset name
19
+ */
20
+ assetName ?: string ;
21
+ /**
22
+ * the asset group, default is "icons"
23
+ */
24
+ assetGroup ?: string ;
25
+ /**
26
+ * the asset tint
27
+ */
28
+ tintColor ?: string ;
29
+ /**
30
+ * whether the image should flip horizontally on RTL locals
31
+ */
32
+ supportRTL ?: boolean ;
33
+ /**
34
+ * Show image as a cover, full width, image (according to aspect ratio, default: 16:8)
35
+ */
36
+ cover ?: boolean ;
37
+ /**
38
+ * The aspect ratio for the image
39
+ */
40
+ aspectRatio ?: number ;
41
+ /**
42
+ * The type of overly to place on top of the image. Note: the image MUST have proper size, see examples in:
43
+ * https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/OverlaysScreen.js
44
+ */
45
+ overlayType ?: OverlayTypeType ;
46
+ /**
47
+ * Pass a custom color for the overlay
48
+ */
49
+ overlayColor ?: string ;
50
+ /**
51
+ * Render an overlay with custom content
52
+ */
53
+ customOverlayContent ?: JSX . Element ;
54
+ } ;
55
+
56
+ type Props = ImageProps & ForwardRefInjectedProps ;
10
57
11
58
/**
12
59
* @description : Image wrapper with extra functionality like source transform and assets support
13
60
* @extends : Image
14
61
* @extendslink : https://facebook.github.io/react-native/docs/image.html
15
62
*/
16
- class Image extends PureBaseComponent {
63
+ class Image extends PureComponent < Props > {
17
64
static displayName = 'Image' ;
18
65
19
- static propTypes = {
20
- /**
21
- * custom source transform handler for manipulating the image source (great for size control)
22
- */
23
- sourceTransformer : PropTypes . func ,
24
- /**
25
- * if provided image source will be driven from asset name
26
- */
27
- assetName : PropTypes . string ,
28
- /**
29
- * the asset group, default is "icons"
30
- */
31
- assetGroup : PropTypes . string ,
32
- /**
33
- * the asset tint
34
- */
35
- tintColor : PropTypes . string ,
36
- /**
37
- * whether the image should flip horizontally on RTL locals
38
- */
39
- supportRTL : PropTypes . bool ,
40
- /**
41
- * Show image as a cover, full width, image (according to aspect ratio, default: 16:8)
42
- */
43
- cover : PropTypes . bool ,
44
- /**
45
- * The aspect ratio for the image
46
- */
47
- aspectRatio : PropTypes . number ,
48
- /**
49
- * The type of overly to place on top of the image. Note: the image MUST have proper size, see examples in:
50
- * https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/OverlaysScreen.js
51
- */
52
- overlayType : Overlay . propTypes . type ,
53
- /**
54
- * Pass a custom color for the overlay
55
- */
56
- overlayColor : PropTypes . string ,
57
- /**
58
- * Render an overlay with custom content
59
- */
60
- customOverlayContent : PropTypes . element
61
- } ;
62
-
63
66
static defaultProps = {
64
67
assetGroup : 'icons'
65
68
} ;
66
69
67
- static overlayTypes = Overlay . overlayTypes ;
70
+ public static overlayTypes = Overlay . overlayTypes ;
71
+
72
+ sourceTransformer ?: Function ;
68
73
69
- constructor ( props ) {
74
+ constructor ( props : Props ) {
70
75
super ( props ) ;
71
76
72
- this . sourceTransformer = this . getThemeProps ( ) . sourceTransformer ;
77
+ this . sourceTransformer = this . props . sourceTransformer ;
73
78
}
74
79
75
80
isGif ( ) {
@@ -99,6 +104,7 @@ class Image extends PureBaseComponent {
99
104
100
105
const { source} = this . props ;
101
106
if ( _ . get ( source , 'uri' ) === null || _ . get ( source , 'uri' ) === '' ) {
107
+ // @ts -ignore
102
108
return { ...source , uri : undefined } ;
103
109
}
104
110
@@ -116,13 +122,16 @@ class Image extends PureBaseComponent {
116
122
overlayType,
117
123
overlayColor,
118
124
customOverlayContent,
125
+ forwardedRef,
119
126
...others
120
- } = this . getThemeProps ( ) ;
127
+ } = this . props ;
121
128
const shouldFlipRTL = supportRTL && Constants . isRTL ;
122
129
const ImageView = this . shouldUseImageBackground ( ) ? ImageBackground : RNImage ;
123
130
124
131
return (
132
+ // @ts -ignore
125
133
< ImageView
134
+ ref = { forwardedRef }
126
135
style = { [
127
136
{ tintColor} ,
128
137
shouldFlipRTL && styles . rtlFlipped ,
@@ -137,7 +146,7 @@ class Image extends PureBaseComponent {
137
146
source = { source }
138
147
>
139
148
{ ( overlayType || customOverlayContent ) && (
140
- < Overlay style = { style } type = { overlayType } color = { overlayColor } customContent = { customOverlayContent } />
149
+ < Overlay type = { overlayType } color = { overlayColor } customContent = { customOverlayContent } />
141
150
) }
142
151
</ ImageView >
143
152
) ;
@@ -158,4 +167,5 @@ const styles = StyleSheet.create({
158
167
} ) ;
159
168
160
169
hoistNonReactStatic ( Image , RNImage ) ;
161
- export default Image ;
170
+ export { Image } ;
171
+ export default asBaseComponent < ImageProps > ( forwardRef ( Image ) ) ;
0 commit comments