vue3-core/playground/src/todo-mvc.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
1.8 KiB
Vue
Raw Normal View History

<script setup lang="ts">
2024-01-25 16:49:34 +08:00
import { computed } from 'vue/vapor'
import { useLocalStorage } from '@vueuse/core'
interface Task {
title: string
completed: boolean
}
2024-01-25 16:49:34 +08:00
const tasks = useLocalStorage<Task[]>('tasks', [])
const value = useLocalStorage('value', '')
const remaining = computed(() => {
return tasks.value.filter(task => !task.completed).length
})
2024-01-20 13:38:20 +08:00
function handleAdd() {
tasks.value.push({
title: value.value,
completed: false,
})
// TODO: clear input
value.value = ''
}
2024-01-25 16:49:34 +08:00
function handleComplete(index: number, evt: Event) {
tasks.value[index].completed = (evt.target as HTMLInputElement).checked
}
function handleClearComplete() {
tasks.value = tasks.value.filter(task => !task.completed)
}
function handleClearAll() {
tasks.value = []
}
2024-01-31 17:00:19 +08:00
function handleRemove(idx: number, task: Task) {
console.log(task)
tasks.value.splice(idx, 1)
}
</script>
2024-01-20 13:38:20 +08:00
<template>
2024-01-25 16:49:34 +08:00
<h1>todos</h1>
2024-01-20 13:38:20 +08:00
<ul>
2024-01-31 17:00:19 +08:00
<li
v-for="(task, index) of tasks"
:key="index"
:class="{ del: task.completed }"
>
2024-01-25 16:49:34 +08:00
<input
type="checkbox"
2024-01-31 17:00:19 +08:00
:checked="task.completed"
@change="handleComplete(index, $event)"
2024-01-25 16:49:34 +08:00
/>
2024-01-31 17:00:19 +08:00
{{ task.title }}
<button @click="handleRemove(index, task)">x</button>
2024-01-20 13:38:20 +08:00
</li>
</ul>
2024-01-25 16:49:34 +08:00
<p>
{{ remaining }} item{{ remaining !== 1 ? 's' : '' }} left /
{{ tasks.length }} item{{ tasks.length !== 1 ? 's' : '' }} in total
</p>
<div style="display: flex; gap: 8px">
<input
type="text"
v-model="value"
@keydown.enter="handleAdd"
placeholder="What need to be done?"
/>
<button @click="handleAdd">Add</button>
<button @click="handleClearComplete">Clear completed</button>
<button @click="handleClearAll">Clear all</button>
</div>
2024-01-20 13:38:20 +08:00
</template>
2024-01-25 16:49:34 +08:00
<style>
.del {
text-decoration: line-through;
}
</style>