Skip to content

Commit 1be6a4e

Browse files
committed
fix(ImagePicker): 修复删除不立马刷新问题
1 parent ec77d4f commit 1be6a4e

File tree

2 files changed

+59
-78
lines changed

2 files changed

+59
-78
lines changed

packages/react-native-image-picker/src/ImagePicker/index.tsx

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
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';
123
import { 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';
145
import { useTheme } from '@shopify/restyle';
156
import useImagePicker from './useImagePicker';
7+
168
export 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
}

packages/react-native-image-picker/src/ImagePicker/useImagePicker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useEffect, useRef } from 'react';
2-
import { Keyboard, PermissionsAndroid, Platform, Animated } from 'react-native';
1+
import { useEffect, useState } from 'react';
2+
import { Keyboard, PermissionsAndroid, Platform } from 'react-native';
33
import {
44
CameraOptions,
55
ImagePickerResponse,
@@ -45,7 +45,9 @@ export default function useImagePicker({
4545
/** loading */
4646
const [loading, setLoading] = useSafeState(false);
4747
/** 预览照片地址 */
48-
const [previewSrc, setPreviewSrc] = useSafeState<string | undefined>(''); // 选中的图片下标
48+
const [previewSrc, setPreviewSrc] = useSafeState<string | undefined>(undefined); // 选中的图片下标
49+
// 刷新
50+
const [refresh, setRefresh] = useState(false);
4951

5052
useEffect(() => {
5153
if (value && value.length > 0) {
@@ -153,10 +155,18 @@ export default function useImagePicker({
153155
setPreviewVisibleTrue();
154156
};
155157

158+
// 关闭预览照片
159+
const closePreviewImage = () => {
160+
setPreviewSrc(undefined);
161+
setPreviewVisibleFalse();
162+
};
163+
156164
// 删除照片
157165
const deleteImage = (key: number) => {
158166
currentImgSource.splice(key, 1);
159167
setCurrentImgSource(currentImgSource);
168+
// 刷新页面
169+
setRefresh(!refresh);
160170
uploadFinish?.('');
161171
};
162172

@@ -173,12 +183,12 @@ export default function useImagePicker({
173183
launchLibrary,
174184
launchCamera,
175185
launchVisible,
186+
previewVisible,
176187
previewImage,
188+
closePreviewImage,
177189
deleteImage,
178190
handlePress,
179-
previewVisible,
180191
setLaunchVisibleFalse,
181-
setPreviewVisibleFalse,
182-
setPreviewSrc,
192+
refresh,
183193
};
184194
}

0 commit comments

Comments
 (0)