09: Focus Input

Automatically focus after switching edit mode.

  1. Add focusTodo invocation to controller element by x-invocations directive.
  2. Call focusTodo on double click at todo title.

<body>
  <section class="todoapp"


      editTodo (_, payload, meta) {

      },
      focusTodo (_, payload, meta) {
        const inputs = _.$.querySelectorAll('input.edit')
        const target = inputs[payload.index]
        if (!target) {
          return
        }

        target.focus()
        target.setSelectionRange(
          target.value.length,
          target.value.length
        )
      },
    }"
  ::>

    <section class="main">

      <ul class="todo-list"
        x-each="todo, index in todos"
      >

          <div class="view">

            <label @dblclick="editTodo({index}), focusTodo({index})">{{ todo.title }}</label>
            <button class="destroy"></button>
          </div>

        </li>
      </ul>
    </section>

  </section>
</body>
</html>