import Preact from "preact"; import { html } from "htm"; import { Consumer } from "./store.js"; import * as Tree from "./tree.js"; /** @typedef {(e:SubmitEvent|Event)=>void|null} handler */ /** @type {(props:{person:Store.Person })=>preact.VNode} */ const Person = (props) => { const [, dispatch] = Consumer(); /** @type {handler} */ const handleClick = () => { dispatch({ Key: "person-delete", Arg: props.person }); }; return html`
${props.person.Name} ${props.person.Age}
`; }; /** @type {()=>preact.VNode} */ const PersonTable = () => { const [state] = Consumer(); return html`

People

${ state.People.length > 0 ? html`
Delete Name Age
${state.People.map((p) => html`<${Person} person=${p}/>`)}
` : html`

No people created

` }
`; }; /** @type {()=>preact.VNode} */ const PersonForm = () => { const [, dispatch] = Consumer(); const [nameGet, nameSet] = Preact.useState("default name"); const [ageGet, ageSet] = Preact.useState(21); /** @type {handler} */ const handleSubmit = (e) => { e.preventDefault(); dispatch({ Key: "person-create", Arg: { Name: nameGet, Age: ageGet } }); }; /** @type {handler} */ const handleName = (e) => { e.target && nameSet(/** @type {HTMLInputElement}*/ (e.target).value); }; /** @type {handler} */ const handleAge = (e) => { e.target && ageSet(parseInt(/** @type {HTMLInputElement}*/ (e.target).value)); }; return html`

Add Person

`; }; /** @type {()=>preact.VNode} */ export default () => { return html`

Person Records

<${PersonForm}/> <${PersonTable}/> <${Tree.Branch}> <${Tree.Button}> <${Tree.Menu}>

The method addEventListener() works by adding a function, or an object that implements EventListener, to the list of event listeners for the specified event type on the EventTarget on which it's called. If the function or object is already in the list of event listeners for this target, the function or object is not added a second time.

`; };