Open In App

React.js Blueprint Select2 Creating new items

React.js Blueprint is a front-end UI toolkit. It is very optimized and popular for building interfaces that are complex data-dense for desktop applications.

The React.js Blueprint Select2 component provides a list of items to the user with a search box to search for items in the list or to add new items to the list.



The createNewItemFromQuery prop converts the user query to a form that is understandable by the Select2 component, and the createNewItemRenderer renders the item that will be eventually shown in the list. 

Select2 props:



Syntax:

<Select2
   createNewItemFromQuery={}
   createNewItemRenderer={}
/>

Prerequisite:

Creating React Application and Module installation:

Step 1: Create the react project folder, for that open the terminal, and write the command npm create-react-app folder name, if you have already installed create-react-app globally. If you haven’t, install create-react-app globally using the command npm -g create-react-app or install locally by npm i create-react-app.

npm create-react-app project

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

cd project

Step 3: Now install the dependency by using the following command:

npm install @blueprintjs/core
npm install @blueprintjs/select

Project Structure: It will look like this.

 

Example 1: We are importing MenuItem from “@blueprintjs/core” and Select2 from “@blueprintjs/select”. To apply the default styles of the components we are importing “@blueprintjs/core/lib/css/blueprint.css” and “@blueprintjs/select/lib/css/blueprint-select.css”. We are adding the Select2 Component, adding the MenuItem component that renders the items list we passed and we have passed a custom function addText to the createNewItemFromQuery prop which returns a string and the createNewItemRenderer prop returns the value.

App.js




import React from "react";
import "@blueprintjs/core/lib/css/blueprint.css";
import { MenuItem } from "@blueprintjs/core";
import { Select2 } from "@blueprintjs/select";
import "@blueprintjs/select/lib/css/blueprint-select.css";
 
function App() {
    const addText = (s) => {
        return s;
    }
    return (
        <div style={{ margin: 30 }}>
            <h3>ReactJs Blueprint Select2 Creating new items</h3>
            <Select2
                createNewItemFromQuery={addText}
                createNewItemRenderer={(val) => {
                    return val;
                }}
                items={[
                    "Blockchain",
                    "Internet of things"
                ]}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                        />
                    );
                }}
                onItemSelect={() => { }}
            >
                <button> Click Me !</button>
            </Select2>
        </div>
    );
}
 
export default App;

Step to Run Application: Run the application using the following command from the project’s root directory.

npm start

Output:

 

Example 2: In this example, to the above code we have created an item state using react useState hook, that adds the items to the list.

App.js




import React, { useState } from "react";
import "@blueprintjs/core/lib/css/blueprint.css";
import { MenuItem } from "@blueprintjs/core";
import { Select2 } from "@blueprintjs/select";
import "@blueprintjs/select/lib/css/blueprint-select.css";
 
function App() {
    const [item, setItem] = useState("");
    const addItem = (s) => {
        return s;
    }
    return (
        <div style={{ margin: 30 }}>
            <h3>ReactJs Blueprint Select2 Creating new items</h3>
            <Select2
                createNewItemFromQuery={addItem}
                createNewItemRenderer={(val) => {
                    return <MenuItem
                        key={val}
                        text={val}
                        onClick={(elm) => {
                            setItem(elm.target.textContent);
                        }}
                    />
                }}
                items={[
                    "Blockchain",
                    "Internet of things"
                ]}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            onClick={(elm) => {
                                setItem(elm.target.textContent);
                            }}
                        />
                    );
                }}
                onItemSelect={setItem}
            >
                <button>click me</button>
 
                <h5>Selected : {item}</h5>
            </Select2>
        </div>
    );
}
 
export default App;

Step to Run Application: Run the application using the following command from the project’s root directory.

npm start

Output:

 

Reference: https://blueprintjs.com/docs/#select/select2.creating-new-items


Article Tags :