-
Notifications
You must be signed in to change notification settings - Fork 1
/
TodoListRenderless.vue
106 lines (103 loc) · 2.9 KB
/
TodoListRenderless.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<template>
<section>
<DejaVue
v-slot="{ getHistory, getCursor, undo, redo, canUndo, canRedo }"
:record.sync="todos"
:objectHash="(obj) => obj.id"
>
<AddTodo @add="addTodo" />
<nav class="grid grid-flow-col gap-4 mt-4">
<button
:disabled="!canUndo"
class="flex items-center justify-center rounded py-4 px-6 text-sm font-bold text-gray-600 focus:outline-none"
:class="{
'opacity-50': !canUndo,
'bg-gray-300 hover:bg-gray-400 focus:bg-gray-400 active:bg-gray-400': canUndo,
}"
@click="undo"
>
<svg class="mr-4" width="14" height="14" viewBox="0 0 24 24">
<path
fill="currentColor"
transform="translate(24 0) scale(-1 1) "
d="M14 12l-14 9v-18l14 9zm-4-9v4l8.022 5-8.022 5v4l14-9-14-9z"
/>
</svg>
Undo
</button>
<button
:disabled="!canRedo"
class="flex items-center justify-center rounded py-4 px-6 text-sm font-bold text-gray-600 focus:outline-none"
:class="{
'opacity-50': !canRedo,
'bg-gray-300 hover:bg-gray-400 focus:bg-gray-400 active:bg-gray-400': canRedo,
}"
@click="redo"
>
Redo
<svg class="ml-4" width="14" height="14" viewBox="0 0 24 24">
<path fill="currentColor" d="M14 12l-14 9v-18l14 9zm-4-9v4l8.022 5-8.022 5v4l14-9-14-9z" />
</svg>
</button>
</nav>
<transition-group
v-if="getSortedTodos"
name="list"
tag="ul"
class="mt-8 relative"
:style="`--t: ${getSortedTodos.length}`"
>
<TodoItem
v-for="(todo, index) in getSortedTodos"
:key="todo.id"
class="todo"
:style="`--i: ${index}`"
:class="{ 'mt-4': index > 0 }"
:todo="todo"
@check="todo.done = $event"
@update="todo.text = $event"
@remove="removeTodo(todo)"
/>
</transition-group>
</DejaVue>
</section>
</template>
<script>
import { uniqueId, partition } from 'lodash';
import DejaVue from '@/libs/DejaVue';
import AddTodo from '@/components/AddTodo';
import TodoItem from '@/components/TodoItem';
export default {
components: {
AddTodo,
TodoItem,
DejaVue,
},
data: () => ({
todos: ['Milk', 'Eggs', 'Bread', 'Chocolate', 'Cake'].map((text, i) => ({
text,
done: i > 2,
id: uniqueId(),
})),
}),
computed: {
getSortedTodos() {
const [a, b] = partition(this.todos, 'done');
return b.concat(a);
},
},
methods: {
addTodo(text) {
this.todos.unshift({
text,
done: false,
id: uniqueId(),
});
},
removeTodo(todo) {
const index = this.todos.findIndex((t) => t.id === todo.id);
this.todos.splice(index, 1);
},
},
};
</script>