77 type DraggableStackProps ,
88 type ObjectWithId ,
99} from '@mgcrea/react-native-dnd/src' ;
10- import React , { useCallback , useState , type FunctionComponent } from 'react' ;
10+ import React , { useState , type FunctionComponent } from 'react' ;
1111import { Button , StyleSheet , Text , View } from 'react-native' ;
1212import { runOnUI } from 'react-native-reanimated' ;
1313
@@ -23,15 +23,11 @@ export const DraggableStackExample: FunctionComponent = () => {
2323 const [ fontSize , setFontSize ] = useState ( 32 ) ;
2424 const ref = React . useRef < DraggableStackHandle > ( null ) ;
2525
26- const onStackOrderChange : DraggableStackProps [ 'onOrderChange' ] = useCallback (
27- ( order : UniqueIdentifier [ ] ) => {
28- console . log ( 'onStackOrderChange' , order ) ;
29- setTimeout ( ( ) => {
30- // setItems(items => order.map(id => items.find(item => item.id === id)!));
31- } , 1000 ) ;
32- } ,
33- [ ] ,
34- ) ;
26+ const onStackOrderChange : DraggableStackProps [ 'onOrderChange' ] = (
27+ order : UniqueIdentifier [ ] ,
28+ ) => {
29+ console . log ( 'onStackOrderChange' , order ) ;
30+ } ;
3531 const onStackOrderUpdate : DraggableStackProps [ 'onOrderUpdate' ] = value => {
3632 console . log ( 'onStackOrderUpdate' , value ) ;
3733 } ;
@@ -47,17 +43,17 @@ export const DraggableStackExample: FunctionComponent = () => {
4743 onOrderChange = { onStackOrderChange }
4844 onOrderUpdate = { onStackOrderUpdate }
4945 ref = { ref } >
50- { items . map ( letter => (
46+ { items . map ( ( letter , index ) => (
5147 < Draggable
52- key = { letter . id }
48+ key = { ` ${ letter . id } - ${ index } ` }
5349 id = { letter . id }
5450 style = { [ styles . draggable ] } >
5551 < Text style = { [ styles . text , { fontSize} ] } > { letter . value } </ Text >
5652 </ Draggable >
5753 ) ) }
5854 </ DraggableStack >
5955 </ DndProvider >
60- < View style = { { flexDirection : 'row' } } >
56+ < View style = { styles . actions } >
6157 < Button
6258 title = "Add"
6359 onPress = { ( ) => {
@@ -103,6 +99,21 @@ export const DraggableStackExample: FunctionComponent = () => {
10399 }
104100 } }
105101 />
102+
103+ < Button
104+ title = "Reorder"
105+ onPress = { ( ) => {
106+ setItems ( items => {
107+ const nextItems = items . slice ( ) . reverse ( ) ;
108+ console . log ( { nextItems} ) ;
109+ return nextItems ;
110+ } ) ;
111+ // if (ref.current) {
112+ // const {refreshOffsets} = ref.current;
113+ // runOnUI(refreshOffsets)();
114+ // }
115+ } }
116+ />
106117 </ View >
107118 </ View >
108119 ) ;
@@ -111,47 +122,49 @@ export const DraggableStackExample: FunctionComponent = () => {
111122const styles = StyleSheet . create ( {
112123 container : {
113124 flexGrow : 1 ,
114- // alignItems: 'stretch',
115- // justifyContent: 'st ',
125+ alignItems : 'stretch' ,
126+ justifyContent : 'center ' ,
116127 flexDirection : 'column' ,
117- } ,
118- view : {
119- flexDirection : 'row' ,
120- justifyContent : 'space-around' ,
121- alignItems : 'flex-end' ,
122- backgroundColor : 'rgba(255,0,255,0.1)' ,
123- flexGrow : 1 ,
128+ overflow : 'hidden' ,
124129 } ,
125130 stack : {
126131 backgroundColor : 'rgba(0,0,0,0.1)' ,
127132 alignItems : 'center' ,
128133 justifyContent : 'center' ,
129134 // flexGrow: 1,
135+ margin : 32 ,
130136 padding : 32 ,
131137 borderRadius : 32 ,
132138 } ,
133- title : {
134- color : '#555' ,
135- textTransform : 'uppercase' ,
136- fontWeight : 'bold' ,
137- position : 'absolute' ,
138- top : 10 ,
139- left : 10 ,
140- } ,
141139 draggable : {
142140 backgroundColor : 'seagreen' ,
143- height : 100 ,
144141 borderColor : 'rgba(0,0,0,0.2)' ,
145142 borderWidth : 1 ,
146143 overflow : 'hidden' ,
147144 alignItems : 'center' ,
148145 justifyContent : 'center' ,
149146 borderRadius : 32 ,
147+ paddingVertical : 8 ,
150148 } ,
151- text : {
152- color : 'white' ,
149+ title : {
150+ color : '#555' ,
151+ textTransform : 'uppercase' ,
153152 fontWeight : 'bold' ,
153+ position : 'absolute' ,
154+ top : 10 ,
155+ left : 10 ,
156+ } ,
157+ text : {
154158 fontSize : 32 ,
155159 padding : 16 ,
156160 } ,
161+ actions : {
162+ flexDirection : 'row' ,
163+ alignItems : 'center' ,
164+ justifyContent : 'center' ,
165+ position : 'absolute' ,
166+ bottom : 0 ,
167+ left : 0 ,
168+ right : 0 ,
169+ } ,
157170} ) ;
0 commit comments