1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 16:55:23 +01:00
deka-dom-el/test/index.js

82 lines
2.0 KiB
JavaScript
Raw Normal View History

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
const style= createStyle();
const app= el(todosComponent);
document.head.append(style.element);
document.body.append(app);
function todosComponent({ todos= [] }= {}){
const className= "basicTodoForm";
style.css`
.${className}{
display: flex;
flex-flow: column nowrap;
}
.${className} input{
margin-inline-start: .5em;
}
`;
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);
});
return el("div", { className }).append(
el("div").append(
el("h1", "Todos:"),
el("<>", todos, ts=> !ts.length
? el("p", "No todos yet")
: ts.map((t, i)=> el(todoComponent, { textContent: t, value: i, className }, onremove)))
),
el("form", null, onsubmit).append(
el("h2", "Add:"),
el("label", "New todo: ").append(
el("input", { name, type: "text", required: true }),
),
el("button", "+")
)
2023-08-22 16:30:03 +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: "-" })
);
}
function createStyle(){
const element= el("style");
const store= new WeakSet();
return {
element,
key(k){
if(store.has(k)) return { css: ()=> {} };
store.add(k);
return this;
},
css(...args){
element.appendChild(el("#text", { textContent: String.raw(...args) }));
}
};
}