/* # Todo App (JSX & Store API) This is a basic todo app with browser backed storage using JSX and rvx's store API. */ import { $, For, Show, Signal, watch } from "rvx"; import { reflect, wrap } from "rvx/store"; const STORAGE_KEY = "rvx-examples:todo-app"; export function Example() { const name = $(""); // Load items from storage by creating a // deep reactive wrapper for the items array: let items: Item[]; try { items = wrap(JSON.parse(localStorage.getItem(STORAGE_KEY)!) ?? []); } catch (error) { items = wrap([]); console.error(error); } function add() { if (name.value) { // "items" can be modified directly because it's a deep reactive wrapper: items.push({ name: name.value, done: false }); name.value = ""; } } watch(() => { try { // "JSON.stringify" accesses all reactive properties of "items" // and will cause this function to re-run when anything changes: localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch (error) { console.error(error); } }); return
; } function TextInput(props: { value: Signal; action?: () => void; }) { return { props.value.value = (event.target as HTMLInputElement).value; }} on:keydown={event => { if (event.key === "Enter" && props.action) { event.preventDefault(); props.action(); } }} />; } interface Item { name: string; done: boolean; }