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

205 lines
4.4 KiB
Svelte

<script>
import {todos} from "$lib/stores/todos.js";
let todo_text = "";
let todo_type = "active";
let filteredTodo = [];
let addTodo = () => {
if (todo_text.length > 0) {
const todo = {
text: todo_text,
date: Date.now,
done: false
}
todo_text = "";
if (todo_type == "done") todo_type = "all";
$todos = [todo, ...$todos];
}
}
let clearTodo = (todo) => {
if(todo.done) {
$todos = $todos.filter(x => x != todo)
}
else {
todo.done = true;
$todos = $todos
}
}
let filterTodo = (type) => {
todo_type = 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;
filterTodo(todo_type)
}
</script>
<div class = "panel">
<h2 class= "title">TODO</h2>
<hr/>
<div class="todo-input">
<input class="todo-text" bind:value="{todo_text}" placeholder="title"/>
<button on:click="{addTodo}" disabled = {todo_text.length == 0} class = "todo-add {todo_text.length == 0 ? 'disabled' : ''}">add</button>
</div>
<div class="todo-filter">
<button on:click="{() => filterTodo('active')}" class = {todo_type == 'active' ? 'disabled' : ''}>active</button>
<button on:click="{() => filterTodo('done')}" class = {todo_type == 'done' ? 'disabled' : ''}>done</button>
<button on:click="{() => filterTodo('all')}" class = {todo_type == 'all' ? 'disabled' : ''}>all</button>
</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>
<style>
* {
--cream: #FEF9EF;
--blue: #227c9d;
--green: #17C3B2;
--yellow: #FFCB77;
--red: #FE6D73;
}
.panel {
padding: 10px;
background-color: var(--cream);
}
@media only screen and (min-width: 500px) {
.panel {
margin-left: auto;
margin-right: auto;
width: 500px;
border: solid var(--cream) 1px;
border-radius: 5px;
}
}
hr {
margin-top: 10px;
margin-bottom: 20px;
border: 1px solid var(--yellow);
}
button {
padding: 5px;
margin: 0px;
background-color: var(--blue);
border: solid var(--blue) 1px;
border-radius: 5px;
color: var(--cream);
}
.todo-input {
font-size: 0px;
}
.todo-text {
padding: 8px;
margin: 0px;
border: solid white 1px;
border-right: 0px;
border-radius: 0px;
border-bottom-left-radius: 5px;
border-top-left-radius: 5px;
}
.todo-text:focus {
outline: none;
}
.todo-add {
padding: 8px;
border-radius: 0px;
border-bottom-right-radius: 5px;
border-top-right-radius: 5px;
}
.disabled {
background-color: var(--yellow);
border-color: var(--yellow);
color: black;
}
button:active {
background-color: var(--yellow);
color: black;
}
.title {
padding: 0px;
margin: 0px;
margin-left: 5px;
font-weight: 400;
font-size: 1.5rem;
color: var(--yellow);
}
.todo-filter {
padding-top: 10px;
padding-bottom: 10px;
display: flex;
align-items: center;
justify-content: center;
}
.todo-filter button {
margin-right: 5px;
}
.todo-list {
display: flex;
flex-direction: column;
min-height: 10em;
}
.todo {
padding: 10px;
margin: 5px 0px 5px 0px;
background-color: var(--yellow);
border-radius: 5px;
display: flex;
flex-wrap: wrap;
align-items: center;
font-size: 1.2rem;
}
.todo.done {
/* background-color: #FEF9EF; */
}
.done p {
/* text-decoration: line-through; */
color: var(--cream);
}
.todo button {
margin-left: auto;
background-color: var(--green);
border-color: var(--green);
font-size: 1rem;
color: var(--cream)
}
.todo.done button {
background-color: var(--red);
border-color: var(--red);
color:var(--cream);
}
</style>