Open In App

React.js Blueprint Tree Component Props

BlueprintJS is an open-source UI toolkit for React applications. It is used for making complex and data-heavy web applications. In this article, we will be discussing the Props of the Tree Component of the BluePrintJS library.

The Tree Component is used to display the hierarchical data to the user such as the employee list in an organization or the folder structure in a storage device. A tree contains various children known as TreeNodes.



React.js BluePrint Tree Component Props:

React.js BluePrint TreeNode Component Props:



Syntax:

<Tree
    contents={...}
    onNodeMouseEnter={...}
    onNodeMouseLeave={...}
/>

Creating React Application And Installing Modules:

Step 1: Create a React application using the following command:

npx create-react-app myApp

Step 2: After creating your project folder i.e. myApp, move to it using the following command:

cd myApp

Step 3: After creating the ReactJS application, Install the required modules using the following command:

npm install @blueprintjs/core @blueprintjs/icons

Project Structure: After following the above steps, the project structure will look as below:

Project Structure

Example 1: Now write the following code in the App.js file. This example shows how to make a simple tree structure.




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/icons/lib/css/blueprint-icons.css'
import { Tree } from '@blueprintjs/core';
  
function App() {
  
    // Tree Data
    const treeData = [
        {
            id: 0,
            hasCaret: true,
            icon: "folder-close",
            label: "Folders",
        },
        {
            id: 1,
            hasCaret: true,
            icon: "cog",
            label: "Settings"
        },
        {
            id: 2,
            hasCaret: false,
            icon: "log-out",
            label: "Log Out"
        },
    ];
  
    return (
        <div style={{
            display: 'block',
            width: 400,
            padding: 25
        }}>
            <h2 style={{ color: "green" }}>
                GeeksforGeeks
            </h2>
            <h4>
                ReactJS Blueprint
                Tree Component Props
            </h4>
            <Tree
                contents={treeData}
            />
        </div>
    );
}
  
export default App;

Step to run the application: Execute the below command from the root directory of the project to run the app.

npm start

Output:

 

Example 2: This example shows the use of various tree component properties to expand the tree when we hover over it.




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/icons/lib/css/blueprint-icons.css'
import { Tree } from '@blueprintjs/core';
  
function App() {
  
    const [isFolderOpen, setFolderOpen] = React.useState(false);
    // Tree Data
    const treeData = [
        {
            id: 1,
            hasCaret: true,
            isExpanded: isFolderOpen,
            icon: "folder-close",
            label: "Folders",
            childNodes: [
                {
                    id: 11,
                    icon: "folder-open",
                    label: "Documents"
                },
                {
                    id: 12,
                    icon: "folder-open",
                    label: "Videos"
                },
                {
                    id: 13,
                    icon: "folder-open",
                    label: "Pictures"
                },
                {
                    id: 14,
                    icon: "folder-open",
                    label: "Music"
                }
            ]
        }
    ];
  
    return (
        <div style={{
            display: 'block',
            width: 400,
            padding: 25
        }}>
            <h2 style={{ color: "green" }}>
                GeeksforGeeks
            </h2>
            <h4>
                ReactJS Blueprint
                Tree Component Props
            </h4>
            <Tree
                contents={treeData}
                onNodeMouseEnter={() => setFolderOpen(true)}
                onNodeMouseLeave={() => setFolderOpen(false)}
            />
        </div>
    );
}
  
export default App;

Output:

 

Reference: https://blueprintjs.com/docs/#core/components/tree.props


Article Tags :