看板分为三个模块,分别是待办,正在做,已做完三个部分。每个事件采取"卡片"式设计,支持任务间拖拽,删除等操作。
import React, { useState } from 'react';
import { Card, CardContent } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
const KanbanBoard = () => {
const [tasks, setTasks] = useState({
todo: [
{ id: '1', title: '设计新的登录页面' },
{ id: '2', title: '优化数据库查询' }
],
doing: [
{ id: '3', title: '实现用户认证功能' },
{ id: '4', title: '编写单元测试' }
],
done: [
{ id: '5', title: '更新API文档' },
{ id: '6', title: '修复登录bug' }
]
});
const [newTasks, setNewTasks] = useState({
todo: '',
doing: '',
done: ''
});
const onDragStart = (e, id, sourceColumn) => {
e.dataTransfer.setData('text/plain', JSON.stringify({ id, sourceColumn }));
};
const onDragOver = (e) => {
e.preventDefault();
};
const onDrop = (e, targetColumn) => {
e.preventDefault();
const { id, sourceColumn } = JSON.parse(e.dataTransfer.getData('text'));
if (sourceColumn === targetColumn) return;
const updatedTasks = { ...tasks };
const taskToMove = updatedTasks[sourceColumn].find(task => task.id === id);
updatedTasks[sourceColumn] = updatedTasks[sourceColumn].filter(task => task.id !== id);
updatedTasks[targetColumn].push(taskToMove);
setTasks(updatedTasks);
};
const addTask = (column) => {
if (newTasks[column].trim()) {
const updatedTasks = { ...tasks };
updatedTasks[column] = [...tasks[column], {
id: Date.now().toString(),
title: newTasks[column]
}];
setTasks(updatedTasks);
setNewTasks({...newTasks, [column]: ''});
}
};
const deleteTask = (columnId, taskId) => {
const updatedTasks = { ...tasks };
updatedTasks[columnId] = tasks[columnId].filter(task => task.id !== taskId);
setTasks(updatedTasks);
};
const renderTask = (task, columnId) => (
onDragStart(e, task.id, columnId)}
className="mb-2 shadow-sm hover:shadow-md transition-shadow duration-200"
>
<CardContent className="p-3 flex justify-between items-center">
<p className="text-sm font-medium text-gray-700 truncate flex-grow mr-2">{task.title}p>