Skip to content

Commit 69a0c6b

Browse files
authored
Move full column order fetching back into onDragStart (#1280)
1 parent 456bf99 commit 69a0c6b

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

webview/src/experiments/components/Experiments/index.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,6 @@ export const ExperimentsTable: React.FC<{
166166
expandedRowCount
167167
})
168168
})
169-
hooks.allColumns.push((allColumns, { instance: { state } }) => {
170-
const { columnOrder } = state
171-
if (!columnOrder || columnOrder.length === 0) {
172-
state.columnOrder = allColumns.map(col => col.id)
173-
}
174-
return allColumns
175-
})
176169
}
177170
)
178171

webview/src/experiments/components/Table/MergeHeaderGroups.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@ import cx from 'classnames'
33
import { SortDefinition } from 'dvc/src/experiments/model/sortBy'
44
import { Experiment, MetricOrParam } from 'dvc/src/experiments/webview/contract'
55
import { HeaderGroup } from 'react-table'
6-
import {
7-
DragDropContext,
8-
DragUpdate,
9-
Droppable,
10-
DropResult,
11-
ResponderProvided
12-
} from 'react-beautiful-dnd'
6+
import { DragDropContext, Droppable, Responders } from 'react-beautiful-dnd'
137
import { TableHeader } from './TableHeader'
148
import styles from './styles.module.scss'
159

16-
export const MergedHeaderGroup: React.FC<{
17-
headerGroup: HeaderGroup<Experiment>
18-
columns: HeaderGroup<Experiment>[]
19-
sorts: SortDefinition[]
20-
orderedColumns: MetricOrParam[]
21-
onDragUpdate?: (initial: DragUpdate, provided: ResponderProvided) => void
22-
onDragEnd: (initial: DropResult, provided: ResponderProvided) => void
23-
}> = ({
10+
export const MergedHeaderGroup: React.FC<
11+
{
12+
headerGroup: HeaderGroup<Experiment>
13+
columns: HeaderGroup<Experiment>[]
14+
sorts: SortDefinition[]
15+
orderedColumns: MetricOrParam[]
16+
} & Responders
17+
> = ({
2418
headerGroup,
2519
sorts,
2620
columns,
2721
orderedColumns,
22+
onDragStart,
2823
onDragUpdate,
2924
onDragEnd
3025
}) => {
3126
return (
32-
<DragDropContext onDragUpdate={onDragUpdate} onDragEnd={onDragEnd}>
27+
<DragDropContext
28+
onDragStart={onDragStart}
29+
onDragUpdate={onDragUpdate}
30+
onDragEnd={onDragEnd}
31+
>
3332
<Droppable droppableId="droppable" direction="horizontal">
3433
{provided => (
3534
<div

webview/src/experiments/components/Table/TableHead.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SortDefinition } from 'dvc/src/experiments/model/sortBy'
22
import { Experiment, MetricOrParam } from 'dvc/src/experiments/webview/contract'
3-
import React from 'react'
3+
import React, { useRef } from 'react'
44
import { HeaderGroup, TableInstance } from 'react-table'
55
import { DragUpdate } from 'react-beautiful-dnd'
66
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
@@ -19,7 +19,8 @@ export const TableHead: React.FC<TableHeadProps> = ({
1919
instance: {
2020
headerGroups,
2121
setColumnOrder,
22-
state: { columnOrder }
22+
state: { columnOrder },
23+
allColumns
2324
},
2425
columns,
2526
sorts
@@ -28,18 +29,23 @@ export const TableHead: React.FC<TableHeadProps> = ({
2829
const allHeaders: HeaderGroup<Experiment>[] = []
2930
headerGroups.forEach(headerGroup => allHeaders.push(...headerGroup.headers))
3031

32+
const fullColumnOrder = useRef<string[]>()
33+
34+
const onDragStart = () => {
35+
fullColumnOrder.current = allColumns.map(column => column.id)
36+
}
37+
3138
const onDragUpdate = (column: DragUpdate) => {
3239
if (!column.destination) {
3340
return
3441
}
3542
const { draggableId, destination } = column
3643
if (destination.index > 1) {
37-
const colOrder = [...columnOrder]
38-
const oldIndex = colOrder.indexOf(draggableId)
39-
40-
colOrder.splice(oldIndex, 1)
41-
colOrder.splice(destination.index, 0, draggableId)
42-
setColumnOrder(colOrder)
44+
const newColumnOrder = [...(fullColumnOrder.current as string[])]
45+
const oldIndex = newColumnOrder.indexOf(draggableId)
46+
newColumnOrder.splice(oldIndex, 1)
47+
newColumnOrder.splice(destination.index, 0, draggableId)
48+
setColumnOrder(newColumnOrder)
4349
}
4450
}
4551

@@ -60,6 +66,7 @@ export const TableHead: React.FC<TableHeadProps> = ({
6066
headerGroup={headerGroup}
6167
columns={allHeaders}
6268
sorts={sorts}
69+
onDragStart={onDragStart}
6370
onDragUpdate={onDragUpdate}
6471
onDragEnd={onDragEnd}
6572
/>

0 commit comments

Comments
 (0)