import _ from "lodash" import React from "react" import CrossComponent from "./CrossComponent" export interface VerticalTreeLayoutComponentProps { headElem?: any /** e.g. 50 */ height?: number line: string children: React.ReactNode } // Makes a vertical tree component with lines between export default class VerticalTreeLayoutComponent extends React.Component { renderChildren() { const len = React.Children.count(this.props.children) const children = [] // Alternate spacer and items, ending with spacer for (let i = 0, end = len * 2 + 1, asc = 0 <= end; asc ? i < end : i > end; asc ? i++ : i--) { const isCenter = i === len // Add spacer at start if (i === 0) { children.push() // Add spacer at end } else if (i === len * 2) { children.push() // Add spacer if odd } else if (i % 2 === 0) { children.push( ) } else { const child = React.Children.toArray(this.props.children)[Math.floor(i / 2)] // It sets {flexShrink: 0} because without it the boxes where overlapping (on iPad at least) // Note: that fix is based on that article: https://philipwalton.com/articles/normalizing-cross-browser-flexbox-bugs/ // (It contains many other interesting fixes for flexBox) children.push(
1 ? this.props.line : undefined} height={this.props.height} /> {child}
) } } return children } render() { return (
{/* Center head */} {this.props.headElem} {React.Children.count(this.props.children) > 0 ? : undefined} {/* Put children */}
{this.renderChildren()}
) } }