A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.npmjs.com/package/react-vtree below:

react-vtree - npm

react-vtree

This package provides a lightweight and flexible solution for rendering large tree structures. It is built on top of the react-window library.

Attention! This library is entirely rewritten to work with the react-window. If you are looking for the tree view solution for the react-virtualized, take a look at react-virtualized-tree.

Installation

npm i react-window react-vtree

 

yarn add react-window react-vtree

Usage FixedSizeTree Example

You can also take a look at the very similar example at the Storybook:

import {FixedSizeTree as Tree} from 'react-vtree';

 

const tree = {

  name: 'Root #1',

  id: 'root-1',

  children: [

    {

      children: [

        {id: 'child-2', name: 'Child #2'},

        {id: 'child-3', name: 'Child #3'},

      ],

      id: 'child-1',

      name: 'Child #1',

    },

    {

      children: [{id: 'child-5', name: 'Child #5'}],

      id: 'child-4',

      name: 'Child #4',

    },

  ],

};

 

function* treeWalker(refresh) {

  const stack = [];

 

  

  stack.push({

    nestingLevel: 0,

    node: tree,

  });

 

  

  while (stack.length !== 0) {

    const {

      node: {children = [], id, name},

      nestingLevel,

    } = stack.pop();

 

    

    

    

    

    

    const isOpened = yield refresh

      ? {

          id,

          isLeaf: children.length === 0,

          isOpenByDefault: true,

          name,

          nestingLevel,

        }

      : id;

 

    

    

    if (children.length !== 0 && isOpened) {

      

      

      for (let i = children.length - 1; i >= 0; i--) {

        stack.push({

          nestingLevel: nestingLevel + 1,

          node: children[i],

        });

      }

    }

  }

}

 

const Node = ({data: {isLeaf, name}, isOpen, style, toggle}) => (

  <div style={style}>

    {!isLeaf && (

      <button type="button" onClick={toggle}>

        {isOpen ? '-' : '+'}

      </button>

    )}

    <div>{name}</div>

  </div>

);

 

const Example = () => (

  <Tree treeWalker={treeWalker} itemSize={30} height={150} width={300}>

    {Node}

  </Tree>

);

Props

The component receives all the props of the FixedSizeList component except for the itemCount. Additional properties are the following:

children

The Node component that is responsible for rendering each node. It receives all the properties Row recieves except for the index prop plus the following properties:

rowComponent: component

This property receives a custom Row component for the FixedSizeList that will override the default one. It can be used for adding new functionality to an existing one by wrapping the default Row into a custom component.

* treeWalker(refresh: boolean)

An iterator function that walks around the tree and yields each node one by one flattening them to an array that can be easily displayed by FixedSizeList component.

The function receives refresh parameter. If it is true, the component requests the full node update and expects the complete data object yielded. If it is false, the component awaits only the node id to update the order of displayed nodes.

The data object should contain the following required properties:

You can add any other property you need. This object will be sent directly to the Node component.

Yielding the object gets the current openness state of the node. Basing on it, you should decide if the node's children are going to be rendered.

Methods

The component provides all the methods FixedSizeList provides with the following changes:

scrollToItem(id: string | symbol, align?: Align): void

The scrollToItem method receives node id instead of index.

async recomputeTree(options): void

This method runs the treeWalker function again and, basing on the received options, updates either nodes or their order.

It receives options object with the following parameters:

Types VariableSizeTree Example

You can also take a look at the very similar example at the Storybook:

import {VariableSizeTree as Tree} from 'react-vtree';

 

const tree = {

  name: 'Root #1',

  id: 'root-1',

  children: [

    {

      children: [

        {id: 'child-2', name: 'Child #2'},

        {id: 'child-3', name: 'Child #3'},

      ],

      id: 'child-1',

      name: 'Child #1',

    },

    {

      children: [{id: 'child-5', name: 'Child #5'}],

      id: 'child-4',

      name: 'Child #4',

    },

  ],

};

 

function* treeWalker(refresh) {

  const stack = [];

 

  stack.push({

    nestingLevel: 0,

    node: tree,

  });

 

  while (stack.length !== 0) {

    const {

      node: {children = [], id, name},

      nestingLevel,

    } = stack.pop();

 

    const isOpened = yield refresh

      ? {

          

          

          

          defaultHeight: 30,

          id,

          isLeaf: children.length === 0,

          isOpenByDefault: true,

          name,

          nestingLevel,

        }

      : id;

 

    if (children.length !== 0 && isOpened) {

      for (let i = children.length - 1; i >= 0; i--) {

        stack.push({

          nestingLevel: nestingLevel + 1,

          node: children[i],

        });

      }

    }

  }

}

 

const Node = ({data: {isLeaf, name}, height, isOpen, style, toggle}) => (

  <div style={style}>

    {!isLeaf && (

      <button type="button" onClick={toggle}>

        {isOpen ? '-' : '+'}

      </button>

    )}

    <div>{name}</div>

  </div>

);

 

const Example = () => (

  <Tree treeWalker={treeWalker} height={150} width={300}>

    {Node}

  </Tree>

);

Props

The component receives all the props of the VariableSizeList component except for the itemCount and itemSize. itemSize is still available but not required, and should be used only if the default behavior is not enough. Additional properties are the following:

children

The Node component. It is the same as the FixedSizeTree's one but receives two additional properties:

rowComponent: component

This property receives a custom Row component for the VariableSizeList that will override the default one. It can be used for adding new functionality to an existing one by wrapping the default Row into a custom component.

* treeWalker(refresh: boolean)

An iterator function that walks over the tree. It behaves the same as FixedSizeTree's treeWalker, but there one additional required property for the data object:

Methods

The component provides all the methods VariableSizeList provides with the following changes:

scrollToItem(id: string | symbol, align?: Align): void

The scrollToItem method receives node id instead of index.

resetAfterId(id: string | symbol, shouldForceUpdate: boolean = false): void

This method replaces the resetAfterIndex method of VariableSizeList, but works exactly the same. It receives node id as a first argument.

async recomputeTree(options): void

This method works exactly the same as the FixedSizeTree's one, but receives one additional option:

Types

All types in this section are the extended variants of FixedSizeTree types.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4