feat: added jsx support and some tests

This commit is contained in:
Daniel Ledda
2022-05-26 22:00:23 +02:00
parent 182c38232e
commit e5e0c47f68
10 changed files with 173 additions and 75 deletions

View File

@@ -3,36 +3,36 @@ import {SubNode} from "./helpers";
export type RungOptions = {};
export default abstract class Rung {
protected el: HTMLElement | null = null;
protected node: Node | null = null;
protected constructor(options: RungOptions) {}
render(): HTMLElement {
if (!this.el) {
this.el = this.build();
render(): Node {
if (!this.node) {
this.node = this.build();
}
return this.el;
return this.node;
}
protected getEl(): HTMLElement {
protected getEl(): Node {
return this.render();
}
redraw(): void {
const oldNode = this.el;
if (!oldNode || !this.el) {
const oldNode = this.node;
if (!oldNode || !this.node) {
return;
}
const parent = this.el.parentElement;
const parent = this.node.parentElement;
if (parent) {
this.el = this.build();
parent.replaceChild(this.el, oldNode);
this.node = this.build();
parent.replaceChild(this.node, oldNode);
} else {
this.render();
}
}
protected abstract build(): HTMLElement;
protected abstract build(): Node;
}
export type FunctionalRung<Props extends Record<string, any>, N extends Node> = (attributes: Props, subNodes: SubNode[]) => N;
export type FunctionalRung<Props extends Record<string, any>, N extends HTMLElement> = (attributes: Props, subNodes?: SubNode[]) => N;