react-organizational-chart

repository·master·Indexed 41 days ago

https://github.com/daniel-hauser/react-organizational-chart

A simple React library for rendering hierarchy trees that allows any React component to be used as a node. Version 2.2.1 provides the Tree and TreeNode components to build flexible organizational charts with customizable connecting lines, including props for lineWidth, lineColor, and lineBorderRadius.

Tokens
1.9K
Snippets
8
Records
10
Agent score
46%

What's inside react-organizational-chart

  1. Basic usage of react-organizational-chart

    master

    To create a simple hierarchy, import Tree and TreeNode. The Tree component acts as the root, and TreeNode components are nested within it to represent children and descendants. The label prop for both components accepts any React Node (e.g., a div, span, or custom component).

    import React from 'react';
    import { Tree, TreeNode } from 'react-organizational-chart';
    
    const ExampleTree = () => (
      <Tree label={<div>Root</div>}>
        <TreeNode label={<div>Child 1</div>}>
          <TreeNode label={<div>Grand Child</div>} />
        </TreeNode>
      </Tree>
    );
  2. How to style the tree lines and nodes

    master

    You can customize the appearance of the connecting lines and the spacing of nodes using props on the Tree component. This allows you to control line width, color, style, and border radius, as well as the padding around nodes.

    const StyledNode = styled.div`
      padding: 5px;
      border-radius: 8px;
      display: inline-block;
      border: 1px solid red;
    `;
    
    const StyledTreeExample = () => (
      <Tree
        lineWidth={'2px'}
        lineColor={'green'}
        lineBorderRadius={'10px'}
        label={<StyledNode>Root</StyledNode>}
      >
        <TreeNode label={<StyledNode>Child 1</StyledNode>}>
          <TreeNode label={<StyledNode>Grand Child</StyledNode>} />
        </TreeNode>
        {/* ... other nodes ... */}
      </Tree>
    );
  3. Tree component API reference

    master

    The Tree component is the root of the hierarchy. It accepts the following props:

    PropRequiredTypeDefaultDescription
    labelYesReact Node-The content to display for the root node
    childrenYesReactNode-Any number of <TreeNode> components
    lineHeightNoCSS length20pxThe height of the Path
    lineWidthNoCSS length1pxThe width of the Path
    lineColorNoCSS colorblackThe color of the Path
    lineStyleNoCSS border-stylesolidThe line style
    lineBorderRadiusNoCSS border-radius5pxThe border radius of the Path
    nodePaddingNoCSS length5pxThe left and right padding of every <TreeNode>
  4. Use the Tree component as the root container

    master

    The Tree component serves as the root of your organizational chart hierarchy. It initializes the tree structure and provides global styling configuration for the connecting lines and node spacing via CSS variables.

    To use it, you must provide a label for the root node and its children (which should be TreeNode components).

    import { Tree } from 'react-organizational-chart';
    import { TreeNode } from 'react-organizational-chart';
    
    function MyChart() {
      return (
        <Tree label="Root Node">
          <TreeNode label="Child 1" />
          <TreeNode label="Child 2" />
        </Tree>
      );
    }
  5. Configure Tree styling via TreeProps

    master

    You can customize the appearance of the connecting lines and the spacing between nodes using the following props on the Tree component:

    PropTypeDefaultDescription
    labelTreeNodeProps['label']RequiredThe label for the root node.
    childrenTreeNodeProps['children']RequiredThe child nodes of the tree.
    lineHeightstring'20px'The height of the connecting lines.
    lineWidthstring'1px'The width of the connecting lines.
    lineColorstring'black'The color of the connecting lines.
    lineStyle'dashed' | 'dotted' | 'double' | 'solid' | string'solid'The style of the connecting lines.
    lineBorderRadiusstring'5px'The border radius applied to the line ends.
    nodePaddingstring'5px'The padding between sibling nodes.
    export interface TreeProps {
      label: TreeNodeProps['label'];
      lineHeight?: string;
      lineWidth?: string;
      lineColor?: string;
      lineStyle?: 'dashed' | 'dotted' | 'double' | 'solid' | string;
      lineBorderRadius?: string;
      nodePadding?: string;
      children: TreeNodeProps['children'];
    }
  6. Use the TreeNode component

    master

    The TreeNode component represents an individual node within the organizational chart hierarchy. It renders a label and, if provided, a list of children nodes. The component uses CSS variables (e.g., --tree-line-color, --tree-line-width) to control the appearance of the connecting lines, which are typically configured at the Tree root level.

    Props

    PropTypeDescription
    labelReact.ReactNodeRequired. The content to display for the node (text, images, or other React elements).
    childrenReactNodeOptional. The sub-nodes to be rendered as children of this node.
    classNamestringOptional. Additional CSS classes to apply to the node element.
    <TreeNode label={<div>My Node</div>}>
      <TreeNode label={<div>Child 1</div>} />
      <TreeNode label={<div>Child 2</div>} />
    </TreeNode>