Files
ladder/lib/Rung.ts
2023-01-03 12:04:30 +01:00

35 lines
889 B
TypeScript

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