1
1
import { MenuOutlined , InfoCircleOutlined } from "@ant-design/icons" ;
2
2
import { List , Collapse , Tooltip , type CollapseProps } from "antd" ;
3
- import React from "react" ;
4
- import type { SortEnd } from "react-sortable-hoc" ;
5
- import { SortableContainer , SortableElement , SortableHandle } from "react-sortable-hoc" ;
6
3
import { settings , settingsTooltips } from "messages" ;
4
+ import { DndContext , type DragEndEvent } from "@dnd-kit/core" ;
5
+ import { CSS } from "@dnd-kit/utilities" ;
6
+ import { SortableContext , useSortable , verticalListSortingStrategy } from "@dnd-kit/sortable" ;
7
7
8
- // Example taken and modified from https://4x. ant.design/components/table/#components-table-demo-drag-sorting-handler.
8
+ // Example taken and modified from https://ant.design/components/table/#components-table-demo-drag-sorting-handler.
9
9
10
- const DragHandle = SortableHandle ( ( ) => < MenuOutlined style = { { cursor : "grab" , color : "#999" } } /> ) ;
10
+ function SortableListItem ( { colorLayerName } : { colorLayerName : string } ) {
11
+ const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable ( {
12
+ id : colorLayerName ,
13
+ } ) ;
11
14
12
- const SortableItem = SortableElement ( ( { name } : { name : string } ) => (
13
- < List . Item key = { name } >
14
- < DragHandle /> { name }
15
- </ List . Item >
16
- ) ) ;
15
+ const style = {
16
+ transform : CSS . Transform . toString ( transform ) ,
17
+ transition,
18
+ zIndex : isDragging ? "100" : "auto" ,
19
+ opacity : isDragging ? 0.3 : 1 ,
20
+ } ;
17
21
18
- const SortableLayerSettingsContainer = SortableContainer ( ( { children } : { children : any } ) => {
19
- return < div style = { { paddingTop : - 16 , paddingBottom : - 16 } } > { children } </ div > ;
20
- } ) ;
22
+ return (
23
+ < List . Item id = { colorLayerName } ref = { setNodeRef } style = { style } >
24
+ < MenuOutlined style = { { cursor : "grab" , color : "#999" } } { ...listeners } { ...attributes } /> { " " }
25
+ { colorLayerName }
26
+ </ List . Item >
27
+ ) ;
28
+ }
21
29
22
30
export default function ColorLayerOrderingTable ( {
23
31
colorLayerNames,
24
32
onChange,
25
33
} : {
26
34
colorLayerNames ?: string [ ] ;
27
35
onChange ?: ( newColorLayerNames : string [ ] ) => void ;
28
- } ) : JSX . Element {
29
- const onSortEnd = ( { oldIndex, newIndex } : SortEnd ) => {
30
- document . body . classList . remove ( "is-dragging" ) ;
31
- if ( oldIndex !== newIndex && onChange && colorLayerNames ) {
32
- const movedElement = colorLayerNames [ oldIndex ] ;
33
- const newColorLayerNames = colorLayerNames . filter ( ( _ , index ) => index !== oldIndex ) ;
34
- newColorLayerNames . splice ( newIndex , 0 , movedElement ) ;
35
- onChange ( newColorLayerNames ) ;
36
+ } ) {
37
+ const onSortEnd = ( event : DragEndEvent ) => {
38
+ const { active, over } = event ;
39
+
40
+ if ( active && over && colorLayerNames ) {
41
+ const oldIndex = colorLayerNames . indexOf ( active . id as string ) ;
42
+ const newIndex = colorLayerNames . indexOf ( over . id as string ) ;
43
+
44
+ document . body . classList . remove ( "is-dragging" ) ;
45
+
46
+ if ( oldIndex !== newIndex && onChange ) {
47
+ const movedElement = colorLayerNames [ oldIndex ] ;
48
+ const newColorLayerNames = colorLayerNames . filter ( ( _ , index ) => index !== oldIndex ) ;
49
+ newColorLayerNames . splice ( newIndex , 0 , movedElement ) ;
50
+ onChange ( newColorLayerNames ) ;
51
+ }
36
52
}
37
53
} ;
38
54
39
55
const isSettingEnabled = colorLayerNames && colorLayerNames . length > 1 ;
56
+ const sortingItems = isSettingEnabled ? colorLayerNames . map ( ( name ) => name ) : [ ] ;
40
57
const collapsibleDisabledExplanation =
41
58
"The order of layers can only be configured when the dataset has multiple color layers." ;
42
59
@@ -55,29 +72,25 @@ export default function ColorLayerOrderingTable({
55
72
{
56
73
label : panelTitle ,
57
74
key : "1" ,
58
- children : (
59
- < SortableLayerSettingsContainer
60
- onSortEnd = { onSortEnd }
61
- onSortStart = { ( ) =>
62
- colorLayerNames &&
63
- colorLayerNames . length > 1 &&
64
- document . body . classList . add ( "is-dragging" )
65
- }
66
- useDragHandle
67
- >
68
- { colorLayerNames ?. map ( ( name , index ) => (
69
- < SortableItem key = { name } index = { index } name = { name } />
70
- ) ) }
71
- </ SortableLayerSettingsContainer >
72
- ) ,
75
+ children : sortingItems . map ( ( name ) => < SortableListItem key = { name } colorLayerName = { name } /> ) ,
73
76
} ,
74
77
] ;
75
78
76
79
return (
77
- < Collapse
78
- defaultActiveKey = { [ ] }
79
- collapsible = { isSettingEnabled ? "header" : "disabled" }
80
- items = { collapseItems }
81
- />
80
+ < DndContext
81
+ autoScroll = { false }
82
+ onDragStart = { ( ) => {
83
+ colorLayerNames && colorLayerNames . length > 1 && document . body . classList . add ( "is-dragging" ) ;
84
+ } }
85
+ onDragEnd = { onSortEnd }
86
+ >
87
+ < SortableContext items = { sortingItems } strategy = { verticalListSortingStrategy } >
88
+ < Collapse
89
+ defaultActiveKey = { [ ] }
90
+ collapsible = { isSettingEnabled ? "header" : "disabled" }
91
+ items = { collapseItems }
92
+ />
93
+ </ SortableContext >
94
+ </ DndContext >
82
95
) ;
83
96
}
0 commit comments