Open In App

React Suite CheckTreePicker Async

React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application.

In this article, we’ll learn about React suite CheckTreePicker Async. CheckTreePicker is used as multiple selectors for the selection of multiple complex data structures. In async CheckTreePicker, the data node is loaded asynchronously with the help of the getChildren prop.



CheckTreePicker Props:

Syntax:



<CheckTreePicker onOpen={() => {...}} 
    getChildren={(activeNode) => ... }  />

Creating React Application And Installing Module:

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

npm create-react-app projectname

Step 2: After creating your project, move to it using the given command:

cd projectname

Step 3: Now Install the rsuite node package using the given command:

npm install rsuite

Project Structure: Now your project structure should look like the following:

 

Example 1: Below example demonstrate the basic implementation of async CheckTreePicker.




import { useState } from "react";
import { CheckTreePicker } from "rsuite";
import SpinnerIcon from "@rsuite/icons/legacy/Spinner";
import "rsuite/dist/rsuite.min.css";
  
export default function App() {
    const [value, setValue] = useState([]);
    const [data, setData] = useState([]);
  
    const customData = [
        {
            label: "Algorithms",
            value: 1,
            children: [
                {
                    label: "Searching",
                    value: 2,
                    children: [
                        {
                            label: "Binary Search",
                            value: 3,
                        },
                        {
                            label: "Linear Search",
                            value: 4,
                        },
                    ],
                },
                {
                    label: "Sorting",
                    value: 5,
                    children: [
                        {
                            label: "Selection Sort",
                            value: 6,
                        },
                        {
                            label: "Bubble Sort",
                            value: 7,
                        },
                        {
                            label: "Merge Sort",
                            value: 8,
                        },
                    ],
                },
            ],
        },
    ];
  
    return (
        <center>
            <div>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite CheckTreePicker Async</h4>
                <div style={{ marginTop: 20, width: 800 }}>
                    <CheckTreePicker
                        data={customData}
                        value={value}
                        style={{ width: 280 }}
                        onChange={(value) => setValue(value)}
                        onOpen={() => {
                            if (data.length === 0) {
                                setTimeout(() => {
                                    setData([
                                        {
                                            label: "Parent Node",
                                            value: "0",
                                            children: [],
                                        },
                                    ]);
                                }, 2000);
                            }
                        }}
                        renderMenu={(menuTree) => {
                            if (data.length === 0) {
                                return (
                                    <p style={
                        { padding: 6, textAlign: "center" }}>
                                        <SpinnerIcon size="md" spin /> 
                                            Loading...
                                    </p>
  
                                );
                            }
                            return menuTree;
                        }}
                        getChildren={(activeNode) =>
                            new Promise((resolve) => {
                                setTimeout(() => {
                                    resolve([
                                        {
                                            label: "Child Node",
                                            value: `${activeNode.refKey}-0`,
                                            children: [],
                                        },
                                        {
                                            label: "Child Node",
                                            value: `${activeNode.refKey}-1`,
                                            children: [],
                                        },
                                    ]);
                                }, 2000);
                            })
                        }
                    />
                </div>
            </div>
        </center>
    );
}

Output:

 

Example 2: Below example demonstrate the async CheckTreePicker along with disabled options.




import { useState } from "react";
import { CheckTreePicker } from "rsuite";
import SpinnerIcon from "@rsuite/icons/legacy/Spinner";
import "rsuite/dist/rsuite.min.css";
  
export default function App() {
    const [value, setValue] = useState([]);
    const [data, setData] = useState([]);
  
    const customData = [
        {
            label: "Algorithms",
            value: 1,
            children: [
                {
                    label: "Searching",
                    value: 2,
                    children: [
                        {
                            label: "Binary Search",
                            value: 3,
                        },
                        {
                            label: "Linear Search",
                            value: 4,
                        },
                    ],
                },
                {
                    label: "Sorting",
                    value: 5,
                    children: [
                        {
                            label: "Selection Sort",
                            value: 6,
                        },
                        {
                            label: "Bubble Sort",
                            value: 7,
                        },
                        {
                            label: "Merge Sort",
                            value: 8,
                        },
                    ],
                },
            ],
        },
    ];
  
    return (
        <center>
            <div>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite CheckTreePicker Async</h4>
                <div style={{ marginTop: 20, width: 800 }}>
                    <CheckTreePicker
                        data={customData}
                        value={value}
                        style={{ width: 280 }}
                        onChange={(value) => setValue(value)}
                        disabledItemValues={[4, 6, 7]}
                        onOpen={() => {
                            if (data.length === 0) {
                                setTimeout(() => {
                                    setData([
                                        {
                                            label: "Parent Node",
                                            value: "1",
                                            children: [],
                                        },
                                    ]);
                                }, 1000);
                            }
                        }}
                        renderMenu={(menuTree) => {
                            if (data.length === 0) {
                                return (
                                    <p style={
                         { padding: 6, textAlign: "center" }}>
                                        <SpinnerIcon size="md" spin /> 
                                            Loading...
                                    </p>
  
                                );
                            }
                            return menuTree;
                        }}
                        getChildren={(activeNode) =>
                            new Promise((resolve) => {
                                setTimeout(() => {
                                    resolve([
                                        {
                                            label: "Child Node",
                                            value: `${activeNode.refKey}-0`,
                                            children: [],
                                        },
                                        {
                                            label: "Child Node",
                                            value: `${activeNode.refKey}-1`,
                                            children: [],
                                        },
                                    ]);
                                }, 1000);
                            })
                        }
                    />
                </div>
            </div>
        </center>
    );
}

Output:

 

Reference: https://rsuitejs.com/components/check-tree-picker/#async


Article Tags :