rsuite
Version:
A suite of react components
34 lines • 907 B
JavaScript
'use client';
/**
* Partial implementation of Node API
* Used for holding tree nodes hierarchy
* Ref: https://developer.mozilla.org/zh-CN/docs/Web/API/Node
*/
export class Node {
id = null;
nodeValue = null;
parent = null;
parentNode = null;
childNodes = [];
appendChild(newChild) {
newChild.parentNode = this;
this.childNodes.push(newChild);
}
hasChildNodes() {
return this.childNodes.length > 0;
}
get firstChild() {
return this.childNodes[0] ?? null;
}
get lastChild() {
return this.childNodes[this.childNodes.length - 1] ?? null;
}
get nextSibling() {
if (!this.parentNode) return null;
return this.parentNode.childNodes[this.parentNode.childNodes.indexOf(this) + 1] ?? null;
}
get previousSibling() {
if (!this.parentNode) return null;
return this.parentNode.childNodes[this.parentNode.childNodes.indexOf(this) - 1] ?? null;
}
}