React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The tree component allows the user to show tree-structured data. We can use the following approach in ReactJS to use the React Suite Tree Component.
Tree Props:
- childrenKey: It is used to denote the Tree data structure Children property name.
- classPrefix: It is used to denote the prefix of the component CSS class.
- data: It is used to denote the selectable data.
- defaultExpandAll: It is used to expand all nodes by default.
- defaultExpandItemValues: It is used to set the value of the default expanded node.
- defaultValue: It is used to denote the default value.
- disabledItemValues: It is used to disable optional.
- draggable: It is used to set the drag node.
- expandItemValues: It is used to set the value of the expanded node (controlled).
- height: It is used to denote the menu height.
- labelKey: It is used to set the options to display the ‘key’ in ‘data’.
- onChange: It is a callback function that is triggered when value changes.
- onDragStart: It is a callback function that is triggered when node drag starts.
- onDragEnter: It is a callback function that is triggered when node drag enters.
- onDragOver: It is a callback function that is triggered when the node drags over.
- onDragLeave: It is a callback function that is triggered when node drag leaves.
- onDragEnd: It is a callback function that is triggered when node drag ends.
- onDrop: It is a callback function that is triggered when the node drops.
- onExpand: It is a callback function that is triggered when the tree node is displayed.
- onSelect: It is a callback function that is triggered on the selection of an option.
- renderDragNode: It is used for the custom Render drag node when draggable is true.
- renderTreeIcon: It is used to denote the custom render icon.
- renderTreeNode: It is used to denote the custom render tree node.
- searchKeyword: It is used to denote the search keyword (Controlled).
- value: It is used to denote the value (Controlled).
- valueKey: It is used to set the option value ‘key’ in ‘data’.
- virtualized: It is used to indicate whether using Virtualized List or not.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install rsuite
Project Structure: It will look like the following.
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React from 'react' import 'rsuite/dist/styles/rsuite-default.css' ; import { Tree } from 'rsuite' ; export default function App() { // Sample Options const options = [ { "label" : "Madhya Pradesh" , "value" : 1, "children" : [ { "label" : "Mhow" , "value" : 2 }, { "label" : "Indore" , "value" : 3, "children" : [ { "label" : "Vijay Nagar" , "value" : 4 }, { "label" : "Rajiv Gandhi Square" , "value" : 5 }, { "label" : "MR 10" , "value" : 6 }, ] }, ] } ] return ( <div style={{ display: 'block' , width: 600, paddingLeft: 30 }}> <h4>React Suite Tree Component</h4> <Tree style={{ width: 300 }} defaultExpandAll data={options} /> </div> ); } |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://rsuitejs.com/components/tree/