svelte-playground/src/lib/apps/todoApp.svelte

136 lines
2.9 KiB
Svelte
Raw Normal View History

2021-08-01 21:33:31 +02:00
<script>
2021-08-01 22:22:34 +02:00
import {todos} from "$lib/stores/todos.js";
let todo_text = "";
2021-08-01 21:33:31 +02:00
let addTodo = () => {
if (todo_text.length > 0) {
const todo = {
text: todo_text,
date: Date.now,
done: false
}
$todos = [todo, ...$todos];
}
}
let clearTodo = (todo) => {
if(todo.done)
$todos = $todos.filter(x => x != todo)
else
todo.done = true;
$todos = $todos;
}
2021-08-01 22:22:34 +02:00
let filterTodo = (type) => {
switch (type) {
case 'active':
filteredTodo = $todos.filter(todo => !todo.done);
break;
case 'done':
filteredTodo = $todos.filter(todo => todo.done);
break;
case 'all':
filteredTodo = $todos;
break;
}
}
$: filteredTodo = $todos
2021-08-01 21:33:31 +02:00
</script>
<div class = "panel">
2021-08-01 22:22:34 +02:00
<h2 class= "title">TODO</h2>
<hr/>
2021-08-01 21:33:31 +02:00
<input bind:value="{todo_text}"/>
<button on:click="{addTodo}">Add</button>
2021-08-01 22:22:34 +02:00
<div class="todo-filter">
<button on:click="{() => filterTodo('active')}">active</button>
<button on:click="{() => filterTodo('done')}">done</button>
<button on:click="{() => filterTodo('all')}">all</button>
2021-08-01 21:33:31 +02:00
</div>
2021-08-01 22:22:34 +02:00
<ul class = "todo-list">
{#each filteredTodo as todo}
<li class = "todo {todo.done ? 'done' : ''}">
<p>{todo.text}</p>
<button on:click="{() => clearTodo(todo)}">{todo.done ? '✗' : '✓'}</button>
</li>
{/each}
</ul>
2021-08-01 21:33:31 +02:00
</div>
<style>
input {
padding: 5px;
margin: 0px;
2021-08-01 22:22:34 +02:00
border: solid rgb(161, 161, 161) 1px;
2021-08-01 21:33:31 +02:00
border-radius: 5px;
}
button {
padding: 5px;
margin: 0px;
border: solid rgb(182, 182, 182) 1px;
border-radius: 5px;
}
2021-08-01 22:22:34 +02:00
hr {
border: 1px solid rgb(182, 182, 182);
}
2021-08-01 21:33:31 +02:00
.panel {
background-color: rgb(245, 253, 255);
padding: 10px;
border: solid rgb(182, 182, 182) 1px;
border-radius: 5px;
}
.title {
2021-08-01 22:22:34 +02:00
padding: 0px;
2021-08-01 21:33:31 +02:00
margin: 0px;
margin-left: 5px;
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: 400;
font-size: 1.5rem;
}
2021-08-01 22:22:34 +02:00
.todo-filter {
padding-top: 10px;
padding-bottom: 10px;
display: flex;
align-items: center;
justify-content: center;
}
.todo-filter button {
margin-right: 5px;
}
2021-08-01 21:33:31 +02:00
/* .todo-list {
padding: 5px;
background-color: azure;
display: flex;
flex-direction: column;
} */
.todo {
padding: 10px;
margin: 5px 0px 5px 0px;
background-color: rgb(255, 255, 255);
border: solid rgb(182, 182, 182) 1px;
border-radius: 5px;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.done p {
text-decoration: line-through
}
.todo p {
padding: 0px;
margin: 0px 10px 0px 0px;
}
.todo button {
margin-left: auto;
}
</style>