all repos — h3 @ 25dfab636dbe8070597e23bdc5d2f678aea307db

A tiny, extremely minimalist JavaScript microframework.

example/assets/js/components/addTodoForm.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
import h3 from "../h3.js";
import store from "../store.js";

export default function AddTodoForm() {
  const addTodo = () => {
    const newTodo = document.getElementById("new-todo");
    if (!newTodo.value) {
      store.dispatch("error/set");
      store.dispatch("$update");
      document.getElementById("new-todo").focus();
      return;
    }
    store.dispatch("error/clear");
    store.dispatch("todos/add", {
      key: `todo_${Date.now()}__${newTodo.value}`, // Make todos "unique-enough" to ensure they are processed correctly
      text: newTodo.value,
    });
    newTodo.value = "";
    store.dispatch("$update");
    document.getElementById("new-todo").focus();
  };
  const addTodoOnEnter = (event) => {
    if (event.keyCode == 13) {
      addTodo();
      event.preventDefault();
    }
  };
  return h3("form.add-todo-form", [
    h3("input", {
      id: "new-todo",
      placeholder: "What do you want to do?",
      onkeydown: addTodoOnEnter,
    }),
    h3(
      "span.submit-todo",
      {
        onclick: addTodo,
      },
      "+"
    ),
  ]);
}