11import React from "react" ;
2- import { flexRender } from "@tanstack/react-table" ;
2+ import { Column , flexRender } from "@tanstack/react-table" ;
33import { c } from "helpers/StylesHelper" ;
44import { TableHeaderProps } from "cdm/HeaderModel" ;
55import { useDrag , useDrop } from "react-dnd" ;
6- import { TableColumn } from "cdm/FolderModel" ;
6+ import { RowDataType } from "cdm/FolderModel" ;
77import { DnDConfiguration } from "helpers/Constants" ;
88
99interface DragItem {
@@ -13,76 +13,57 @@ interface DragItem {
1313}
1414
1515export default function TableHeader ( headerProps : TableHeaderProps ) {
16- const { table, header, findColumn, headerIndex } = headerProps ;
17- const { id } = header . column . columnDef as TableColumn ;
18- const { view, tableState } = table . options . meta ;
16+ const { table, header, reorderColumn, headerIndex } = headerProps ;
17+ const { view } = table . options . meta ;
1918 const { columnOrder } = table . options . state ;
20- const columns = tableState . columns ( ( state ) => state . columns ) ;
2119
22- const originalIndex = React . useMemo ( ( ) => {
23- return columnOrder . findIndex ( ( colId ) => colId === id ) ;
24- } , [ columnOrder ] ) ;
25-
26- //DnD
27- const DnDref = React . useRef < HTMLDivElement > ( null ) ;
28-
29- function moveColumn ( source : number , destinationKey : string ) {
30- const { index : destIndex } = findColumn ( destinationKey ) ;
31- const newColumnOrder = [ ...columnOrder ] ;
32- newColumnOrder . splice ( destIndex , 1 ) ;
33- newColumnOrder . splice ( source , 0 , columns [ destIndex ] . id ) ;
34- table . setColumnOrder ( newColumnOrder ) ;
20+ function moveColumn (
21+ draggedColumnId : string ,
22+ targetColumnId : string ,
23+ columnOrder : string [ ]
24+ ) {
25+ const newColumnOrder = reorderColumn (
26+ draggedColumnId ,
27+ targetColumnId ,
28+ columnOrder
29+ ) ;
30+ // Save on disk
3531 view . diskConfig . reorderColumns ( newColumnOrder ) ;
32+ // Save on memory
33+ return newColumnOrder ;
3634 }
3735
38- const [ { isDragging, handlerId } , drag ] = useDrag (
39- ( ) => ( {
40- type : DnDConfiguration . DRAG_TYPE ,
41- item : { id, originalIndex } ,
42- collect : ( monitor ) => ( {
43- handlerId : monitor . getHandlerId ( ) ,
44- isDragging : monitor . isDragging ( ) ,
45- } ) ,
46- end : ( item , monitor ) => {
47- const { id : droppedId , originalIndex } = item ;
48- const didDrop = monitor . didDrop ( ) ;
49- if ( ! didDrop ) {
50- moveColumn ( originalIndex , droppedId ) ;
51- }
52- } ,
53- } ) ,
54- [ id , originalIndex , findColumn ]
55- ) ;
36+ const [ , dropRef ] = useDrop ( {
37+ accept : DnDConfiguration . DRAG_TYPE ,
38+ drop : ( draggedColumn : Column < RowDataType > ) => {
39+ const newColumnOrder = moveColumn (
40+ draggedColumn . id ,
41+ header . column . id ,
42+ columnOrder
43+ ) ;
44+ table . setColumnOrder ( newColumnOrder ) ;
45+ } ,
46+ } ) ;
5647
57- const [ , drop ] = useDrop (
58- ( ) => ( {
59- accept : DnDConfiguration . DRAG_TYPE ,
60- hover ( { id : draggedId } : DragItem ) {
61- if ( draggedId !== id ) {
62- const { index : overIndex } = findColumn ( id ) ;
63- moveColumn ( overIndex , draggedId ) ;
64- }
65- } ,
48+ const [ { isDragging } , dragRef ] = useDrag ( {
49+ collect : ( monitor ) => ( {
50+ isDragging : monitor . isDragging ( ) ,
6651 } ) ,
67- [ findColumn ]
68- ) ;
52+ item : ( ) => header . column ,
53+ type : DnDConfiguration . DRAG_TYPE ,
54+ } ) ;
6955
70- const opacity = isDragging ? 0 : 1 ;
71- drag ( drop ( DnDref ) ) ;
7256 return (
7357 < div
7458 key = { `${ header . id } -${ headerIndex } ` }
7559 className = { `${ c ( "th noselect" ) } header` }
7660 style = { {
7761 width : header . getSize ( ) ,
78- opacity,
62+ opacity : isDragging ? 0.5 : 1 ,
7963 } }
64+ ref = { dropRef }
8065 >
81- < div
82- ref = { DnDref }
83- data-handler-id = { handlerId }
84- key = { `${ header . id } -${ headerIndex } -dnd` }
85- >
66+ < div ref = { dragRef } key = { `${ header . id } -${ headerIndex } -dnd` } >
8667 { header . isPlaceholder
8768 ? null
8869 : flexRender ( header . column . columnDef . header , header . getContext ( ) ) }
0 commit comments