2023-09-05 09:25:47 +02:00
|
|
|
import { style, el, on, S } 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;
|
|
|
|
}
|
2023-09-05 09:25:47 +02:00
|
|
|
`;
|
|
|
|
/** @param {{ todos: string[] }} */
|
2023-09-08 20:18:58 +02:00
|
|
|
export function todosComponent({ todos= [ "Task A" ] }= {}){
|
2023-09-07 17:35:23 +02:00
|
|
|
const todosS= S([], {
|
2023-09-05 09:25:47 +02:00
|
|
|
add(v){ this.value.push(S(v)); },
|
2023-09-07 17:35:23 +02:00
|
|
|
remove(i){ this.value.splice(i, 1); },
|
|
|
|
[S.symbols.onclear](){ S.clear(...this.value); },
|
2023-09-05 09:25:47 +02:00
|
|
|
});
|
2023-09-07 17:35:23 +02:00
|
|
|
todos.forEach(v=> S.action(todosS, "add", v));
|
2023-09-05 09:25:47 +02:00
|
|
|
const name= "todoName";
|
|
|
|
const onsubmitAdd= on("submit", event=> {
|
|
|
|
const value= event.target.elements[name].value;
|
|
|
|
if(!value) return;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
S.action(todosS, "add", value);
|
|
|
|
event.target.elements[name].value= "";
|
|
|
|
});
|
|
|
|
const onremove= on("remove", event=>
|
|
|
|
S.action(todosS, "remove", event.detail));
|
|
|
|
|
|
|
|
const ul_todos= el("ul").append(
|
2023-09-08 20:18:58 +02:00
|
|
|
S.el(todosS, ts=> ts
|
|
|
|
.map((textContent, value)=>
|
|
|
|
el(todoComponent, { textContent, value, className }, onremove))
|
2023-09-05 09:25:47 +02:00
|
|
|
));
|
|
|
|
return el("div", { className }).append(
|
|
|
|
el("div").append(
|
|
|
|
el("h2", "Todos:"),
|
|
|
|
el("h3", "List of todos:"),
|
2023-09-08 20:18:58 +02:00
|
|
|
S.el(todosS, ts=> !ts.length
|
2023-09-05 09:25:47 +02:00
|
|
|
? el("p", "No todos yet")
|
2023-09-08 20:18:58 +02:00
|
|
|
: ul_todos),
|
|
|
|
el("p", "Click to the text to edit it.")
|
2023-09-05 09:25:47 +02:00
|
|
|
),
|
|
|
|
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(todosS, null, "\t")))
|
2023-09-05 09:25:47 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @type {ddeFires<[ "click" ]>}
|
|
|
|
* @param {{
|
2023-09-08 20:18:58 +02:00
|
|
|
* textContent: ddeSignal<string, any>
|
2023-09-05 09:25:47 +02:00
|
|
|
* value: number
|
|
|
|
* }}
|
|
|
|
* */
|
|
|
|
function todoComponent({ textContent, value }){
|
|
|
|
const ref= S();
|
|
|
|
const onclick= on("click", event=> {
|
|
|
|
const value= Number(event.target.value);
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
ref().dispatchEvent(new CustomEvent("remove", { detail: value }));
|
|
|
|
});
|
2023-09-08 20:18:58 +02:00
|
|
|
const is_editable= S(false);
|
|
|
|
const onedited= on("change", ev=> {
|
|
|
|
textContent(ev.target.value);
|
|
|
|
is_editable(false);
|
|
|
|
});
|
2023-09-05 09:25:47 +02:00
|
|
|
return el("li", null, ref).append(
|
2023-09-08 20:18:58 +02:00
|
|
|
S.el(is_editable, is=> is
|
|
|
|
? el("input", { value: textContent(), type: "text" }, onedited)
|
|
|
|
: el("span", textContent, on("click", ()=> is_editable(true))),
|
|
|
|
),
|
2023-09-05 09:25:47 +02:00
|
|
|
el("button", { type: "button", value, textContent: "-" }, onclick)
|
|
|
|
);
|
|
|
|
}
|