frontPanel/src/pages/Questions/AnswerDraggableList/index.tsx
2023-09-06 10:30:27 +03:00

55 lines
1.4 KiB
TypeScript

import { Box } from "@mui/material";
import { DragDropContext, Droppable } from "react-beautiful-dnd";
import { AnswerItem } from "./AnswerItem";
import { updateVariants } from "@root/questions";
import { reorder } from "./helper";
import { ReactNode } from "react";
import type { DropResult } from "react-beautiful-dnd";
import type { Variants } from "@root/questions";
type AnswerDraggableListProps = {
variants: Variants[];
totalIndex: number;
icon?: ReactNode;
};
export const AnswerDraggableList = ({
variants,
totalIndex,
icon,
}: AnswerDraggableListProps) => {
const onDragEnd = ({ destination, source }: DropResult) => {
if (destination) {
const newItems = reorder(variants, source.index, destination.index);
updateVariants(totalIndex, newItems);
}
};
return (
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="droppable-answer-list">
{(provided) => (
<Box ref={provided.innerRef} {...provided.droppableProps}>
{variants.map((variant, index) => (
<AnswerItem
key={index}
index={index}
totalIndex={totalIndex}
variants={variants}
variant={variant}
icon={icon}
/>
))}
{provided.placeholder}
</Box>
)}
</Droppable>
</DragDropContext>
);
};