import React from "react"; import { action } from "mobx"; import { observer } from "mobx-react"; import classNames from "classnames"; import { capitalize } from "eez-studio-shared/string"; import { Icon } from "eez-studio-ui/icon"; //////////////////////////////////////////////////////////////////////////////// interface ITableProps { className?: string; columns: IColumn[]; showOnlyChildren: boolean; rootNode: ITreeNode; selectNode: (node: ITreeNode) => void; } export interface IColumn { name: string; title: string; } export interface ITreeNode { id: string; // labels [column: string]: any; children: (() => ITreeNode[]) | undefined; selected: boolean; expanded: { get(): boolean; set(value: boolean): void; }; data?: T; className?: string; } //////////////////////////////////////////////////////////////////////////////// const TreeTableRow = observer( class TreeTableRow extends React.Component<{ columns: IColumn[]; showOnlyChildren: boolean; node: ITreeNode; level: number; selectNode: (node: ITreeNode) => void; }> { onTriangleClick = action((event: any) => { event.preventDefault(); event.stopPropagation(); this.props.selectNode(this.props.node); this.props.node.expanded.set(!this.props.node.expanded.get()); }); onClick = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); this.props.selectNode(this.props.node); }; render() { let childrenRows: JSX.Element[] = []; if (this.props.showOnlyChildren || this.props.node.expanded.get()) { let childrenLevel = this.props.showOnlyChildren ? this.props.level : this.props.level + 1; if (this.props.node.children) { this.props.node.children().forEach(child => { childrenRows.push( ); }); } } let row: JSX.Element | undefined; if (!this.props.showOnlyChildren) { let className = classNames("EezStudio_TreeRow", { EezStudio_Selected: this.props.node.selected }); const firstColumn = this.props.columns[0]; let labelText = this.props.node[firstColumn.name]; let labelTitle = this.props.node[firstColumn.name + "Title"]; let label: JSX.Element | undefined; let triangle: JSX.Element | undefined; if (this.props.node.children) { let triangleClassName = classNames( "EezStudio_TreeRowTriangle", { EezStudio_Expanded: this.props.node.expanded.get() } ); triangle = ( ); label = ( {labelText} ); } else { label = ( {labelText} ); } row = ( {triangle} {label} {this.props.columns.slice(1).map(column => ( {column.name + "Component" in this.props.node ? this.props.node[column.name + "Component"] : this.props.node[column.name]} ))} ); } return ( <> {row} {childrenRows} ); } } ); //////////////////////////////////////////////////////////////////////////////// export const TreeTable = observer( class TreeTable extends React.Component { columnClassName(column: IColumn) { let className = "col" + capitalize(column.name); return className; } render() { let className = classNames( "table EezStudio_TreeTable", this.props.className ); return ( {this.props.columns.map(column => ( ))}
{column.title}
); } } );