Skip to content

Commit 4d03f40

Browse files
committed
🔧 chore: remove unused dependency and optimize image handling in the example
1 parent a073749 commit 4d03f40

File tree

3 files changed

+97
-55
lines changed

3 files changed

+97
-55
lines changed

example/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"gradle": "cd android && ./gradlew clean && ./gradlew build"
1414
},
1515
"dependencies": {
16-
"@baronha/react-native-image-grid": "^0.2.7",
1716
"@react-native-segmented-control/segmented-control": "2.5.7",
1817
"expo": "^54.0.27",
1918
"expo-build-properties": "~1.0.10",

example/src/index.tsx

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useCallback, useMemo, useState } from 'react'
22
import {
33
ActionSheetIOS,
44
Alert,
@@ -16,7 +16,6 @@ import {
1616
} from 'react-native'
1717

1818
import { StyleSheet } from 'react-native'
19-
import ImageGrid from '@baronha/react-native-image-grid'
2019
import {
2120
openPicker,
2221
PickerResult,
@@ -77,44 +76,49 @@ export default function App() {
7776
})
7877
}
7978

80-
const onPressImage = (_: PickerResult, index: number) => {
81-
openPreview(images, index, {
82-
onLongPress: () => {
83-
if (Platform.OS === 'ios') {
84-
ActionSheetIOS.showActionSheetWithOptions(
85-
{
86-
options: ['Download', 'Cancel'],
87-
cancelButtonIndex: 1,
88-
userInterfaceStyle: colorScheme ?? 'light',
89-
},
90-
(buttonIndex) => {
91-
if (buttonIndex === 0) {
92-
// Download
93-
} else if (buttonIndex === 1) {
94-
// Cancel
79+
console.log('images: ', images)
80+
81+
const onPressImage = useCallback(
82+
(_: PickerResult, index: number) => {
83+
openPreview(images, index, {
84+
onLongPress: () => {
85+
if (Platform.OS === 'ios') {
86+
ActionSheetIOS.showActionSheetWithOptions(
87+
{
88+
options: ['Download', 'Cancel'],
89+
cancelButtonIndex: 1,
90+
userInterfaceStyle: colorScheme ?? 'light',
91+
},
92+
(buttonIndex) => {
93+
if (buttonIndex === 0) {
94+
// Download
95+
} else if (buttonIndex === 1) {
96+
// Cancel
97+
}
9598
}
96-
}
97-
)
98-
} else {
99-
Alert.alert('Options', '', [
100-
{
101-
text: 'Cancel',
102-
style: 'cancel',
103-
onPress: () => {
104-
console.log('Cancel')
99+
)
100+
} else {
101+
Alert.alert('Options', '', [
102+
{
103+
text: 'Cancel',
104+
style: 'cancel',
105+
onPress: () => {
106+
console.log('Cancel')
107+
},
105108
},
106-
},
107-
{
108-
text: 'Download',
109-
onPress: () => {
110-
console.log('Download')
109+
{
110+
text: 'Download',
111+
onPress: () => {
112+
console.log('Download')
113+
},
111114
},
112-
},
113-
])
114-
}
115-
},
116-
})
117-
}
115+
])
116+
}
117+
},
118+
})
119+
},
120+
[images, colorScheme]
121+
)
118122

119123
const onPicker = async () => {
120124
try {
@@ -167,15 +171,40 @@ export default function App() {
167171
}
168172
}
169173

170-
const onRemovePhoto = (_: PickerResult, index: number) => {
171-
const data = [...images].filter((__, idx) => idx !== index)
172-
setImages(data)
173-
}
174+
const onRemovePhoto = useCallback(
175+
(_: PickerResult, index: number) => {
176+
const data = [...images].filter((__, idx) => idx !== index)
177+
setImages(data)
178+
},
179+
[images, setImages]
180+
)
174181

175182
const onChangeTheme = (value: string) => {
176183
Appearance.setColorScheme(value as ColorSchemeName)
177184
}
178185

186+
const renderImage = useMemo(() => {
187+
return (
188+
<View style={style.imageContainer}>
189+
{images.map((image, index) => (
190+
<TouchableOpacity
191+
key={index}
192+
onPress={() => onPressImage(image, index)}
193+
style={style.image}
194+
>
195+
<Image source={{ uri: image.path }} style={style.image} />
196+
<TouchableOpacity
197+
style={style.removeButton}
198+
onPress={() => onRemovePhoto(image, index)}
199+
>
200+
<Image source={assets.plusSign} style={style.trash} />
201+
</TouchableOpacity>
202+
</TouchableOpacity>
203+
))}
204+
</View>
205+
)
206+
}, [images, onPressImage, onRemovePhoto])
207+
179208
return (
180209
<Container>
181210
<SafeAreaView />
@@ -212,17 +241,7 @@ export default function App() {
212241
>
213242
{images.length > 0 ? (
214243
<>
215-
<ImageGrid
216-
dataImage={images}
217-
onPressImage={onPressImage}
218-
width={WIDTH - 6}
219-
sourceKey={'path'}
220-
videoKey={'type'}
221-
conditionCheckVideo={'video'}
222-
videoURLKey={'thumbnail'}
223-
showDelete
224-
onDeleteImage={onRemovePhoto}
225-
/>
244+
{renderImage}
226245
<Button style={style.buttonOpen} onPress={onCrop}>
227246
Open Cropping
228247
</Button>
@@ -618,6 +637,8 @@ export default function App() {
618637
)
619638
}
620639

640+
const numberOfColumn = 3
641+
621642
const style = StyleSheet.create({
622643
titleView: {
623644
padding: 16,
@@ -686,7 +707,6 @@ const style = StyleSheet.create({
686707

687708
des: {
688709
fontSize: 12,
689-
// marginBottom: 12,
690710
},
691711
keyboardAvoidingView: {
692712
flex: 1,
@@ -709,4 +729,28 @@ const style = StyleSheet.create({
709729
openPicker: {
710730
flex: 1,
711731
},
732+
imageContainer: {
733+
flexDirection: 'row',
734+
flexWrap: 'wrap',
735+
gap: 6,
736+
paddingHorizontal: 6,
737+
},
738+
image: {
739+
width: (WIDTH - 24) / numberOfColumn,
740+
height: (WIDTH - 24) / numberOfColumn,
741+
},
742+
743+
removeButton: {
744+
position: 'absolute',
745+
top: 6,
746+
right: 6,
747+
backgroundColor: 'rgba(0, 0, 0, 0.9)',
748+
borderRadius: 100,
749+
padding: 6,
750+
},
751+
trash: {
752+
width: 16,
753+
height: 16,
754+
transform: [{ rotate: '45deg' }],
755+
},
712756
})

ios/HybridMultipleImagePicker+Result.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77

88
import HXPhotoPicker
9-
// import Photos
109

1110
extension HybridMultipleImagePicker {
1211
func getResult(_ asset: PhotoAsset) async throws -> PickerResult {

0 commit comments

Comments
 (0)