Files
reui-blocks/blocks/todo4.tsx
T

355 lines
10 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, Plus, X } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import {
Item,
ItemContent,
ItemGroup,
ItemMedia,
ItemTitle,
} from "@/components/ui/item";
import { cn } from "@/lib/utils";
interface Todo {
id: string;
title: string;
completed: boolean;
}
interface Todo4Props {
heading?: string;
description?: string;
addTaskLabel?: string;
taskPlaceholder?: string;
cancelLabel?: string;
submitLabel?: string;
initialTodos?: Todo[];
className?: string;
}
const defaultTodos: Todo[] = [
{
id: "1",
title: "تکمیل مستندات فنی پروژه",
completed: false,
},
{
id: "2",
title: "بررسی و تأیید طراحی رابط کاربری",
completed: false,
},
{
id: "3",
title: "آماده‌سازی گزارش هفتگی",
completed: false,
},
];
interface SortableTodoItemProps {
todo: Todo;
onToggle: (id: string) => void;
isNew?: boolean;
}
const SortableTodoItem = ({
todo,
onToggle,
isNew = false,
}: 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}
className={isNew ? "animate-in duration-500 fade-in" : ""}
>
<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 Todo4 = ({
heading = "مدیریت وظایف با افزودن وظیفه جدید",
description =
"وظیفه جدید اضافه کنید، ترتیب را با کشیدن تغییر دهید و وضعیت تکمیل را علامت بزنید.",
addTaskLabel = "افزودن وظیفه جدید",
taskPlaceholder = "عنوان وظیفه",
cancelLabel = "انصراف",
submitLabel = "افزودن",
initialTodos = defaultTodos,
className,
}: Todo4Props) => {
const [todos, setTodos] = useState(initialTodos);
const [isFormExpanded, setIsFormExpanded] = useState(false);
const [newTaskTitle, setNewTaskTitle] = useState("");
const [activeTodo, setActiveTodo] = useState<Todo | null>(null);
const [newTodoIds, setNewTodoIds] = useState<Set<string>>(new Set());
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 handleAddTask = () => {
if (newTaskTitle.trim()) {
const newTodo: Todo = {
id: Date.now().toString(),
title: newTaskTitle.trim(),
completed: false,
};
setTodos((prevTodos) => [...prevTodos, newTodo]);
setNewTodoIds((prev) => new Set(prev).add(newTodo.id));
setNewTaskTitle("");
setIsFormExpanded(false);
setTimeout(() => {
setNewTodoIds((prev) => {
const updated = new Set(prev);
updated.delete(newTodo.id);
return updated;
});
}, 500);
}
};
const handleCancel = () => {
setNewTaskTitle("");
setIsFormExpanded(false);
};
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}
>
<div className="flex max-w-2xl flex-col gap-4">
{!isFormExpanded ? (
<Button
variant="ghost"
size="lg"
className="w-full justify-start gap-2"
onClick={() => setIsFormExpanded(true)}
>
<Plus className="h-4 w-4" aria-hidden />
{addTaskLabel}
</Button>
) : (
<div className="flex flex-col gap-2 rounded-lg border p-4">
<Input
autoFocus
placeholder={taskPlaceholder}
value={newTaskTitle}
onChange={(e) => setNewTaskTitle(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && newTaskTitle.trim()) {
handleAddTask();
}
if (e.key === "Escape") {
handleCancel();
}
}}
className="text-base font-medium"
aria-label={taskPlaceholder}
/>
<div className="flex items-center justify-end gap-2">
<Button
variant="secondary"
size="sm"
onClick={handleCancel}
>
<X className="h-4 w-4" aria-hidden />
{cancelLabel}
</Button>
<Button
size="sm"
onClick={handleAddTask}
disabled={!newTaskTitle.trim()}
>
<Plus className="h-4 w-4" aria-hidden />
{submitLabel}
</Button>
</div>
</div>
)}
<SortableContext
items={todos.map((t) => t.id)}
strategy={verticalListSortingStrategy}
>
<ItemGroup>
{todos.map((todo) => (
<SortableTodoItem
key={todo.id}
todo={todo}
onToggle={toggleTodo}
isNew={newTodoIds.has(todo.id)}
/>
))}
</ItemGroup>
</SortableContext>
</div>
<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 { Todo4 };