11<script setup lang="ts">
2- import { ref } from ' vue/vapor'
3-
2+ import { computed } from ' vue/vapor'
3+ import { useLocalStorage } from ' @vueuse/core '
44interface Task {
55 title: string
66 completed: boolean
77}
8- const tasks = ref <Task []>([])
9- const value = ref (' hello' )
8+
9+ const tasks = useLocalStorage <Task []>(' tasks' , [])
10+ const value = useLocalStorage (' value' , ' ' )
11+
12+ const remaining = computed (() => {
13+ return tasks .value .filter (task => ! task .completed ).length
14+ })
1015
1116function handleAdd() {
1217 tasks .value .push ({
@@ -16,31 +21,76 @@ function handleAdd() {
1621 // TODO: clear input
1722 value .value = ' '
1823}
24+
25+ function handleComplete(index : number , evt : Event ) {
26+ tasks .value [index ].completed = (evt .target as HTMLInputElement ).checked
27+ }
28+
29+ function handleClearComplete() {
30+ tasks .value = tasks .value .filter (task => ! task .completed )
31+ }
32+
33+ function handleClearAll() {
34+ tasks .value = []
35+ }
1936 </script >
2037
2138<template >
39+ <h1 >todos</h1 >
2240 <ul >
2341 <!-- TODO: v-for -->
24- <li >
25- <!-- TODO checked=false -->
26- <input type =" checkbox" :checked =" tasks[0]?.completed" />
42+ <li v-show =" tasks[0]" :class =" { del: tasks[0]?.completed }" >
43+ <input
44+ type =" checkbox"
45+ :checked =" tasks[0]?.completed"
46+ @change =" handleComplete(0, $event)"
47+ />
2748 {{ tasks[0]?.title }}
2849 </li >
29- <li >
30- <input type =" checkbox" :checked =" tasks[1]?.completed" />
50+ <li v-show =" tasks[1]" :class =" { del: tasks[1]?.completed }" >
51+ <input
52+ type =" checkbox"
53+ :checked =" tasks[1]?.completed"
54+ @change =" handleComplete(1, $event)"
55+ />
3156 {{ tasks[1]?.title }}
3257 </li >
33- <li >
34- <input type =" checkbox" :checked =" tasks[2]?.completed" />
58+ <li v-show =" tasks[2]" :class =" { del: tasks[2]?.completed }" >
59+ <input
60+ type =" checkbox"
61+ :checked =" tasks[2]?.completed"
62+ @change =" handleComplete(2, $event)"
63+ />
3564 {{ tasks[2]?.title }}
3665 </li >
37- <li >
38- <input type =" checkbox" :checked =" tasks[3]?.completed" />
66+ <li v-show =" tasks[3]" :class =" { del: tasks[3]?.completed }" >
67+ <input
68+ type =" checkbox"
69+ :checked =" tasks[3]?.completed"
70+ @change =" handleComplete(3, $event)"
71+ />
3972 {{ tasks[3]?.title }}
4073 </li >
41- <li >
42- <input type =" text" v-model =" value" />
43- <button @click =" handleAdd" >Add</button >
44- </li >
4574 </ul >
75+ <p >
76+ {{ remaining }} item{{ remaining !== 1 ? 's' : '' }} left /
77+ {{ tasks.length }} item{{ tasks.length !== 1 ? 's' : '' }} in total
78+ </p >
79+ <div style =" display : flex ; gap : 8px " >
80+ <input
81+ type =" text"
82+ v-model =" value"
83+ @keydown.enter =" handleAdd"
84+ placeholder =" What need to be done?"
85+ />
86+ <button @click =" handleAdd" >Add</button >
87+ <button @click =" handleClearComplete" >Clear completed</button >
88+ <button @click =" handleClearAll" >Clear all</button >
89+ </div >
4690</template >
91+
92+ <style >
93+ .del {
94+ text-decoration : line-through ;
95+ }
96+ </style >
0 commit comments