08297189d1139912ddb2857d1093c88e4fe2632e.svn-base 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <li :class="{ completed: todo.done, editing: editing }" class="todo">
  3. <div class="view">
  4. <input
  5. :checked="todo.done"
  6. class="toggle"
  7. type="checkbox"
  8. @change="toggleTodo( todo)"
  9. >
  10. <label @dblclick="editing = true" v-text="todo.text" />
  11. <button class="destroy" @click="deleteTodo( todo )" />
  12. </div>
  13. <input
  14. v-show="editing"
  15. v-focus="editing"
  16. :value="todo.text"
  17. class="edit"
  18. @keyup.enter="doneEdit"
  19. @keyup.esc="cancelEdit"
  20. @blur="doneEdit"
  21. >
  22. </li>
  23. </template>
  24. <script>
  25. export default {
  26. name: 'Todo',
  27. directives: {
  28. focus(el, { value }, { context }) {
  29. if (value) {
  30. context.$nextTick(() => {
  31. el.focus()
  32. })
  33. }
  34. }
  35. },
  36. props: {
  37. todo: {
  38. type: Object,
  39. default: function() {
  40. return {}
  41. }
  42. }
  43. },
  44. data() {
  45. return {
  46. editing: false
  47. }
  48. },
  49. methods: {
  50. deleteTodo(todo) {
  51. this.$emit('deleteTodo', todo)
  52. },
  53. editTodo({ todo, value }) {
  54. this.$emit('editTodo', { todo, value })
  55. },
  56. toggleTodo(todo) {
  57. this.$emit('toggleTodo', todo)
  58. },
  59. doneEdit(e) {
  60. const value = e.target.value.trim()
  61. const { todo } = this
  62. if (!value) {
  63. this.deleteTodo({
  64. todo
  65. })
  66. } else if (this.editing) {
  67. this.editTodo({
  68. todo,
  69. value
  70. })
  71. this.editing = false
  72. }
  73. },
  74. cancelEdit(e) {
  75. e.target.value = this.todo.text
  76. this.editing = false
  77. }
  78. }
  79. }
  80. </script>