fix: show dropzone
This commit is contained in:
parent
3e7aabe8ad
commit
7f04f5f519
@ -7,12 +7,12 @@ import { ReactComponent as PlusIcon } from "../../../assets/icons/plus.svg";
|
|||||||
|
|
||||||
type DraggableListItemProps = {
|
type DraggableListItemProps = {
|
||||||
index: number;
|
index: number;
|
||||||
activePlus: boolean;
|
dropPlaceIndex: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DraggableListItem = ({
|
export const DraggableListItem = ({
|
||||||
index,
|
index,
|
||||||
activePlus,
|
dropPlaceIndex,
|
||||||
}: DraggableListItemProps) => (
|
}: DraggableListItemProps) => (
|
||||||
<Draggable draggableId={String(index)} index={index}>
|
<Draggable draggableId={String(index)} index={index}>
|
||||||
{(provided, snapshot) => (
|
{(provided, snapshot) => (
|
||||||
@ -23,7 +23,7 @@ export const DraggableListItem = ({
|
|||||||
totalIndex={index}
|
totalIndex={index}
|
||||||
draggableProps={provided.dragHandleProps}
|
draggableProps={provided.dragHandleProps}
|
||||||
/>
|
/>
|
||||||
{activePlus && !snapshot.mode && (
|
{dropPlaceIndex === index && !snapshot.mode && (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
|
|||||||
@ -9,8 +9,11 @@ import { questionStore, updateQuestionsListDragAndDrop } from "@root/questions";
|
|||||||
import { reorder } from "./helper";
|
import { reorder } from "./helper";
|
||||||
|
|
||||||
import type { DropResult } from "react-beautiful-dnd";
|
import type { DropResult } from "react-beautiful-dnd";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
export const DraggableList = () => {
|
export const DraggableList = () => {
|
||||||
|
const [draggableId, setDraggableId] = useState<number>(-1);
|
||||||
|
const [dropPlaceIndex, setDropPlaceIndex] = useState<number>(-1);
|
||||||
const { listQuestions } = questionStore();
|
const { listQuestions } = questionStore();
|
||||||
|
|
||||||
const onDragEnd = ({ destination, source }: DropResult) => {
|
const onDragEnd = ({ destination, source }: DropResult) => {
|
||||||
@ -18,19 +21,31 @@ export const DraggableList = () => {
|
|||||||
const newItems = reorder(listQuestions, source.index, destination.index);
|
const newItems = reorder(listQuestions, source.index, destination.index);
|
||||||
|
|
||||||
updateQuestionsListDragAndDrop(newItems);
|
updateQuestionsListDragAndDrop(newItems);
|
||||||
|
setDraggableId(-1);
|
||||||
|
setDropPlaceIndex(-1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DragDropContext onDragEnd={onDragEnd}>
|
<DragDropContext
|
||||||
|
onDragStart={({ draggableId }) => setDraggableId(Number(draggableId))}
|
||||||
|
onDragUpdate={({ destination }) => {
|
||||||
|
setDropPlaceIndex(destination?.index ?? -1);
|
||||||
|
}}
|
||||||
|
onDragEnd={onDragEnd}
|
||||||
|
>
|
||||||
<StrictModeDroppable droppableId="droppable-list">
|
<StrictModeDroppable droppableId="droppable-list">
|
||||||
{(provided, snapshot) => (
|
{(provided) => (
|
||||||
<Box ref={provided.innerRef} {...provided.droppableProps}>
|
<Box ref={provided.innerRef} {...provided.droppableProps}>
|
||||||
{listQuestions.map((_, index) => (
|
{listQuestions.map((_, index) => (
|
||||||
<DraggableListItem
|
<DraggableListItem
|
||||||
key={index}
|
key={index}
|
||||||
index={index}
|
index={index}
|
||||||
activePlus={snapshot.isDraggingOver}
|
dropPlaceIndex={
|
||||||
|
dropPlaceIndex < draggableId
|
||||||
|
? dropPlaceIndex - 1
|
||||||
|
: dropPlaceIndex
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{provided.placeholder}
|
{provided.placeholder}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user