1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <template>
- <li class="todo" :class="{ completed: todo.done, editing: editing }">
- <div class="view">
- <input class="toggle"
- type="checkbox"
- :checked="todo.done"
- @change="toggleTodo({ todo: todo })">
- <label v-text="todo.text" @dblclick="editing = true"></label>
- <button class="destroy" @click="deleteTodo({ todo: todo })"></button>
- </div>
- <input class="edit"
- v-show="editing"
- v-focus="editing"
- :value="todo.text"
- @keyup.enter="doneEdit"
- @keyup.esc="cancelEdit"
- @blur="doneEdit">
- </li>
- </template>
- <script>
- import { mapMutations } from 'vuex'
- export default {
- name: 'Todo',
- props: ['todo'],
- data () {
- return {
- editing: false
- }
- },
- directives: {
- focus (el, { value }, { context }) {
- if (value) {
- context.$nextTick(() => {
- el.focus()
- })
- }
- }
- },
- methods: {
- ...mapMutations([
- 'editTodo',
- 'toggleTodo',
- 'deleteTodo'
- ]),
- doneEdit (e) {
- const value = e.target.value.trim()
- const { todo } = this
- if (!value) {
- this.deleteTodo({
- todo
- })
- } else if (this.editing) {
- this.editTodo({
- todo,
- value
- })
- this.editing = false
- }
- },
- cancelEdit (e) {
- e.target.value = this.todo.text
- this.editing = false
- }
- }
- }
- </script>
|