Todo.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <li class="todo" :class="{ completed: todo.done, editing: editing }">
  3. <div class="view">
  4. <input class="toggle"
  5. type="checkbox"
  6. :checked="todo.done"
  7. @change="toggleTodo(todo)">
  8. <label v-text="todo.text" @dblclick="editing = true"></label>
  9. <button class="destroy" @click="deleteTodo(todo)"></button>
  10. </div>
  11. <input class="edit"
  12. v-show="editing"
  13. v-focus="editing"
  14. :value="todo.text"
  15. @keyup.enter="doneEdit"
  16. @keyup.esc="cancelEdit"
  17. @blur="doneEdit">
  18. </li>
  19. </template>
  20. <script>
  21. export default {
  22. props: ['todo'],
  23. data () {
  24. return {
  25. editing: false
  26. }
  27. },
  28. directives: {
  29. focus (el, { value }, { context }) {
  30. if (value) {
  31. context.$nextTick(() => {
  32. el.focus()
  33. })
  34. }
  35. }
  36. },
  37. methods: {
  38. toggleTodo (todo) {
  39. this.$store.call('toggleTodo', todo)
  40. },
  41. doneEdit (e) {
  42. const value = e.target.value.trim()
  43. if (!value) {
  44. this.$store.call('deleteTodo', this.todo)
  45. } else if (this.editing) {
  46. this.$store.call('editTodo', this.todo, value)
  47. this.editing = false
  48. }
  49. },
  50. cancelEdit (e) {
  51. e.target.value = this.todo.text
  52. this.editing = false
  53. }
  54. }
  55. }
  56. </script>