Vue
by flek
HTML
<div id="app">
<input
type="text"
placeholder="what needs to be done?"
v-on:keydown="keydownHandler"
/>
<h2>Todos:</h2>
<ol>
<li v-for="todo in todos" :key="todo">
<label>
<input
type="checkbox"
v-on:change="check(todo)" />
{{ todo }}
<button v-on:click="remove(todo)">remove</button>
</label>
</li>
</ol>
<h2>Dones:</h2>
<ol>
<li v-for="done in dones">
<del>{{ done }}</del>
</li>
<button v-if="dones.length" v-on:click="clear">clear</button>
</ol>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
Vue
// Application code
const state = {
todos: [],
dones: [],
};
function keydownHandler(event) {
if (event.key === 'Enter') {
add(event.target.value);
event.target.value = '';
}
}
function add(todo) {
state.todos = [todo, ...state.todos];
}
function remove(todo) {
state.todos = state.todos.filter(t => t !== todo);
}
function check(todo) {
remove(todo);
state.dones = [...state.dones, todo];
}
function clear() {
state.dones = [];
}
new Vue({
el: "#app",
data: state,
methods: {
keydownHandler,
add,
remove,
check,
clear
}
})