1
1
<script setup lang="ts">
2
- import { ref } from ' vue/vapor'
3
-
2
+ import { computed } from ' vue/vapor'
3
+ import { useLocalStorage } from ' @vueuse/core '
4
4
interface Task {
5
5
title: string
6
6
completed: boolean
7
7
}
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
+ })
10
15
11
16
function handleAdd() {
12
17
tasks .value .push ({
@@ -16,31 +21,76 @@ function handleAdd() {
16
21
// TODO: clear input
17
22
value .value = ' '
18
23
}
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
+ }
19
36
</script >
20
37
21
38
<template >
39
+ <h1 >todos</h1 >
22
40
<ul >
23
41
<!-- 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
+ />
27
48
{{ tasks[0]?.title }}
28
49
</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
+ />
31
56
{{ tasks[1]?.title }}
32
57
</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
+ />
35
64
{{ tasks[2]?.title }}
36
65
</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
+ />
39
72
{{ tasks[3]?.title }}
40
73
</li >
41
- <li >
42
- <input type =" text" v-model =" value" />
43
- <button @click =" handleAdd" >Add</button >
44
- </li >
45
74
</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 >
46
90
</template >
91
+
92
+ <style >
93
+ .del {
94
+ text-decoration : line-through ;
95
+ }
96
+ </style >
0 commit comments