@@ -7,43 +7,43 @@ import {
7
7
UniqueIdentifier ,
8
8
useSensor ,
9
9
useSensors ,
10
- } from " @dnd-kit/core" ;
10
+ } from ' @dnd-kit/core'
11
11
import {
12
12
arrayMove ,
13
13
SortableContext ,
14
14
sortableKeyboardCoordinates ,
15
15
verticalListSortingStrategy ,
16
- } from " @dnd-kit/sortable" ;
17
- import { CSS } from " @dnd-kit/utilities" ;
18
- import { useSortable } from " @dnd-kit/sortable" ;
19
- import { Drag } from " ./icons" ;
16
+ } from ' @dnd-kit/sortable'
17
+ import { CSS } from ' @dnd-kit/utilities'
18
+ import { useSortable } from ' @dnd-kit/sortable'
19
+ import { Drag } from ' ./icons'
20
20
21
21
type Props < T > = {
22
- children : ( item : T , index : number ) => React . ReactNode ;
23
- setItems : ( items : T [ ] ) => void ;
24
- items : T [ ] ;
25
- disableDragByIndex ?: number ;
26
- } ;
22
+ children : ( item : T , index : number ) => React . ReactNode
23
+ setItems : ( items : T [ ] ) => void
24
+ items : T [ ]
25
+ disableDragByIndex ?: number
26
+ }
27
27
28
28
function ItemWrapper ( {
29
29
children,
30
30
id,
31
- disabledDrag ,
31
+ hasDragDisabled ,
32
32
} : {
33
- children : React . ReactNode ;
34
- id : UniqueIdentifier ;
35
- disabledDrag : boolean ;
33
+ children : React . ReactNode
34
+ id : UniqueIdentifier
35
+ hasDragDisabled : boolean
36
36
} ) {
37
37
const { attributes, listeners, setNodeRef, transform, transition } =
38
- useSortable ( { id } ) ;
38
+ useSortable ( { id } )
39
39
const style = {
40
40
transform : CSS . Transform . toString ( transform ) ,
41
41
transition,
42
- } ;
42
+ }
43
43
44
44
return (
45
- < div style = { style } className = "flex items-center w-full" >
46
- { disabledDrag ? (
45
+ < div style = { style } className = "flex w-full items-center " >
46
+ { hasDragDisabled ? (
47
47
< div className = "size-8" />
48
48
) : (
49
49
< div ref = { setNodeRef } { ...attributes } { ...listeners } className = "size-8" >
@@ -52,7 +52,7 @@ function ItemWrapper({
52
52
) }
53
53
< div className = "grow" > { children } </ div >
54
54
</ div >
55
- ) ;
55
+ )
56
56
}
57
57
58
58
export function SortableArea < T extends { id : UniqueIdentifier } > ( {
@@ -65,21 +65,22 @@ export function SortableArea<T extends { id: UniqueIdentifier }>({
65
65
useSensor ( PointerSensor ) ,
66
66
useSensor ( KeyboardSensor , {
67
67
coordinateGetter : sortableKeyboardCoordinates ,
68
- } ) ,
69
- ) ;
68
+ } )
69
+ )
70
70
71
71
function handleDragEnd ( event : DragEndEvent ) {
72
- const { active, over } = event ;
72
+ const { active, over } = event
73
73
74
74
if ( over == null ) {
75
- return ;
75
+ // The item was dropped in it's original place
76
+ return
76
77
}
77
78
78
79
if ( active . id !== over . id ) {
79
- const oldIndex = items . findIndex ( ( { id } ) => id === active . id ) ;
80
- const newIndex = items . findIndex ( ( { id } ) => id === over . id ) ;
80
+ const oldIndex = items . findIndex ( ( { id } ) => id === active . id )
81
+ const newIndex = items . findIndex ( ( { id } ) => id === over . id )
81
82
82
- setItems ( arrayMove ( items , oldIndex , newIndex ) ) ;
83
+ setItems ( arrayMove ( items , oldIndex , newIndex ) )
83
84
}
84
85
}
85
86
@@ -94,12 +95,12 @@ export function SortableArea<T extends { id: UniqueIdentifier }>({
94
95
< ItemWrapper
95
96
key = { index }
96
97
id = { item . id }
97
- disabledDrag = { disableDragByIndex === index }
98
+ hasDragDisabled = { disableDragByIndex === index }
98
99
>
99
100
{ children ( item , index ) }
100
101
</ ItemWrapper >
101
102
) ) }
102
103
</ SortableContext >
103
104
</ DndContext >
104
- ) ;
105
+ )
105
106
}
0 commit comments