import * as React from "react"; import { observable, computed } from "mobx"; import { observer } from "mobx-react"; import { CheckBox, box } from "../../index"; interface TreeNodeModel { readonly label: string; state: boolean | undefined; readonly children?: TreeNodeModel[]; } /** A LeafNode is a tree node which has no children and maintains its own check state */ class LeafNode implements TreeNodeModel { constructor(public readonly label: string) {} @observable state = false; } /** A ParentNode's check state is just a summary of its childrens' check states */ class ParentNode implements TreeNodeModel { readonly children: TreeNodeModel[]; constructor(public readonly label: string, ...children: TreeNodeModel[]) { this.children = children; } @computed get state() { let count = 0; for (const child of this.children) { switch (child.state) { case undefined: return undefined; case true: count++; break; } } return count === this.children.length ? true : count === 0 ? false : undefined; } set state(newState: boolean | undefined) { for (const child of this.children) { child.state = newState; } } } const TreeNode = observer((props: { node: TreeNodeModel }): JSX.Element => { return (