Skip to content

Commit

Permalink
1.1.6 fix reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
KaWaite committed Sep 28, 2021
1 parent b23e90c commit a19a24a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-align",
"version": "1.1.5",
"version": "1.1.6",
"author": "KaWaite",
"module": "dist/realign.esm.js",
"license": "MIT",
Expand Down
42 changes: 26 additions & 16 deletions src/Grid/GridItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function GridItem<T = unknown>({
const { enabled } = useEditorMode();

const [isHovered, setHovered] = useState(false);
const dragIndexRef = useRef<number | undefined>();

const handleExtend = () => {
if (!extendable || !onExtend) return;
Expand Down Expand Up @@ -132,27 +133,36 @@ export default function GridItem<T = unknown>({
}
}

onReorder(item.id, location, dragIndex, hoverIndex);
dragIndexRef.current = dragIndex;
},
drop(item) {
if (dragIndexRef.current !== undefined) {
onReorder(item.id, location, dragIndexRef.current, index);
dragIndexRef.current = undefined;
}
},
});

const [{ isDragging }, drag, preview] = useDrag({
type: ItemType.ITEM,
item: { id, index },
canDrag: draggable ?? enabled,
end: (item, monitor) => {
const dropResults: {
location: T;
} | null = monitor.getDropResult();
const [{ isDragging }, drag, preview] = useDrag(
{
type: ItemType.ITEM,
item: { id, index },
canDrag: draggable ?? enabled,
end: (item, monitor) => {
const dropResults: {
location: T;
} | null = monitor.getDropResult();

if (dropResults && dropResults.location !== location) {
onMoveArea(item.id, dropResults.location, location);
}
if (dropResults && dropResults.location !== location) {
onMoveArea(item.id, dropResults.location, location);
}
},
collect: (monitor: DragSourceMonitor) => ({
isDragging: monitor.isDragging(),
}),
},
collect: (monitor: DragSourceMonitor) => ({
isDragging: monitor.isDragging(),
}),
});
[dragIndexRef]
);

preview(drop(ref));
// ***************************************
Expand Down

0 comments on commit a19a24a

Please sign in to comment.