1+ import { useCallback } from 'react' ;
12import { useAnimatedReaction } from 'react-native-reanimated' ;
23
34import { useDebugContext } from '../../../debug' ;
4- import type { PredefinedStrategies , SortStrategyFactory } from '../../../types' ;
5+ import type {
6+ OrderUpdaterCallbackProps ,
7+ PredefinedStrategies ,
8+ SortStrategyFactory
9+ } from '../../../types' ;
510import { error } from '../../../utils' ;
611import { useCommonValuesContext } from '../CommonValuesProvider' ;
712import { useDragContext } from '../DragProvider' ;
813
914export default function useOrderUpdater <
1015 P extends PredefinedStrategies = PredefinedStrategies
11- > ( strategy : keyof P | SortStrategyFactory , predefinedStrategies : P ) {
16+ > (
17+ strategy : keyof P | SortStrategyFactory ,
18+ predefinedStrategies : P ,
19+ reorderOnDrag : boolean
20+ ) {
1221 const useStrategy =
1322 typeof strategy === 'string' ? predefinedStrategies [ strategy ] : strategy ;
1423
@@ -25,27 +34,18 @@ export default function useOrderUpdater<
2534
2635 const updater = useStrategy ( ) ;
2736
28- useAnimatedReaction (
29- ( ) => ( {
30- activeKey : activeItemKey . value ,
31- dimensions : activeItemDimensions . value ,
32- position : triggerOriginPosition . value
33- } ) ,
34- ( { activeKey, dimensions, position } ) => {
35- if ( ! activeKey || ! dimensions || ! position ) {
36- if ( debugCross ) debugCross . set ( { position : null } ) ;
37- return ;
38- }
39-
37+ const handleOrderUpdate = useCallback (
38+ ( {
39+ activeKey,
40+ dimensions,
41+ position
42+ } : Omit < OrderUpdaterCallbackProps , 'activeIndex' > ) => {
43+ 'worklet' ;
4044 const activeIndex = keyToIndex . value [ activeKey ] ;
4145 if ( activeIndex === undefined ) {
4246 return ;
4347 }
4448
45- if ( debugCross ) {
46- debugCross . set ( { color : '#00007e' , position } ) ;
47- }
48-
4949 const newOrder = updater ( {
5050 activeIndex,
5151 activeKey,
@@ -61,6 +61,38 @@ export default function useOrderUpdater<
6161 newOrder
6262 ) ;
6363 }
64+ } ,
65+ [ handleOrderChange , keyToIndex , updater ]
66+ ) ;
67+
68+ useAnimatedReaction (
69+ ( ) => ( {
70+ activeKey : activeItemKey . value ,
71+ dimensions : activeItemDimensions . value ,
72+ position : triggerOriginPosition . value
73+ } ) ,
74+ ( { activeKey, dimensions, position } , prevProps ) => {
75+ debugCross ?. set ( { color : '#00007e' , position } ) ;
76+
77+ if ( reorderOnDrag ) {
78+ if ( activeKey !== null && dimensions && position ) {
79+ handleOrderUpdate ( { activeKey, dimensions, position } ) ;
80+ } else {
81+ debugCross ?. set ( { position : null } ) ;
82+ }
83+ } else if (
84+ activeKey === null &&
85+ prevProps ?. dimensions &&
86+ prevProps ?. position &&
87+ prevProps ?. activeKey !== null
88+ ) {
89+ debugCross ?. set ( { position : null } ) ;
90+ handleOrderUpdate ( {
91+ activeKey : prevProps . activeKey ,
92+ dimensions : prevProps . dimensions ,
93+ position : prevProps . position
94+ } ) ;
95+ }
6496 }
6597 ) ;
6698}
0 commit comments