|
| 1 | +import ilha, { html, raw } from "ilha"; |
| 2 | + |
| 3 | +const DEFAULT_TODOS = [ |
| 4 | + { text: "Start Ilha Dev Server", completed: true }, |
| 5 | + { text: "Develop my Ilha app", completed: false }, |
| 6 | + { text: "Deploy my Ilha app", completed: false }, |
| 7 | +]; |
| 8 | + |
| 9 | +type Todo = { text: string; completed: boolean }; |
| 10 | + |
| 11 | +const addTodo = (todos: Todo[], text: string): Todo[] => [...todos, { text, completed: false }]; |
| 12 | + |
| 13 | +const toggleTodo = (todos: Todo[], index: number): Todo[] => |
| 14 | + todos.map((todo, i) => (i === index ? { ...todo, completed: !todo.completed } : todo)); |
| 15 | + |
| 16 | +const deleteTodo = (todos: Todo[], index: number): Todo[] => todos.filter((_, i) => i !== index); |
| 17 | + |
| 18 | +const getIndex = (target: Element) => Number.parseInt(target.getAttribute("data-index")!); |
| 19 | + |
| 20 | +const todoItem = (todo: Todo, index: number) => html` |
| 21 | + <div class="item x-stack"> |
| 22 | + <input id="todo-${index}" type="checkbox" data-index="${index}" ${todo.completed ? "checked" : ""} data-todo-checkbox /> |
| 23 | + <label for="todo-${index}" style="${todo.completed ? "flex:1;text-decoration:line-through" : "flex:1;"}">${todo.text}</label> |
| 24 | + <button data-action="delete" data-index="${index}" type="button" data-variant="secondary">Delete</button> |
| 25 | + </div> |
| 26 | +`; |
| 27 | + |
| 28 | +export default ilha |
| 29 | + .state("todos", DEFAULT_TODOS) |
| 30 | + .state("serverResult", "") |
| 31 | + .on("[data-todo-checkbox]@change", ({ state, target }) => { |
| 32 | + state.todos(toggleTodo(state.todos(), getIndex(target))); |
| 33 | + }) |
| 34 | + .on("[data-todo-form]@submit", ({ event, target, state }) => { |
| 35 | + event.preventDefault(); |
| 36 | + const form = target as HTMLFormElement; |
| 37 | + const text = new FormData(form).get("todo")!.toString().trim(); |
| 38 | + if (!text) return; |
| 39 | + state.todos(addTodo(state.todos(), text)); |
| 40 | + form.reset(); |
| 41 | + }) |
| 42 | + .on("[data-action=delete]@click", ({ state, target }) => { |
| 43 | + state.todos(deleteTodo(state.todos(), getIndex(target))); |
| 44 | + }) |
| 45 | + .on("[data-action=fetch_component]@click", async ({ state }) => { |
| 46 | + const req = await fetch("/server-island"); |
| 47 | + const html = await req.text(); |
| 48 | + state.serverResult(html); |
| 49 | + }) |
| 50 | + .render( |
| 51 | + ({ state }) => |
| 52 | + html` |
| 53 | + <div class="y-stack"> |
| 54 | + <div class="card"> |
| 55 | + <header> |
| 56 | + <h2>To Do</h2> |
| 57 | + </header> |
| 58 | + <section class="y-stack"> |
| 59 | + <form data-todo-form> |
| 60 | + <div class="x-stack"> |
| 61 | + <input |
| 62 | + name="todo" |
| 63 | + type="text" |
| 64 | + placeholder="Add a new todo" |
| 65 | + style="flex-shrink: 1;" |
| 66 | + /> |
| 67 | + <button type="submit">Add</button> |
| 68 | + </div> |
| 69 | + </form> |
| 70 | + <div class="y-stack">${state.todos().map(todoItem)}</div> |
| 71 | + </section> |
| 72 | + </div> |
| 73 | + <div class="x-stack"> |
| 74 | + <button data-action="fetch_component" data-variant="secondary">Fetch Server Side Component</button> |
| 75 | + ${raw(state.serverResult())} |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + `, |
| 79 | + ); |
0 commit comments