Optimise sortable tile over detection and disable adding groups to groups

This commit is contained in:
Mitchell McCaffrey 2021-05-24 17:44:49 +10:00
parent 82f3a985f2
commit 68a084ebc9
2 changed files with 7 additions and 6 deletions

View File

@ -4,7 +4,7 @@ import { useDroppable } from "@dnd-kit/core";
import { useSortable } from "@dnd-kit/sortable"; import { useSortable } from "@dnd-kit/sortable";
import { animated, useSpring } from "react-spring"; import { animated, useSpring } from "react-spring";
function Sortable({ id, disableGrouping, hidden, children }) { function SortableTile({ id, disableGrouping, hidden, children, type }) {
const { const {
attributes, attributes,
listeners, listeners,
@ -13,10 +13,10 @@ function Sortable({ id, disableGrouping, hidden, children }) {
setDraggableNodeRef, setDraggableNodeRef,
over, over,
active, active,
} = useSortable({ id }); } = useSortable({ id, data: { type } });
const { setNodeRef: setGroupNodeRef } = useDroppable({ const { setNodeRef: setGroupNodeRef } = useDroppable({
id: `__group__${id}`, id: `__group__${id}`,
disabled: disableGrouping, disabled: disableGrouping || active?.data?.current?.type === "group",
}); });
const dragStyle = { const dragStyle = {
@ -82,4 +82,4 @@ function Sortable({ id, disableGrouping, hidden, children }) {
); );
} }
export default Sortable; export default SortableTile;

View File

@ -42,7 +42,7 @@ function SortableTiles({
onTileSelect(active.id); onTileSelect(active.id);
} }
function handleDragMove({ over }) { function handleDragOver({ over }) {
setOverId(over?.id); setOverId(over?.id);
} }
@ -95,8 +95,8 @@ function SortableTiles({
return ( return (
<DndContext <DndContext
onDragStart={handleDragStart} onDragStart={handleDragStart}
onDragMove={handleDragMove}
onDragEnd={handleDragEnd} onDragEnd={handleDragEnd}
onDragOver={handleDragOver}
sensors={sensors} sensors={sensors}
collisionDetection={closestCenter} collisionDetection={closestCenter}
> >
@ -107,6 +107,7 @@ function SortableTiles({
key={group.id} key={group.id}
disableGrouping={disableGrouping} disableGrouping={disableGrouping}
hidden={group.id === openGroupId} hidden={group.id === openGroupId}
type={group.type}
> >
{renderSortableGroup(group)} {renderSortableGroup(group)}
</SortableTile> </SortableTile>