1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 07:49:38 +01:00
deka-dom-el/examples/components/todosComponent.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-11-24 20:41:04 +01:00
import { style, el, dispatchEvent, on, O, 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;
const todosO= O(new Map(), {
add(v){ this.value.set(key++, O(v)); },
remove(key){ O.clear(this.value.get(key)); this.value.delete(key); }
});
todos.forEach(text=> O.action(todosO, "add", text));
const name= "todoName";
const onsubmitAdd= on("submit", event=> {
const el= event.target.elements[name];
event.preventDefault();
O.action(todosO, "add", el.value);
el.value= "";
});
const onremove= on("remove", event=>
O.action(todosO, "remove", event.detail));
return el("div", { className }).append(
el("div").append(
el("h2", "Todos:"),
el("h3", "List of todos:"),
O.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", O(()=> JSON.stringify(Array.from(todosO()), null, "\t")))
)
)
}
/**
* @dispatchs {number} remove
* */
function todoComponent({ textContent, value }){
const { host }= scope;
const onclick= on("click", event=> {
const value= Number(event.target.value);
event.preventDefault();
event.stopPropagation();
2023-11-21 17:19:59 +01:00
dispatchEvent("remove")(host(), value);
});
2023-11-24 20:41:04 +01:00
const is_editable= O(false);
2023-09-08 20:18:58 +02:00
const onedited= on("change", ev=> {
textContent(ev.target.value);
is_editable(false);
});
return el("li").append(
2023-11-24 20:41:04 +01:00
O.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)
);
}