-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
Copy pathTodoItem.vue
68 lines (59 loc) · 1.54 KB
/
TodoItem.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<template>
<li class="todo" :class="{ completed: todo.done, editing }">
<div class="view">
<input class="toggle"
type="checkbox"
:checked="todo.done"
@change="toggleTodo(todo)">
<label v-text="todo.text" @dblclick="editing = true"></label>
<button class="destroy" @click="removeTodo(todo)"></button>
</div>
<input class="edit"
v-show="editing"
:value="todo.text"
ref="input"
@keyup.enter="doneEdit"
@keyup.esc="cancelEdit"
@blur="doneEdit">
</li>
</template>
<script>
import { ref, watch, nextTick } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'Todo',
props: ['todo'],
setup (props) {
const input = ref(null)
const editing = ref(false)
watch(editing, (v) => {
v && nextTick(() => { input.value.focus() })
})
const store = useStore()
const editTodo = (todo, value) => store.dispatch('editTodo', { todo, value })
const toggleTodo = (todo) => store.dispatch('toggleTodo', todo)
const removeTodo = (todo) => store.dispatch('removeTodo', todo)
function doneEdit (e) {
const value = e.target.value.trim()
if (!value) {
removeTodo(props.todo)
} else if (editing.value) {
editTodo(props.todo, value)
}
editing.value = false
}
function cancelEdit (e) {
e.target.value = props.todo.text
editing.value = false
}
return {
input,
editing,
toggleTodo,
doneEdit,
cancelEdit,
removeTodo
}
}
}
</script>