1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-23 17:19:37 +01:00
deka-dom-el/examples/components/todosComponent.js

94 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

import { style, el, dispatchEvent, on, S, scope } from '../exports.js';
const className= style.host(todosComponent).css`
:host{
display: flex;
flex-flow: column nowrap;
}
:host input{
margin-inline-start: .5em;
}
:host button{
margin-inline-start: 1em;
}
2023-09-08 20:18:58 +02:00
:host output{
white-space: pre;
}
`;
/** @param {{ todos: string[] }} */
2023-09-08 20:18:58 +02:00
export function todosComponent({ todos= [ "Task A" ] }= {}){
let key= 0;
2024-11-22 16:26:42 +01:00
const todosO= S( /** @type {Map<number, ddeSignal<string>>} */ (new Map()), {
/** @param {string} v */
add(v){ this.value.set(key++, S(v)); },
2024-11-22 16:26:42 +01:00
/** @param {number} key */
remove(key){ S.clear(this.value.get(key)); this.value.delete(key); }
});
todos.forEach(text=> S.action(todosO, "add", text));
const name= "todoName";
const onsubmitAdd= on("submit", event=> {
2024-11-22 16:26:42 +01:00
const el_form= /** @type {HTMLFormElement} */ (event.target);
const el= /** @type {HTMLInputElement} */ (el_form.elements[name]);
event.preventDefault();
S.action(todosO, "add", el.value);
el.value= "";
});
2024-11-22 16:26:42 +01:00
const onremove= on("remove", /** @param {CustomEvent<number>} event */
event=> S.action(todosO, "remove", event.detail));
return el("div", { className }).append(
el("div").append(
el("h2", "Todos:"),
el("h3", "List of todos:"),
S.el(todosO, (ts, memo)=> !ts.size
? el("p", "No todos yet")
: el("ul").append(
...Array.from(ts).map(([ value, textContent ])=>
memo(value, ()=> el(todoComponent, { textContent, value, className }, onremove)))
)
),
2023-09-08 20:18:58 +02:00
el("p", "Click to the text to edit it.")
),
el("form", null, onsubmitAdd).append(
el("h3", "Add a todo:"),
el("label", "New todo: ").append(
el("input", { name, type: "text", required: true }),
),
el("button", "+")
2023-09-08 20:18:58 +02:00
),
el("div").append(
el("h3", "Output (JSON):"),
el("output", S(()=> JSON.stringify(Array.from(todosO()), null, "\t")))
)
)
}
/**
2024-11-22 16:26:42 +01:00
* @param {{ textContent: ddeSignal<string>, value: ddeSignal<number> }} attrs
* @dispatchs {number} remove
* */
function todoComponent({ textContent, value }){
const { host }= scope;
2024-11-22 16:26:42 +01:00
const dispatchRemove= /** @type {(data: number) => void} */
(dispatchEvent("remove", null, host));
const onclick= on("click", event=> {
2024-11-22 16:26:42 +01:00
const el= /** @type {HTMLButtonElement} */ (event.target);
const value= Number(el.value);
event.preventDefault();
event.stopPropagation();
2024-11-22 16:26:42 +01:00
dispatchRemove(value);
});
const is_editable= S(false);
2023-09-08 20:18:58 +02:00
const onedited= on("change", ev=> {
2024-11-22 16:26:42 +01:00
const el= /** @type {HTMLInputElement} */ (ev.target);
textContent(el.value);
2023-09-08 20:18:58 +02:00
is_editable(false);
});
return el("li").append(
S.el(is_editable, is=> is
2023-09-08 20:18:58 +02:00
? el("input", { value: textContent(), type: "text" }, onedited)
: el("span", { textContent, onclick: is_editable.bind(null, true) })
2023-09-08 20:18:58 +02:00
),
el("button", { type: "button", value, textContent: "-" }, onclick)
);
}