|
1 | | -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { |
3 | 3 | Animated, |
4 | 4 | Image as ImageNative, |
@@ -29,102 +29,100 @@ type ImageState = { |
29 | 29 | placeholderOpacity: Animated.Value; |
30 | 30 | }; |
31 | 31 |
|
32 | | -export default class TransitionImage extends React.Component<ImageProps & Partial<ImageProps>, ImageState> { |
33 | | - static displayName = 'Image'; |
34 | | - static getSize = ImageNative.getSize; |
35 | | - static getSizeWithHeaders = ImageNative.getSizeWithHeaders; |
36 | | - static prefetch = ImageNative.prefetch; |
37 | | - static abortPrefetch = ImageNative.abortPrefetch; |
38 | | - static queryCache = ImageNative.queryCache; |
39 | | - static resolveAssetSource = ImageNative.resolveAssetSource; |
40 | | - |
41 | | - state = { |
| 32 | +export default function TransitionImage(props: ImageProps & Partial<ImageProps>) { |
| 33 | + const [state, _] = useState<ImageState>({ |
42 | 34 | placeholderOpacity: new Animated.Value(1), |
43 | | - }; |
| 35 | + }); |
44 | 36 |
|
45 | | - onLoad = (e: any) => { |
46 | | - const { transition, onLoad, transitionDuration } = this.props; |
| 37 | + const onLoad = (e: any) => { |
| 38 | + const { transition, onLoad, transitionDuration } = props; |
47 | 39 | if (!transition) { |
48 | | - this.state.placeholderOpacity.setValue(0); |
| 40 | + state.placeholderOpacity.setValue(0); |
49 | 41 | return; |
50 | 42 | } |
51 | 43 |
|
52 | | - Animated.timing(this.state.placeholderOpacity, { |
| 44 | + Animated.timing(state.placeholderOpacity, { |
53 | 45 | toValue: 0, |
54 | 46 | duration: transitionDuration, |
55 | 47 | useNativeDriver: true, |
56 | 48 | }).start(); |
57 | 49 | onLoad && onLoad(e); |
58 | 50 | }; |
59 | 51 |
|
60 | | - render() { |
61 | | - const { |
62 | | - onPress, |
63 | | - onLongPress, |
64 | | - Component = onPress || onLongPress ? TouchableOpacity : View, |
65 | | - placeholderStyle, |
66 | | - PlaceholderContent, |
67 | | - containerStyle, |
68 | | - childrenContainerStyle = null, |
69 | | - style = {}, |
70 | | - ImageComponent = ImageNative, |
71 | | - children, |
72 | | - ...attributes |
73 | | - } = this.props; |
| 52 | + const { |
| 53 | + onPress, |
| 54 | + onLongPress, |
| 55 | + Component = onPress || onLongPress ? TouchableOpacity : View, |
| 56 | + placeholderStyle, |
| 57 | + PlaceholderContent, |
| 58 | + containerStyle, |
| 59 | + childrenContainerStyle = null, |
| 60 | + style = {}, |
| 61 | + ImageComponent = ImageNative, |
| 62 | + children, |
| 63 | + ...attributes |
| 64 | + } = props; |
74 | 65 |
|
75 | | - const hasImage = Boolean(attributes.source); |
76 | | - const { width, height, ...styleProps } = StyleSheet.flatten(style); |
| 66 | + const hasImage = Boolean(attributes.source); |
| 67 | + const { width, height, ...styleProps } = StyleSheet.flatten(style); |
77 | 68 |
|
78 | | - return ( |
79 | | - <Component |
80 | | - onPress={onPress} |
81 | | - onLongPress={onLongPress} |
82 | | - accessibilityIgnoresInvertColors={true} |
83 | | - style={StyleSheet.flatten([styles.container, containerStyle])} |
84 | | - > |
85 | | - <ImageComponent |
86 | | - testID="RNE__Image" |
87 | | - transition={true} |
88 | | - transitionDuration={360} |
89 | | - {...attributes} |
90 | | - onLoad={this.onLoad} |
91 | | - style={StyleSheet.flatten([ |
92 | | - StyleSheet.absoluteFill, |
93 | | - { |
94 | | - width: width, |
95 | | - height: height, |
96 | | - } as StyleProp<ImageStyle>, |
97 | | - styleProps, |
98 | | - ])} |
99 | | - /> |
| 69 | + return ( |
| 70 | + <Component |
| 71 | + onPress={onPress} |
| 72 | + onLongPress={onLongPress} |
| 73 | + accessibilityIgnoresInvertColors={true} |
| 74 | + style={StyleSheet.flatten([styles.container, containerStyle])} |
| 75 | + > |
| 76 | + <ImageComponent |
| 77 | + testID="RNE__Image" |
| 78 | + transition={true} |
| 79 | + transitionDuration={360} |
| 80 | + {...attributes} |
| 81 | + onLoad={onLoad} |
| 82 | + style={StyleSheet.flatten([ |
| 83 | + StyleSheet.absoluteFill, |
| 84 | + { |
| 85 | + width: width, |
| 86 | + height: height, |
| 87 | + } as StyleProp<ImageStyle>, |
| 88 | + styleProps, |
| 89 | + ])} |
| 90 | + /> |
100 | 91 |
|
101 | | - <Animated.View |
102 | | - pointerEvents={hasImage ? 'none' : 'auto'} |
103 | | - accessibilityElementsHidden={hasImage} |
104 | | - importantForAccessibility={hasImage ? 'no-hide-descendants' : 'yes'} |
105 | | - style={[ |
106 | | - styles.placeholderContainer, |
107 | | - { |
108 | | - opacity: hasImage ? this.state.placeholderOpacity : 1, |
109 | | - }, |
110 | | - ]} |
| 92 | + <Animated.View |
| 93 | + pointerEvents={hasImage ? 'none' : 'auto'} |
| 94 | + accessibilityElementsHidden={hasImage} |
| 95 | + importantForAccessibility={hasImage ? 'no-hide-descendants' : 'yes'} |
| 96 | + style={[ |
| 97 | + styles.placeholderContainer, |
| 98 | + { |
| 99 | + opacity: hasImage ? state.placeholderOpacity : 1, |
| 100 | + }, |
| 101 | + ]} |
| 102 | + > |
| 103 | + <View |
| 104 | + testID="RNE__Image__placeholder" |
| 105 | + style={StyleSheet.flatten([style, styles.placeholder, placeholderStyle])} |
111 | 106 | > |
112 | | - <View |
113 | | - testID="RNE__Image__placeholder" |
114 | | - style={StyleSheet.flatten([style, styles.placeholder, placeholderStyle])} |
115 | | - > |
116 | | - {PlaceholderContent} |
117 | | - </View> |
118 | | - </Animated.View> |
119 | | - |
120 | | - <View testID="RNE__Image__children__container" style={childrenContainerStyle ?? style}> |
121 | | - {children} |
| 107 | + {PlaceholderContent} |
122 | 108 | </View> |
123 | | - </Component> |
124 | | - ); |
125 | | - } |
| 109 | + </Animated.View> |
| 110 | + |
| 111 | + <View testID="RNE__Image__children__container" style={childrenContainerStyle ?? style}> |
| 112 | + {children} |
| 113 | + </View> |
| 114 | + </Component> |
| 115 | + ); |
126 | 116 | } |
127 | 117 |
|
| 118 | +TransitionImage.displayName = 'Image'; |
| 119 | +TransitionImage.getSize = ImageNative.getSize; |
| 120 | +TransitionImage.getSizeWithHeaders = ImageNative.getSizeWithHeaders; |
| 121 | +TransitionImage.prefetch = ImageNative.prefetch; |
| 122 | +TransitionImage.abortPrefetch = ImageNative.abortPrefetch; |
| 123 | +TransitionImage.queryCache = ImageNative.queryCache; |
| 124 | +TransitionImage.resolveAssetSource = ImageNative.resolveAssetSource; |
| 125 | + |
128 | 126 | const styles = StyleSheet.create({ |
129 | 127 | container: { |
130 | 128 | backgroundColor: 'transparent', |
|
0 commit comments