feat: added jsx support and some tests
This commit is contained in:
26
lib/Rung.ts
26
lib/Rung.ts
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user