3.1.4 • Published 5 years ago

react-d3-tree-editor v3.1.4

Weekly downloads
14
License
ISC
Repository
github
Last release
5 years ago

react-d3-tree-editor

React d3 editor is a d3 tree based editor, fully featured with context menu support. You can dynamicly edit, create and delete chiled nodes. nodes are based on icons you provide for styling.

NPM JavaScript Style Guide

Contents

Demo

Install

npm install --save react-d3-tree-editor

Basic Usage

import React, { Component } from 'react'
import TreeEditor from 'react-d3-tree-editor'

export default class App extends Component {

  constructor() {
    
    //the actual tree data
    this.treeData = {
        name: 'Root Node',
        
        children: [{
            name: 'new node'
            
        }, {
            name: 'new node'
        }]
    }

    //the d3 tree config 
    this.treeConfig = {
      margin: {
        top: 20,
        right: 120,
        bottom: 20,
        left: 120
      },
      textMargin: 20,
      duration: 750,
      nodeSize: [40, 70]
    }
  }
  render() {
    return (
      return (
        <TreeEditor treeData={this.treeData}
            treeConfig={this.treeConfig}/>
        )
    )
  }
}

Fully Featured Usage

  class App extends React.Component {

      constructor(props) {
          super(props);
          
          this.treeRef = null;

          this.treeData = {
              //the actual data
              name: 'Root Node',
            
              children: [{
                  name: 'new node'
                  
              }, {
                  name: 'new node'
              }]
          }
          this.treeConfig = {
              //the d3 tree config 
              margin: {
                  top: 20,
                  right: 120,
                  bottom: 20,
                  left: 120
              },
              textMargin: 20,
              duration: 750,
              nodeSize: [40, 70]
          }
      }

      filterTextName = (d) => {
          //will be fired for each element once to decide which propery be used to display the name 
          return d.name;
      }


      contextMenuOpen = (d) => {
          console.log('contextMenuOpen');
          //will be fired when context menu opended
      }

      contextMenuClose = (d) => {
          console.log('contextMenuClose');
          //will be fired when context menu closed
      }

      selectImageLink = (d) => {
          //will be fired once for each element to decide the icon for that element
          return "http://mojedelo.webfactional.com/img/ikone/mdikona4.png"
      }

      getContextMenu = (e) => {
          //will be fired on each element for attaching a context menu
          return [{
                  title: 'Add Item',
                  action: () => {
                      this.treeRef.addNode(e, {
                          name: 'new node'
                      })
                  },
                  enabled: true
              },
              {
                  title: 'Delete Item',
                  action: (e) => {
                      this.treeRef.removeNode(e);
                  },
                  enabled: true
              },
              {
                  title: 'Delete Children',
                  action: () => {
                      this.treeRef.removeChildren(e);
                  },
                  enabled: true
              },
              {
                  title: 'Expand All',
                  action: () => {
                      this.treeRef.expandAllElements(e);
                      this.treeRef.update(e);
                  },
                  enabled: true
              },
              {
                  title: 'Collapse All',
                  action: () => {
                      this.treeRef.collapseAllElements(e);
                      this.treeRef.update(e);
                  },
                  enabled: true
              },

          ];
      }

      treeRenderedCallback = (d) => {
          console.log('treeRenderedCallback');
          //will be fired when tree finish render
      }
      render() {
          return (
          <TreeEditor treeData={this.treeData}
              treeConfig={this.treeConfig}
              onRef={ref => (this.treeRef=ref)}
              filterTextName={this.filterTextName}
              contextMenuOpen={this.contextMenuOpen}
              contextMenuClose={this.contextMenuClose}
              selectImageLink={this.selectImageLink}
              getContextMenu={this.getContextMenu}
              treeRenderedCallback={this.treeRenderedCallback}/>
          )
      }
  }

Props

Prop nameMandatoryDefaultValueDescription
treeDatatrueN/AThe actual d3.tree data. you can append any data to a node and it will be avalable. Each child node should be a part of node.children array. Nodes contain an 'id' attribute which you can either manage by yourself or let the tree manage it automaticly.
treeConfigtrueN/AThe d3 tree configuration
onReffalseN/Aexecuted when the tree is first generated, the purpose of this callbact is to give the developer the tree referance for lated use
filterTextNamefalsenamewill be fired for each element once to decide which attribute be used to display the name of the element. If not implemented, the tree will assume that the 'name' attribute will be used
contextMenuOpenfalseN/Awill be fired when context menu opended
contextMenuClosefalseN/Awill be fired when context menu closed
selectImageLinkfalseN/Awill be fired once for each element to decide the icon for that element.If not specified a default image will be presented.
getContextMenufalseN/Awill be fired on each element for attaching a context menu, see Context Menu for details
treeRenderedCallbackfalseN/AWill be fired when tree finish render
treeNodeActionfalseN/Awill executed every time tree action occurred

#Context Menu

Context menu is an object array that each object consist of the following properties

  • title: the title of the option that will be displayed
  • enabled: (bool) will it be enabled or not.
  • action: the action that will be executed on the tree node
    [
      {
        title: 'Collapse All',
        action: () => {
            this.treeRef.collapseAllElements(e);
            this.treeRef.update(e);
        },
        enabled: true
      }
    ]

License

MIT © https://github.com/Naihan/react-d3-tree-editor

3.1.4

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.0.6

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.32

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1-security

5 years ago