1- import React , { useState , type FunctionComponent } from 'react' ;
2- import { Button , StyleSheet , Text , View } from 'react-native' ;
31import {
42 DndProvider ,
5- type ObjectWithId ,
63 Draggable ,
74 DraggableStack ,
5+ UniqueIdentifier ,
86 type DraggableStackProps ,
7+ type ObjectWithId ,
98} from '@mgcrea/react-native-dnd/src' ;
9+ import React , { useCallback , useState , type FunctionComponent } from 'react' ;
10+ import { Button , StyleSheet , Text , View } from 'react-native' ;
11+ import { configureReanimatedLogger } from 'react-native-reanimated' ;
12+
13+ // This is the default configuration
14+ configureReanimatedLogger ( {
15+ // level: ReanimatedLogLevel.warn,
16+ strict : false , // Reanimated runs in strict mode by default
17+ } ) ;
1018
1119const items = [ '🤓' , '🤖🤖' , '👻👻👻' , '👾👾👾👾' ] ;
1220const data = items . map ( ( letter , index ) => ( {
@@ -15,15 +23,23 @@ const data = items.map((letter, index) => ({
1523} ) ) satisfies ObjectWithId [ ] ;
1624
1725export const DraggableStackExample : FunctionComponent = ( ) => {
18- const onStackOrderChange : DraggableStackProps [ 'onOrderChange' ] = value => {
19- console . log ( 'onStackOrderChange' , value ) ;
20- } ;
26+ const [ items , setItems ] = useState ( data ) ;
27+ const [ fontSize , setFontSize ] = useState ( 32 ) ;
28+
29+ const onStackOrderChange : DraggableStackProps [ 'onOrderChange' ] = useCallback (
30+ ( order : UniqueIdentifier [ ] ) => {
31+ console . log ( 'onStackOrderChange' , order ) ;
32+ setTimeout ( ( ) => {
33+ // setItems(items => order.map(id => items.find(item => item.id === id)!));
34+ // setFontSize(fontSize => fontSize + 1);
35+ } , 1000 ) ;
36+ } ,
37+ [ ] ,
38+ ) ;
2139 const onStackOrderUpdate : DraggableStackProps [ 'onOrderUpdate' ] = value => {
2240 console . log ( 'onStackOrderUpdate' , value ) ;
2341 } ;
2442
25- const [ items , setItems ] = useState ( data ) ;
26-
2743 return (
2844 < View style = { styles . container } >
2945 < Text style = { styles . title } > DraggableStack Example</ Text >
@@ -39,33 +55,49 @@ export const DraggableStackExample: FunctionComponent = () => {
3955 key = { letter . id }
4056 id = { letter . id }
4157 style = { [ styles . draggable ] } >
42- < Text style = { styles . text } > { letter . value } </ Text >
58+ < Text style = { [ styles . text , { fontSize } ] } > { letter . value } </ Text >
4359 </ Draggable >
4460 ) ) }
4561 </ DraggableStack >
4662 </ DndProvider >
47- < Button
48- title = "Add"
49- onPress = { ( ) => {
50- setItems ( prevItems => {
51- const randomIndex = 2 ; //Math.floor(Math.random() * prevItems.length);
52- prevItems . splice ( randomIndex , 0 , {
53- value : '🤪' ,
54- id : `${ prevItems . length } -🤪` ,
63+ < View style = { { flexDirection : 'row' } } >
64+ < Button
65+ title = "Add"
66+ onPress = { ( ) => {
67+ setItems ( prevItems => {
68+ const randomIndex = 2 ; //Math.floor(Math.random() * prevItems.length);
69+ prevItems . splice ( randomIndex , 0 , {
70+ value : '🤪' ,
71+ id : `${ prevItems . length } -🤪` ,
72+ } ) ;
73+ return prevItems . slice ( ) ;
5574 } ) ;
56- return prevItems . slice ( ) ;
57- } ) ;
58- } }
59- />
60- < Button
61- title = "Delete"
62- onPress = { ( ) => {
63- setItems ( prevItems => {
64- const randomIndex = Math . floor ( Math . random ( ) * prevItems . length ) ;
65- return prevItems . filter ( ( _ , index ) => index !== randomIndex ) ;
66- } ) ;
67- } }
68- />
75+ } }
76+ />
77+ < Button
78+ title = "Delete"
79+ onPress = { ( ) => {
80+ setItems ( prevItems => {
81+ const randomIndex = Math . floor ( Math . random ( ) * prevItems . length ) ;
82+ return prevItems . filter ( ( _ , index ) => index !== randomIndex ) ;
83+ } ) ;
84+ } }
85+ />
86+
87+ < Button
88+ title = "Plus"
89+ onPress = { ( ) => {
90+ setFontSize ( prevFontSize => prevFontSize + 1 ) ;
91+ } }
92+ />
93+
94+ < Button
95+ title = "Minus"
96+ onPress = { ( ) => {
97+ setFontSize ( prevFontSize => prevFontSize - 1 ) ;
98+ } }
99+ />
100+ </ View >
69101 </ View >
70102 ) ;
71103} ;
@@ -93,7 +125,6 @@ const styles = StyleSheet.create({
93125 } ,
94126 draggable : {
95127 backgroundColor : 'seagreen' ,
96- opacity : 0.5 ,
97128 height : 100 ,
98129 borderColor : 'rgba(0,0,0,0.2)' ,
99130 borderWidth : 1 ,
0 commit comments