Skip to content

Instantly share code, notes, and snippets.

@lega911
Created July 8, 2021 01:38
Show Gist options
  • Save lega911/e668fb17894a3a6993d109c9ee235003 to your computer and use it in GitHub Desktop.
Save lega911/e668fb17894a3a6993d109c9ee235003 to your computer and use it in GitHub Desktop.
Todo example
<script>
let name = '';
let todos = [{name: 'Linux'}, {name: 'Ubuntu', done: true}];
let active;
function add() {
if(!name) return;
todos.push({name});
name = '';
}
const remove = i => {
todos.splice(i, 1);
}
const reverse = todo => {
todo.name = todo.name.split("").reverse().join("");
}
$: numDone = todos.filter(t => t.done).length;
</script>
{#if active}
Edit: <input type="text" @keydown|enter={active=null} :value={active.name} />
{:else}
<input type="text" @keydown|enter={add} :value={name} />
{/if}
<ul>
{#each todos as todo, index (todo) }
<li class:active={todo == active} class:inactive={todo.done}>
<input type="checkbox" :checked={todo.done} />
<span @click={active=todo}>{index}: {todo.name}</span>
<a href @click|prevent={remove(index)}>remove</a>
<a href @click|prevent={reverse(todo)}>reverse</a>
</li>
{/each}
</ul>
Total done: {numDone} of {todos.length}
<style>
li {cursor: pointer;}
.active {background-color: #cfc;}
.inactive {text-decoration-line: line-through; color: gray;}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment