Skip to content

Commit 3f45e98

Browse files
committed
Move example to top
1 parent 42dc769 commit 3f45e98

File tree

1 file changed

+75
-74
lines changed

1 file changed

+75
-74
lines changed

packages/rn-tester/js/examples/DragAndDrop/DragAndDropExample.js

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,77 @@ const styles = StyleSheet.create({
2424
},
2525
});
2626

27+
28+
function DragDropView(): React.Node {
29+
// $FlowFixMe[missing-empty-array-annot]
30+
const [log, setLog] = React.useState([]);
31+
const appendLog = (line: string) => {
32+
const limit = 3;
33+
let newLog = log.slice(0, limit - 1);
34+
newLog.unshift(line);
35+
setLog(newLog);
36+
};
37+
const [imageUri, setImageUri] = React.useState(
38+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==',
39+
);
40+
const [isDraggingOver, setIsDraggingOver] = React.useState(false);
41+
42+
return (
43+
<>
44+
<View
45+
draggedTypes={['fileUrl', 'image']}
46+
onDragEnter={e => {
47+
appendLog('onDragEnter');
48+
setIsDraggingOver(true);
49+
}}
50+
onDragLeave={e => {
51+
appendLog('onDragLeave');
52+
setIsDraggingOver(false);
53+
}}
54+
onDrop={e => {
55+
appendLog('onDrop');
56+
setIsDraggingOver(false);
57+
if (e.nativeEvent.dataTransfer.files && e.nativeEvent.dataTransfer.files[0]) {
58+
setImageUri(e.nativeEvent.dataTransfer.files[0].uri);
59+
}
60+
}}
61+
style={{
62+
height: 150,
63+
backgroundColor: isDraggingOver ? '#e3f2fd' : '#f0f0f0',
64+
borderWidth: 2,
65+
borderColor: isDraggingOver ? '#2196f3' : '#0066cc',
66+
borderStyle: 'dashed',
67+
borderRadius: 8,
68+
justifyContent: 'center',
69+
alignItems: 'center',
70+
marginVertical: 10,
71+
}}>
72+
<Text style={{color: isDraggingOver ? '#1976d2' : '#666', fontSize: 14}}>
73+
{isDraggingOver ? 'Release to drop' : 'Drop an image or file here'}
74+
</Text>
75+
</View>
76+
<View style={{flexDirection: 'row', gap: 10, alignItems: 'flex-start'}}>
77+
<View style={{flex: 1}}>
78+
<Text style={{fontWeight: 'bold', marginBottom: 4}}>Event Log:</Text>
79+
<Text style={{height: 90}}>{log.join('\n')}</Text>
80+
</View>
81+
<View style={{flex: 1}}>
82+
<Text style={{fontWeight: 'bold', marginBottom: 4}}>Dropped Image:</Text>
83+
<Image
84+
source={{uri: imageUri}}
85+
style={{
86+
width: 128,
87+
height: 128,
88+
borderWidth: 1,
89+
borderColor: '#ccc',
90+
}}
91+
/>
92+
</View>
93+
</View>
94+
</>
95+
);
96+
}
97+
2798
function OnDragEnterOnDragLeaveOnDrop(): React.Node {
2899
// $FlowFixMe[missing-empty-array-annot]
29100
const [log, setLog] = React.useState([]);
@@ -117,94 +188,24 @@ function OnPaste(): React.Node {
117188
);
118189
}
119190

120-
function OnDropView(): React.Node {
121-
// $FlowFixMe[missing-empty-array-annot]
122-
const [log, setLog] = React.useState([]);
123-
const appendLog = (line: string) => {
124-
const limit = 3;
125-
let newLog = log.slice(0, limit - 1);
126-
newLog.unshift(line);
127-
setLog(newLog);
128-
};
129-
const [imageUri, setImageUri] = React.useState(
130-
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==',
131-
);
132-
const [isDraggingOver, setIsDraggingOver] = React.useState(false);
133-
134-
return (
135-
<>
136-
<View
137-
draggedTypes={['fileUrl', 'image']}
138-
onDragEnter={e => {
139-
appendLog('onDragEnter');
140-
setIsDraggingOver(true);
141-
}}
142-
onDragLeave={e => {
143-
appendLog('onDragLeave');
144-
setIsDraggingOver(false);
145-
}}
146-
onDrop={e => {
147-
appendLog('onDrop');
148-
setIsDraggingOver(false);
149-
if (e.nativeEvent.dataTransfer.files && e.nativeEvent.dataTransfer.files[0]) {
150-
setImageUri(e.nativeEvent.dataTransfer.files[0].uri);
151-
}
152-
}}
153-
style={{
154-
height: 150,
155-
backgroundColor: isDraggingOver ? '#e3f2fd' : '#f0f0f0',
156-
borderWidth: 2,
157-
borderColor: isDraggingOver ? '#2196f3' : '#0066cc',
158-
borderStyle: 'dashed',
159-
borderRadius: 8,
160-
justifyContent: 'center',
161-
alignItems: 'center',
162-
marginVertical: 10,
163-
}}>
164-
<Text style={{color: isDraggingOver ? '#1976d2' : '#666', fontSize: 14}}>
165-
{isDraggingOver ? 'Release to drop' : 'Drop an image or file here'}
166-
</Text>
167-
</View>
168-
<View style={{flexDirection: 'row', gap: 10, alignItems: 'flex-start'}}>
169-
<View style={{flex: 1}}>
170-
<Text style={{fontWeight: 'bold', marginBottom: 4}}>Event Log:</Text>
171-
<Text style={{height: 90}}>{log.join('\n')}</Text>
172-
</View>
173-
<View style={{flex: 1}}>
174-
<Text style={{fontWeight: 'bold', marginBottom: 4}}>Dropped Image:</Text>
175-
<Image
176-
source={{uri: imageUri}}
177-
style={{
178-
width: 128,
179-
height: 128,
180-
borderWidth: 1,
181-
borderColor: '#ccc',
182-
}}
183-
/>
184-
</View>
185-
</View>
186-
</>
187-
);
188-
}
189-
190191
exports.title = 'Drag and Drop Events';
191192
exports.category = 'UI';
192193
exports.description = 'Demonstrates onDragEnter, onDragLeave, onDrop, and onPaste event handling in TextInput.';
193194
exports.examples = [
194195
{
195-
title: 'onDrop with View - Drop Image',
196+
title: 'Drag and Drop (View)',
196197
render: function (): React.Node {
197-
return <OnDropView />;
198+
return <DragDropView />;
198199
},
199200
},
200201
{
201-
title: 'onDragEnter, onDragLeave and onDrop - Single- & MultiLineTextInput',
202+
title: 'Drag and Drop (TextInput)',
202203
render: function (): React.Node {
203204
return <OnDragEnterOnDragLeaveOnDrop />;
204205
},
205206
},
206207
{
207-
title: 'onPaste - MultiLineTextInput',
208+
title: 'onPaste (MultiLineTextInput)',
208209
render: function (): React.Node {
209210
return <OnPaste />;
210211
},

0 commit comments

Comments
 (0)