2.0.1 • Published 6 years ago

react-directory-view v2.0.1

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

React Directory View

You no longer need to write your recursion yourself. Just import the Tree component and supply treeData and treeMap props.

Build Status

How to use

Import the Tree Component, and specify treeMap and treeData props.

const App = () => (
  <Tree
    treeData={treeData}
    treeMap={treeMapping}
  />
);

The treeMap prop is just an object containing the configuration for your tree. An example below:

const treeMapping = {
  /**
    * {string=}
    * recursiveKey is the key on your object tree which
    * the tree structure recursively maps over. By
    * default it looks for 'children'.
    */
  recursiveKey: 'children',
  /**
    * {string}
    * The React key given to each JSX node for dom comparison.
    */
  nodeKey: 'id',
  /**
    * {string=}
    * If a component isn't given to treeMapping, then
    * the 'childToRender' is rendered for each node in the tree.
    * This relates to the treeData you're passing to the Tree component.
    */
  childToRender: 'path',
};

A basic example could like below:

import React, { Fragment } from 'react';
import Tree from './components/Tree';
import ExampleComponent from './components/ExampleComponent';

const treeMapping = {
  recursiveKey: 'children', // recursiveKey is set to 'children' by default
  nodeKey: 'id',
  childToRender: 'path',
};

const treeData = [
  {
    path: 'this',
    id: 1,
    children: [
      {
        path: 'is',
        id: 2,
        children: [
          {
            path: 'a',
            id: 3,
            children: [
              {
                path: 'few',
                id: 4,
                children: [
                  {
                    path: 'deep',
                    id: 5,
                    children: [],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
];

const App = () => (
  <Fragment>
    <Tree
      treeData={treeData}
      treeMap={treeMapping}
    />
  </Fragment>
);

This will render a tree that looks like:

> This
  > Is
    > A
      > Few
        Deep

Props

PropDefaultrequiredDescription
treeDataEmpty arrayYesThe data to recursively map over
treeMapEmpty objectYesDescribes the nodes / keys to use when mapping over each child array
useCheckboxFalseNoWether to use checkboxes or not
checkboxStylesEmpty objectNoStyle object used to add or override checkbox styles
arrowStylesempty objectNoStyle object used to add or override arrow styles
ComponentNoneNoA component to render for each node in the object tree. Receives all props from the treeNode.
paddingLeft15pxNoThe amount of padding to add for each child
onContractnoOpNoCallback to be invoked every time a child node is collapsed
onExpandnoOpNoCallback to be invoked every time a child node is expanded
2.0.1

6 years ago

2.0.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago