265 lines
7.2 KiB
TypeScript
265 lines
7.2 KiB
TypeScript
"use client";
|
|
|
|
import type { DragEndEvent, DragStartEvent } from "@dnd-kit/core";
|
|
import {
|
|
closestCenter,
|
|
DndContext,
|
|
DragOverlay,
|
|
KeyboardSensor,
|
|
PointerSensor,
|
|
useSensor,
|
|
useSensors,
|
|
} from "@dnd-kit/core";
|
|
import {
|
|
arrayMove,
|
|
SortableContext,
|
|
sortableKeyboardCoordinates,
|
|
useSortable,
|
|
verticalListSortingStrategy,
|
|
} from "@dnd-kit/sortable";
|
|
import { CSS } from "@dnd-kit/utilities";
|
|
import { GripVertical } from "lucide-react";
|
|
import { useState } from "react";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
import {
|
|
Item,
|
|
ItemContent,
|
|
ItemGroup,
|
|
ItemMedia,
|
|
ItemTitle,
|
|
} from "@/components/ui/item";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
interface Todo {
|
|
id: string;
|
|
title: string;
|
|
completed: boolean;
|
|
}
|
|
|
|
interface Todo2Props {
|
|
heading?: string;
|
|
description?: string;
|
|
initialTodos?: Todo[];
|
|
className?: string;
|
|
}
|
|
|
|
const defaultTodos: Todo[] = [
|
|
{
|
|
id: "1",
|
|
title: "تکمیل مستندات فنی پروژه",
|
|
completed: false,
|
|
},
|
|
{
|
|
id: "2",
|
|
title: "بررسی و تأیید طراحی رابط کاربری",
|
|
completed: false,
|
|
},
|
|
{
|
|
id: "3",
|
|
title: "آمادهسازی گزارش هفتگی",
|
|
completed: false,
|
|
},
|
|
{
|
|
id: "4",
|
|
title: "بازبینی کد منابع",
|
|
completed: true,
|
|
},
|
|
{
|
|
id: "5",
|
|
title: "هماهنگی با تیم طراحی",
|
|
completed: false,
|
|
},
|
|
];
|
|
|
|
interface SortableTodoItemProps {
|
|
todo: Todo;
|
|
onToggle: (id: string) => void;
|
|
}
|
|
|
|
const SortableTodoItem = ({ todo, onToggle }: SortableTodoItemProps) => {
|
|
const {
|
|
attributes,
|
|
listeners,
|
|
setNodeRef,
|
|
transform,
|
|
transition,
|
|
isDragging,
|
|
} = useSortable({ id: todo.id });
|
|
|
|
const style = {
|
|
transform: CSS.Translate.toString(transform),
|
|
transition,
|
|
opacity: isDragging ? 0.5 : 1,
|
|
};
|
|
|
|
return (
|
|
<div ref={setNodeRef} style={style}>
|
|
<Item
|
|
variant={todo.completed ? "muted" : "default"}
|
|
className={cn(
|
|
"cursor-pointer items-center border-b transition-opacity",
|
|
todo.completed && "opacity-60",
|
|
)}
|
|
onClick={() => onToggle(todo.id)}
|
|
>
|
|
<ItemMedia>
|
|
<div className="flex items-center gap-2">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
{...attributes}
|
|
{...listeners}
|
|
className="cursor-grab active:cursor-grabbing"
|
|
onClick={(e) => e.stopPropagation()}
|
|
aria-label={`جابهجایی «${todo.title}»`}
|
|
>
|
|
<GripVertical className="size-4 text-muted-foreground" />
|
|
</Button>
|
|
<Checkbox
|
|
checked={todo.completed}
|
|
onCheckedChange={() => onToggle(todo.id)}
|
|
onClick={(e) => e.stopPropagation()}
|
|
aria-label={
|
|
todo.completed
|
|
? `علامتگذاری «${todo.title}» بهعنوان انجامنشده`
|
|
: `علامتگذاری «${todo.title}» بهعنوان انجامشده`
|
|
}
|
|
/>
|
|
</div>
|
|
</ItemMedia>
|
|
<ItemContent>
|
|
<ItemTitle
|
|
className={
|
|
todo.completed ? "text-muted-foreground line-through" : ""
|
|
}
|
|
>
|
|
{todo.title}
|
|
</ItemTitle>
|
|
</ItemContent>
|
|
</Item>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const Todo2 = ({
|
|
heading = "مدیریت وظایف با کشیدن و رها کردن",
|
|
description =
|
|
"ترتیب وظایف را با کشیدن تغییر دهید و برای تکمیل، روی آنها کلیک کنید.",
|
|
initialTodos = defaultTodos,
|
|
className,
|
|
}: Todo2Props) => {
|
|
const [todos, setTodos] = useState(initialTodos);
|
|
const [activeTodo, setActiveTodo] = useState<Todo | null>(null);
|
|
|
|
const sensors = useSensors(
|
|
useSensor(PointerSensor),
|
|
useSensor(KeyboardSensor, {
|
|
coordinateGetter: sortableKeyboardCoordinates,
|
|
}),
|
|
);
|
|
|
|
const toggleTodo = (id: string) => {
|
|
setTodos((prevTodos) =>
|
|
prevTodos.map((todo) =>
|
|
todo.id === id ? { ...todo, completed: !todo.completed } : todo,
|
|
),
|
|
);
|
|
};
|
|
|
|
const handleDragStart = (event: DragStartEvent) => {
|
|
const { active } = event;
|
|
const todo = todos.find((t) => t.id === active.id);
|
|
setActiveTodo(todo || null);
|
|
};
|
|
|
|
const handleDragEnd = (event: DragEndEvent) => {
|
|
const { active, over } = event;
|
|
|
|
if (over && active.id !== over.id) {
|
|
setTodos((items) => {
|
|
const oldIndex = items.findIndex((item) => item.id === active.id);
|
|
const newIndex = items.findIndex((item) => item.id === over.id);
|
|
return arrayMove(items, oldIndex, newIndex);
|
|
});
|
|
}
|
|
|
|
setActiveTodo(null);
|
|
};
|
|
|
|
return (
|
|
<section dir="rtl" lang="fa" className={cn("py-32", className)}>
|
|
<div className="container">
|
|
<div className="mx-auto flex max-w-2xl flex-col gap-8">
|
|
<div className="flex flex-col gap-2">
|
|
<h2 className="text-3xl font-semibold md:text-xl">{heading}</h2>
|
|
<p className="text-muted-foreground">{description}</p>
|
|
</div>
|
|
<DndContext
|
|
sensors={sensors}
|
|
collisionDetection={closestCenter}
|
|
onDragStart={handleDragStart}
|
|
onDragEnd={handleDragEnd}
|
|
>
|
|
<SortableContext
|
|
items={todos.map((t) => t.id)}
|
|
strategy={verticalListSortingStrategy}
|
|
>
|
|
<ItemGroup className="max-w-2xl">
|
|
{todos.map((todo) => (
|
|
<SortableTodoItem
|
|
key={todo.id}
|
|
todo={todo}
|
|
onToggle={toggleTodo}
|
|
/>
|
|
))}
|
|
</ItemGroup>
|
|
</SortableContext>
|
|
<DragOverlay>
|
|
{activeTodo ? (
|
|
<Item
|
|
variant={activeTodo.completed ? "muted" : "default"}
|
|
className="cursor-grabbing items-center border-b opacity-90 shadow-lg"
|
|
>
|
|
<ItemMedia>
|
|
<div className="flex items-center gap-2">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className="cursor-grabbing"
|
|
aria-hidden
|
|
tabIndex={-1}
|
|
>
|
|
<GripVertical className="size-4 text-muted-foreground" />
|
|
</Button>
|
|
<Checkbox
|
|
checked={activeTodo.completed}
|
|
aria-hidden
|
|
tabIndex={-1}
|
|
/>
|
|
</div>
|
|
</ItemMedia>
|
|
<ItemContent>
|
|
<ItemTitle
|
|
className={
|
|
activeTodo.completed
|
|
? "text-muted-foreground line-through"
|
|
: ""
|
|
}
|
|
>
|
|
{activeTodo.title}
|
|
</ItemTitle>
|
|
</ItemContent>
|
|
</Item>
|
|
) : null}
|
|
</DragOverlay>
|
|
</DndContext>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
);
|
|
};
|
|
|
|
export { Todo2 };
|