2023-08-23 15:37:32 +02:00
|
|
|
import { el, elNS, assign, listen, dispatch } from "../index.js";
|
|
|
|
Object.assign(globalThis, { el, elNS, assign, listen, dispatch });
|
2023-08-22 16:30:03 +02:00
|
|
|
|
2023-08-23 15:37:32 +02:00
|
|
|
const { style, css }= createStyle();
|
|
|
|
globalThis.test= console.log;
|
|
|
|
const app= el(component, null, listen("change", globalThis.test));
|
|
|
|
dispatch("change", "Peter")(app);
|
|
|
|
console.log(app, app instanceof HTMLDivElement);
|
2023-08-22 16:30:03 +02:00
|
|
|
|
2023-08-23 15:37:32 +02:00
|
|
|
document.head.append(style);
|
|
|
|
document.body.append(app);
|
|
|
|
|
|
|
|
function component({ value= "World" }= {}){
|
|
|
|
const name= "naiveForm";
|
|
|
|
css`
|
|
|
|
.${name}{
|
|
|
|
display: flex;
|
|
|
|
flex-flow: column nowrap;
|
|
|
|
}
|
|
|
|
.${name} input{
|
|
|
|
margin-inline-start: .5em;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2023-08-23 20:15:30 +02:00
|
|
|
const output= eventsSink(store=> ({
|
|
|
|
onchange: listen("change", function(event){
|
|
|
|
assign(store.element, { textContent: event.target.value });
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
const input= eventsSink(store=> ({
|
|
|
|
onchange: listen("change", function(event){
|
|
|
|
assign(store.element, { value: event.detail });
|
|
|
|
dispatch("change")(input.element);
|
|
|
|
})
|
|
|
|
}));
|
2023-08-23 15:37:32 +02:00
|
|
|
return el("div", { className: name }, input.onchange).append(
|
|
|
|
el("p").append(
|
|
|
|
el("#text", { textContent: "Hello " }),
|
2023-08-23 20:15:30 +02:00
|
|
|
el("strong", { textContent: value }, output.target),
|
2023-08-23 15:37:32 +02:00
|
|
|
),
|
|
|
|
el("label").append(
|
|
|
|
el("#text", { textContent: "Set name:" }),
|
2023-08-23 20:15:30 +02:00
|
|
|
el("input", { type: "text", value }, output.onchange, input.target)
|
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-23 20:15:30 +02:00
|
|
|
function eventsSink(fn){
|
|
|
|
const store= {
|
|
|
|
element: null,
|
|
|
|
target: function(element){ store.element= element; },
|
|
|
|
};
|
|
|
|
Object.assign(store, fn(store));
|
|
|
|
return store;
|
|
|
|
}
|
2023-08-23 15:37:32 +02:00
|
|
|
function createStyle(){
|
|
|
|
const style= el("style");
|
|
|
|
return {
|
|
|
|
style,
|
|
|
|
css(...args){
|
|
|
|
style.appendChild(el("#text", { textContent: String.raw(...args) }));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|