Open In App

React.js Blueprint Select2 Custom Menu

Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications.

In this article, we’ll discuss the React.js Blueprint Select2 Custom menu. Select2 component displays a list of items to choose one item and item children are wrapped in a MenuItem that contains the list and an optional InputGroup to filter the items. Select2 provides users to render a custom menu by changing the menu layout or items.



React.js BluePrint Select2 Props:

Syntax:



<Select2
    itemListRenderer={renderCustomMenu}
>
    <Button disabled={true}>
</Select2>

Creating React Application And Installing Module:

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

npm create-react-app appname

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

cd appname

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

npm install @blueprintjs/core

Step 4: Installing @blueprintjs Select component.

npm install @blueprintjs/select

Project Structure:

 

Steps to run the application: Run the project as follows:

npm start

Example 1: The below example demonstrates the usage of the custom menu with the help of itemListRenderer prop in the select2 component.




import React, { useState } from "react";
import { Button, MenuItem } from "@blueprintjs/core";
import { Select2 } from "@blueprintjs/select";
import "@blueprintjs/core/lib/css/blueprint.css";
import "@blueprintjs/select/lib/css/blueprint-select.css";
  
const data = ["Java", "Python", "C++", "SQL", "JavaScript"];
  
function App() {
    const [item, setItem] = useState("");
  
    return (
        <center>
            <div style={{ textAlign: "center", color: "green" }}>
                <h1>GeeksforGeeks</h1>
                <h2>ReactJs Blueprint Select2 Custom Menu</h2>
            </div>
            <Select2
                items={data}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            onClick={(elm) => {
                                setItem(elm.target.textContent);
                            }}
                            active={itemProps.modifiers.active}
                            style={{
                                backgroundColor: "#27EA50",
                                fontWeight: "bold",
                            }}
                        />
                    );
                }}
            >
                <Button
                    text={item}
                    rightIcon="caret-down"
                    style={{ backgroundColor: "#15D13C"
                        color: "white" }}
                />
            </Select2>
        </center>
    );
}
  
export default App;

Output:

 

Example 2: The below example demonstrates the usage of the disabled custom menu in the select2 component.




import React, { useState } from "react";
import { Button, MenuItem } from "@blueprintjs/core";
import { Select2 } from "@blueprintjs/select";
import "@blueprintjs/core/lib/css/blueprint.css";
import "@blueprintjs/select/lib/css/blueprint-select.css";
  
const data = ["Java", "Python", "C++", "SQL", "JavaScript"];
  
function App() {
    const [item, setItem] = useState("");
  
    return (
        <center>
            <div style={{ textAlign: "center", color: "green" }}>
                <h1>GeeksforGeeks</h1>
                <h2>ReactJs Blueprint Select2 Custom Menu</h2>
            </div>
            <Select2
                items={data}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            disabled={true}
                            onClick={(elm) => {
                                setItem(elm.target.textContent);
                            }}
                            active={itemProps.modifiers.active}
                            style={{
                                backgroundColor: "#27EA50",
                                fontWeight: "bold",
                            }}
                        />
                    );
                }}
            >
                <Button
                    text={item}
                    rightIcon="caret-down"
                    style={{ backgroundColor: "#15D13C"
                        color: "white" }}
                />
            </Select2>
        </center>
    );
}
  
export default App;

Output:

 

Reference: https://blueprintjs.com/docs/#select/select2.custom-menu


Article Tags :