1
0

TodoItem.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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="removeTodo(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. import { mapActions } from 'vuex'
  22. export default {
  23. name: 'Todo',
  24. props: ['todo'],
  25. data () {
  26. return {
  27. editing: false
  28. }
  29. },
  30. directives: {
  31. focus (el, { value }, { context }) {
  32. if (value) {
  33. context.$nextTick(() => {
  34. el.focus()
  35. })
  36. }
  37. }
  38. },
  39. methods: {
  40. ...mapActions([
  41. 'editTodo',
  42. 'toggleTodo',
  43. 'removeTodo'
  44. ]),
  45. doneEdit (e) {
  46. const value = e.target.value.trim()
  47. const { todo } = this
  48. if (!value) {
  49. this.removeTodo(todo)
  50. } else if (this.editing) {
  51. this.editTodo({
  52. todo,
  53. value
  54. })
  55. this.editing = false
  56. }
  57. },
  58. cancelEdit (e) {
  59. e.target.value = this.todo.text
  60. this.editing = false
  61. }
  62. }
  63. }
  64. </script>