import { observer } from "mobx-react"; import { CollectionViewBase } from "./CollectionViewBase"; import { Document } from "../../../fields/Document"; import { KeyStore } from "../../../fields/KeyStore"; import { ListField } from "../../../fields/ListField"; import React = require("react") import { TextField } from "../../../fields/TextField"; import { observable, action } from "mobx"; import "./CollectionTreeView.scss"; export interface TreeViewProps { document: Document; } @observer /** * Component that takes in a document prop and a boolean whether it's collapsed or not. */ class TreeView extends React.Component { @observable collapsed: boolean = false; /** * Renders a single child document. If this child is a collection, it will call renderTreeView again. Otherwise, it will just append a list element. * @param document The document to render. */ renderChild(document: Document) { var children = document.GetT>(KeyStore.Data, ListField); let title = document.GetT(KeyStore.Title, TextField); // if the title hasn't loaded, immediately return the div if (!title || title === "") { return
; } // otherwise, check if it's a collection. else if (children && children !== "") { // if it's not collapsed, then render the full TreeView. if (!this.collapsed) { return (
  • this.collapsed = true)} > {title.Data}
  • ); } else { return
  • this.collapsed = false)}>{title.Data}
  • } } // finally, if it's a normal document, then render it as such. else { return
  • {title.Data}
  • ; } } render() { var children = this.props.document.GetT>(KeyStore.Data, ListField); if (children && children !== "") { return (
    {children.Data.map(value => this.renderChild(value))}
    ) // let results: JSX.Element[] = []; // // append a list item for each child in the collection // children.Data.forEach((value) => { // results.push(this.renderChild(value)); // }) // return results; } else { return
    ; } } } @observer export class CollectionTreeView extends CollectionViewBase { render() { let titleStr = ""; let title = this.props.Document.GetT(KeyStore.Title, TextField); if (title && title !== "") { titleStr = title.Data; } return (

    {titleStr}

    ); } }