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