Skip to content

Commit

Permalink
fix(AnalyticalTable): fix wrong column order on consecutive column re…
Browse files Browse the repository at this point in the history
…order events (#233)
  • Loading branch information
vbersch authored Nov 28, 2019
1 parent 5436bd4 commit f94a6ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useCallback, useRef, useState } from 'react';
import { useCallback, useState } from 'react';
import { Event } from '@ui5/webcomponents-react-base/lib/Event';

const getColumnId = (column) => {
return typeof column.accessor === 'string' ? column.accessor : column.id;
};

export const useDragAndDrop = (props, setColumnOrder, columnOrder, isBeingResized, onColumnsOrderChanged) => {
export const useDragAndDrop = (props, setColumnOrder, columnOrder, isBeingResized) => {
const { onColumnsReordered } = props;

const [dragOver, setDragOver] = useState('');

const handleDragStart = useCallback(
Expand Down Expand Up @@ -42,9 +45,13 @@ export const useDragAndDrop = (props, setColumnOrder, columnOrder, isBeingResize
tempCols.splice(droppedColIdx, 0, tempCols.splice(draggedColIdx, 1)[0]);
setColumnOrder(tempCols);

const newOrderedColumns = [...props.columns];
newOrderedColumns.splice(droppedColIdx, 0, newOrderedColumns.splice(draggedColIdx, 1)[0]);
onColumnsOrderChanged(e.currentTarget, props.columns[draggedColIdx], newOrderedColumns);
const columnsNewOrder = tempCols.map((tempColId) => props.columns.find((col) => getColumnId(col) === tempColId));
onColumnsReordered(
Event.of(null, e, {
columnsNewOrder,
column: props.columns[draggedColIdx]
})
);
},
[columnOrder]
);
Expand Down
16 changes: 1 addition & 15 deletions packages/main/src/components/AnalyticalTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, ref: Ref<
selectedRowKey,
LoadingComponent,
onRowExpandChange,
onColumnsReordered,
noDataText,
NoDataComponent,
visibleRows,
Expand Down Expand Up @@ -253,25 +252,12 @@ const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, ref: Ref<
[tableState.groupBy, onGroup]
);

const onColumnsOrderChanged = useCallback(
(target, column, columnsNewOrder) => {
onColumnsReordered(
Event.of(null, target, {
columnsNewOrder,
column
})
);
},
[tableState.columnOrder, onColumnsReordered]
);

const [headerRef, tableWidth] = useWindowResize();
const [dragOver, handleDragEnter, handleDragStart, handleDragOver, handleOnDrop, handleOnDragEnd] = useDragAndDrop(
props,
setColumnOrder,
tableState.columnOrder,
isBeingResized,
onColumnsOrderChanged
isBeingResized
);

return (
Expand Down

0 comments on commit f94a6ff

Please sign in to comment.