2023-08-26 17:32:58 +02:00
|
|
|
import { el, on, off } from "../index.js";
|
|
|
|
import { S } from "../src/signals.js";
|
2023-08-26 12:20:01 +02:00
|
|
|
//import { empty, namespace, on, dispatch } from "../index.js";
|
|
|
|
Object.assign(globalThis, { S, el, on, off });
|
2023-08-22 16:30:03 +02:00
|
|
|
|
2023-08-26 09:36:55 +02:00
|
|
|
const style= createStyle();
|
|
|
|
const app= el(todosComponent);
|
|
|
|
document.head.append(style.element);
|
2023-08-23 15:37:32 +02:00
|
|
|
document.body.append(app);
|
|
|
|
|
2023-08-26 09:36:55 +02:00
|
|
|
function todosComponent({ todos= [] }= {}){
|
|
|
|
const className= "basicTodoForm";
|
|
|
|
style.css`
|
2023-08-24 14:15:55 +02:00
|
|
|
.${className}{
|
2023-08-23 15:37:32 +02:00
|
|
|
display: flex;
|
|
|
|
flex-flow: column nowrap;
|
|
|
|
}
|
2023-08-24 14:15:55 +02:00
|
|
|
.${className} input{
|
2023-08-23 15:37:32 +02:00
|
|
|
margin-inline-start: .5em;
|
|
|
|
}
|
|
|
|
`;
|
2023-08-26 09:36:55 +02:00
|
|
|
todos= S.reactive(todos);
|
|
|
|
globalThis.__todos__= todos; //TODO
|
|
|
|
const name= "todoName";
|
|
|
|
const onsubmit= on("submit", event=> {
|
|
|
|
const value= event.target.elements[name].value;
|
|
|
|
if(!value) return;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
todos.push(value)
|
|
|
|
event.target.elements[name].value= "";
|
|
|
|
});
|
|
|
|
const onremove= on("click", event=> {
|
|
|
|
const value= Number(event.target.value);
|
|
|
|
if(Number.isNaN(value)) return;
|
|
|
|
event.preventDefault();
|
|
|
|
todos.splice(value, 1);
|
|
|
|
});
|
2023-08-23 15:37:32 +02:00
|
|
|
|
2023-08-26 09:36:55 +02:00
|
|
|
return el("div", { className }).append(
|
|
|
|
el("div").append(
|
|
|
|
el("h1", "Todos:"),
|
2023-08-26 17:32:58 +02:00
|
|
|
el("<>", todos, ts=> !ts.length
|
2023-08-26 09:36:55 +02:00
|
|
|
? el("p", "No todos yet")
|
|
|
|
: ts.map((t, i)=> el(todoComponent, { textContent: t, value: i, className }, onremove)))
|
2023-08-23 15:37:32 +02:00
|
|
|
),
|
2023-08-26 09:36:55 +02:00
|
|
|
el("form", null, onsubmit).append(
|
|
|
|
el("h2", "Add:"),
|
|
|
|
el("label", "New todo: ").append(
|
|
|
|
el("input", { name, type: "text", required: true }),
|
|
|
|
),
|
|
|
|
el("button", "+")
|
2023-08-23 15:37:32 +02:00
|
|
|
)
|
2023-08-22 16:30:03 +02:00
|
|
|
)
|
2023-08-23 15:37:32 +02:00
|
|
|
}
|
2023-08-26 09:36:55 +02:00
|
|
|
function todoComponent({ textContent, className, value }){
|
|
|
|
style.key(todoComponent).css`
|
|
|
|
.${className} button{
|
|
|
|
margin-inline-start: 1em;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
return el("p").append(
|
|
|
|
el("#text", textContent),
|
|
|
|
el("button", { type: "button", value, textContent: "-" })
|
|
|
|
);
|
|
|
|
}
|
2023-08-23 15:37:32 +02:00
|
|
|
function createStyle(){
|
2023-08-26 09:36:55 +02:00
|
|
|
const element= el("style");
|
|
|
|
const store= new WeakSet();
|
2023-08-23 15:37:32 +02:00
|
|
|
return {
|
2023-08-26 09:36:55 +02:00
|
|
|
element,
|
|
|
|
key(k){
|
|
|
|
if(store.has(k)) return { css: ()=> {} };
|
|
|
|
store.add(k);
|
|
|
|
return this;
|
|
|
|
},
|
2023-08-23 15:37:32 +02:00
|
|
|
css(...args){
|
2023-08-26 09:36:55 +02:00
|
|
|
element.appendChild(el("#text", { textContent: String.raw(...args) }));
|
2023-08-23 15:37:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|