added todo filtering

This commit is contained in:
Giulio Alessandrini 2021-08-01 22:22:34 +02:00
parent 82dc3948a5
commit 94ed9867ce
2 changed files with 51 additions and 15 deletions

View File

@ -1,6 +1,6 @@
<script> <script>
import {todos} from "$lib/stores/todos.js" import {todos} from "$lib/stores/todos.js";
let todo_text = "" let todo_text = "";
let addTodo = () => { let addTodo = () => {
if (todo_text.length > 0) { if (todo_text.length > 0) {
const todo = { const todo = {
@ -18,22 +18,40 @@
todo.done = true; todo.done = true;
$todos = $todos; $todos = $todos;
} }
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
</script> </script>
<div class = "panel"> <div class = "panel">
<h2 class= "title"> <h2 class= "title">TODO</h2>
TODO <hr/>
</h2>
<input bind:value="{todo_text}"/> <input bind:value="{todo_text}"/>
<button on:click="{addTodo}">Add</button> <button on:click="{addTodo}">Add</button>
<div class = "todo-list"> <div class="todo-filter">
{#each $todos as todo} <button on:click="{() => filterTodo('active')}">active</button>
<div class = "todo {todo.done ? 'done' : ''}"> <button on:click="{() => filterTodo('done')}">done</button>
<p>{todo.text}</p> <button on:click="{() => filterTodo('all')}">all</button>
<button on:click="{() => clearTodo(todo)}">{todo.done ? '✗' : '✓'}</button>
</div>
{/each}
</div> </div>
<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>
</div> </div>
<style> <style>
@ -41,7 +59,7 @@
input { input {
padding: 5px; padding: 5px;
margin: 0px; margin: 0px;
border: solid rgb(182, 182, 182) 1px; border: solid rgb(161, 161, 161) 1px;
border-radius: 5px; border-radius: 5px;
} }
@ -52,6 +70,10 @@ button {
border-radius: 5px; border-radius: 5px;
} }
hr {
border: 1px solid rgb(182, 182, 182);
}
.panel { .panel {
background-color: rgb(245, 253, 255); background-color: rgb(245, 253, 255);
padding: 10px; padding: 10px;
@ -60,7 +82,7 @@ button {
} }
.title { .title {
padding: 5px; padding: 0px;
margin: 0px; margin: 0px;
margin-left: 5px; margin-left: 5px;
font-family: Georgia, 'Times New Roman', Times, serif; font-family: Georgia, 'Times New Roman', Times, serif;
@ -68,6 +90,18 @@ button {
font-size: 1.5rem; font-size: 1.5rem;
} }
.todo-filter {
padding-top: 10px;
padding-bottom: 10px;
display: flex;
align-items: center;
justify-content: center;
}
.todo-filter button {
margin-right: 5px;
}
/* .todo-list { /* .todo-list {
padding: 5px; padding: 5px;
background-color: azure; background-color: azure;

View File

@ -10,7 +10,9 @@
</div> </div>
<style> <style>
:global(a) {
:global(*) {
padding: 0;
text-decoration: none; text-decoration: none;
color : black color : black
} }