1- import React , { PropsWithChildren } from 'react' ;
2- import {
3- View ,
4- StyleSheet ,
5- Rationale ,
6- TouchableOpacity ,
7- ActivityIndicator ,
8- Dimensions ,
9- Image ,
10- Text ,
11- } from 'react-native' ;
1+ import React , { PropsWithChildren , useMemo } from 'react' ;
2+ import { View , StyleSheet , Rationale , TouchableOpacity , Dimensions , Image } from 'react-native' ;
123import { CameraOptions , ImagePickerResponse } from 'react-native-image-picker' ;
13- import { Theme , Flex , ActionSheet , ActionSheetItem , MaskLayer , TransitionImage , Icon } from '@uiw/react-native' ;
4+ import { Theme , Flex , ActionSheet , ActionSheetItem , MaskLayer , TransitionImage , Icon , Text } from '@uiw/react-native' ;
145import { useTheme } from '@shopify/restyle' ;
156import useImagePicker from './useImagePicker' ;
7+
168export interface File {
179 fileName : string ;
1810 fileType : string ;
@@ -89,6 +81,7 @@ const ImagePicker = ({
8981 borderColor : theme . colors . border ,
9082 backgroundColor : theme . colors . mask ,
9183 } ) ;
84+
9285 const {
9386 currentImgSource,
9487 previewSrc,
@@ -97,12 +90,11 @@ const ImagePicker = ({
9790 launchCamera,
9891 launchVisible,
9992 previewImage,
93+ closePreviewImage,
10094 deleteImage,
10195 handlePress,
10296 previewVisible,
10397 setLaunchVisibleFalse,
104- setPreviewVisibleFalse,
105- setPreviewSrc,
10698 } = useImagePicker ( {
10799 value,
108100 options,
@@ -115,6 +107,35 @@ const ImagePicker = ({
115107 cameraRationale,
116108 libraryRationale,
117109 } ) ;
110+
111+ const pictureList = useMemo ( ( ) => {
112+ if ( showUploadImg && currentImgSource && currentImgSource . length > 0 ) {
113+ return currentImgSource . map ( ( item , index ) => (
114+ < Flex . Item key = { index } >
115+ < View style = { styles . box } >
116+ < View style = { { justifyContent : 'center' , alignItems : 'center' , width, height } } >
117+ < TransitionImage
118+ source = { { uri : item } }
119+ style = { { width, height, borderRadius : 2 } }
120+ PlaceholderContent = { < Text color = "text" > 加载失败</ Text > }
121+ transition = { true }
122+ />
123+ < View style = { styles . previewBox } >
124+ < TouchableOpacity disabled = { loading } onPress = { ( ) => previewImage ( index ) } style = { styles . previewIcon } >
125+ < Icon name = "eye" color = { theme . colors . primary_background || '#3578e5' } size = { 16 } />
126+ </ TouchableOpacity >
127+ < TouchableOpacity disabled = { loading } onPress = { ( ) => deleteImage ( index ) } style = { styles . previewIcon } >
128+ < Icon name = "delete" color = { theme . colors . primary_background || '#3578e5' } size = { 16 } />
129+ </ TouchableOpacity >
130+ </ View >
131+ </ View >
132+ </ View >
133+ </ Flex . Item >
134+ ) ) ;
135+ }
136+ return null ;
137+ } , [ showUploadImg , JSON . stringify ( currentImgSource ) ] ) ;
138+
118139 return (
119140 < View >
120141 < Flex justify = "start" wrap = "wrap" >
@@ -130,67 +151,13 @@ const ImagePicker = ({
130151 </ TouchableOpacity >
131152 </ View >
132153 </ Flex . Item >
133- { showUploadImg &&
134- currentImgSource &&
135- currentImgSource . length > 0 &&
136- currentImgSource . map ( ( item , key ) => (
137- < Flex . Item key = { key } >
138- < View style = { styles . box } >
139- < TouchableOpacity
140- activeOpacity = { 0.5 }
141- disabled = { true }
142- style = { { justifyContent : 'center' , alignItems : 'center' , width, height } }
143- >
144- < TransitionImage
145- source = { { uri : item } }
146- style = { { width, height, borderRadius : 2 } }
147- PlaceholderContent = { < ActivityIndicator /> }
148- transition = { true }
149- />
150- < View style = { [ styles . btns , { height : height , width : width } ] } >
151- < TouchableOpacity
152- onPress = { ( ) => previewImage ( key ) }
153- style = { [
154- styles . delete ,
155- {
156- width : width * 0.375 ,
157- height : height * 0.375 ,
158- } ,
159- ] }
160- >
161- < Icon name = "eye" color = { theme . colors . primary_background || '#3578e5' } size = { 16 } />
162- </ TouchableOpacity >
163- < TouchableOpacity
164- onPress = { ( ) => deleteImage ( key ) }
165- style = { [
166- styles . delete ,
167- {
168- width : width * 0.375 ,
169- height : height * 0.375 ,
170- } ,
171- ] }
172- >
173- < Icon name = "delete" color = { theme . colors . primary_background || '#3578e5' } size = { 16 } />
174- </ TouchableOpacity >
175- </ View >
176- </ TouchableOpacity >
177- </ View >
178- </ Flex . Item >
179- ) ) }
154+ { pictureList }
180155 </ Flex >
181156 < ActionSheet onCancel = { setLaunchVisibleFalse } visible = { launchVisible } style = { { zIndex : 99 } } >
182157 < ActionSheetItem onPress = { launchLibrary } > { launchLibraryText } </ ActionSheetItem >
183158 < ActionSheetItem onPress = { launchCamera } > { launchCameraText } </ ActionSheetItem >
184159 </ ActionSheet >
185- < MaskLayer
186- visible = { previewVisible }
187- style = { { zIndex : 999 } }
188- onDismiss = { ( ) => {
189- setPreviewVisibleFalse ( ) ;
190- setPreviewSrc ( '' ) ;
191- } }
192- opacity = { 0.9 }
193- >
160+ < MaskLayer visible = { previewVisible } style = { { zIndex : 999 } } onDismiss = { closePreviewImage } opacity = { 0.9 } >
194161 < View style = { styles . content } >
195162 < Image style = { styles . image } source = { { uri : previewSrc } } />
196163 </ View >
@@ -222,17 +189,21 @@ function createStyles({ width = 100, height = 100, borderColor = '#CCCCCC', back
222189 height : '100%' ,
223190 resizeMode : 'contain' ,
224191 } ,
225- btns : {
192+ previewBox : {
226193 position : 'absolute' ,
227194 left : 0 ,
228195 top : 0 ,
229196 flexDirection : 'row' ,
230197 alignItems : 'center' ,
231198 justifyContent : 'center' ,
199+ height : height ,
200+ width : width ,
232201 } ,
233- delete : {
202+ previewIcon : {
234203 alignItems : 'center' ,
235204 justifyContent : 'center' ,
205+ width : width * 0.375 ,
206+ height : height * 0.375 ,
236207 } ,
237208 } ) ;
238209}
0 commit comments