خب، اینجا یه صفحهٔ کامل Front-End با Next.js برات نوشتم که یک «تودولیست خلاقانه» رو پوشش میده.
قابلیتهای اصلی:
jsx Copy code
import { useState, useEffect } from 'react';
// یک شناسه تصادفی ساده
const uuid = () => Math.random().toString(16).slice(2);
export default function Home() {
/* ---------- حالتها ---------- */
const [todos, setTodos] = useState([]);
const [filter, setFilter] = useState('all'); // all | active | done
const [newTask, setNewTask] = useState('');
const [dueDate, setDueDate] = useState('');
const [priority, setPriority] = useState('low');
const [dark, setDark] = useState(false);
/* ---------- Load / Save ---------- */
useEffect(() => {
const saved = JSON.parse(localStorage.getItem('todos')) || [];
const theme = JSON.parse(localStorage.getItem('dark')) || false;
setTodos(saved);
setDark(theme);
}, []);
useEffect(() => {
localStorage.setItem('todos', JSON.stringify(todos));
}, [todos]);
useEffect(() => {
localStorage.setItem('dark', JSON.stringify(dark));
}, [dark]);
/* ---------- CRUD ---------- */
const addTodo = () => {
if (!newTask.trim()) return;
setTodos([
...todos,
{
id: uuid(),
text: newTask.trim(),
completed: false,
dueDate,
priority
}
]);
setNewTask('');
setDueDate('');
setPriority('low');
};
const toggleTodo = id =>
setTodos(
todos.map(t => (t.id === id ? { ...t, completed: !t.completed } : t))
);
const deleteTodo = id => setTodos(todos.filter(t => t.id !== id));
const editTodo = (id, text) =>
setTodos(todos.map(t => (t.id === id ? { ...t, text } : t)));
/* ---------- فیلتر ---------- */
const filtered = todos.filter(t =>
filter === 'active'
? !t.completed
: filter === 'done'
? t.completed
: true
);
/* ---------- رندر ---------- */
return (
<div className={dark ? 'container dark' : 'container'}>
<h1>📝 TODO List</h1>
{/* Toggle Theme */}
<button className="themeBtn" onClick={() => setDark(!dark)}>
{dark ? '🌞 حالت روشن' : '🌚 حالت تیره'}
</button>
{/* فرم افزودن تسک */}
<div className="new">
<input
placeholder="تسک جدید..."
value={newTask}
onChange={e => setNewTask(e.target.value)}
onKeyDown={e => e.key === 'Enter' && addTodo()}
/>
<input
type="date"
value={dueDate}
onChange={e => setDueDate(e.target.value)}
/>
<select value={priority} onChange={e => setPriority(e.target.value)}>
<option value="low">Low</option>
<option value="mid">Medium</option>
<option value="high">High</option>
</select>
<button onClick={addTodo}>Add</button>
</div>
{/* فیلترها */}
<div className="filters">
<button
className={filter === 'all' ? 'active' : ''}
onClick={() => setFilter('all')}>
همه
</button>
<button
className={filter === 'active' ? 'active' : ''}
onClick={() => setFilter('active')}>
فعال
</button>
<button
className={filter === 'done' ? 'active' : ''}
onClick={() => setFilter('done')}>
تمامشده
</button>
</div>
{/* لیست تسکها */}
<ul>
{filtered.map(t => (
<TodoItem
key={t.id}
todo={t}
toggleTodo={toggleTodo}
deleteTodo={deleteTodo}
editTodo={editTodo}
/>
))}
{filtered.length === 0 && <p>🤷♂️ چیزی برای نمایش نیست!</p>}
</ul>
<p className="left">
{todos.filter(t => !t.completed).length} تسکِ باقیمانده
</p>
{/* استایل */}
<style jsx>{`
* {
font-family: sans-serif;
transition: all 0.2s;
}
.container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
border-radius: 12px;
background: #fafafa;
color: #222;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.07);
}
.dark {
background: #1e1e1e;
color: #ddd;
}
h1 {
text-align: center;
}
.themeBtn {
float: right;
margin-top: -40px;
background: transparent;
border: none;
cursor: pointer;
font-size: 18px;
}
.new {
display: flex;
gap: 8px;
margin-bottom: 20px;
}
input,
select {
padding: 8px;
flex: 1;
border: 1px solid #ccc;
border-radius: 6px;
}
select {
flex: 0.6;
}
button {
padding: 8px 14px;
background: #0070f3;
color: #fff;
border: none;
border-radius: 6px;
cursor: pointer;
}
button:hover {
background: #0053b0;
}
ul {
list-style: none;
padding: 0;
}
.filters {
margin-bottom: 10px;
}
.filters button {
background: transparent;
border: 1px solid #0070f3;
color: #0070f3;
margin-right: 5px;
}
.filters .active,
.filters button:hover {
background: #0070f3;
color: #fff;
}
.left {
margin-top: 10px;
font-weight: bold;
text-align: center;
}
`}</style>
</div>
);
}
/* -------------- کامپوننت آیتم -------------- */
function TodoItem({ todo, toggleTodo, deleteTodo, editTodo }) {
const [editing, setEditing] = useState(false);
const [text, setText] = useState(todo.text);
const save = () => {
editTodo(todo.id, text.trim() ? text : todo.text);
setEditing(false);
};
return (
<li className={todo.completed ? 'done' : ''}>
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleTodo(todo.id)}
/>
{/* متن یا فیلد ویرایش */}
{editing ? (
<input
className="editInput"
value={text}
onChange={e => setText(e.target.value)}
onKeyDown={e => e.key === 'Enter' && save()}
onBlur={save}
autoFocus
/>
) : (
<span onDoubleClick={() => setEditing(true)}>{todo.text}</span>
)}
{/* برچسبها */}
{todo.dueDate && (
<small className="due">
⏰ {new Date(todo.dueDate).toLocaleDateString('fa-IR')}
</small>
)}
{todo.priority && (
<span className={`badge ${todo.priority}`}>{todo.priority}</span>
)}
<button className="delete" onClick={() => deleteTodo(todo.id)}>
❌
</button>
<style jsx>{`
li {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 0;
border-bottom: 1px dashed #ccc;
}
li.done span {
text-decoration: line-through;
opacity: 0.6;
}
span {
flex: 1;
cursor: pointer;
}
.editInput {
flex: 1;
padding: 4px 6px;
}
.delete {
background: transparent;
color: #ff4d4d;
}
.badge {
padding: 2px 6px;
border-radius: 4px;
font-size: 10px;
text-transform: uppercase;
}
.low {
background: #e2e2e2;
color: #333;
}
.mid {
background: #ffd65c;
color: #7a5400;
}
.high {
background: #ff7272;
color: #7a0000;
}
.due {
font-size: 10px;
opacity: 0.7;
}
`}</style>
</li>
);
}
🥳 حالا یک تودولیست جمعوجور ولی باحال داری:
• دوبار روی متن هر تسک بزن تا Inline Edit بشه
• رنگ اولویتها بهصورت برچسب نشون داده میشه
• حالت تیره/روشن رو فراموش نکن!