Open In App

React.js Blueprint MultiSelect2

In this article, we will learn about the MultiSelect2 component offered by the blueprint.js library. BlueprintJS is a React-based UI toolkit for the web. It is written in Typescript. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications that run in a modern web browser.

MultiSelect2 component offered by BlueprintJS: MultiSelect2 component renders a UI that allows us to choose among multiple items from a list. It renders a TagInput component wrapped in a Popover2 component, both offered by BlueprintJS.



MultiSelect2 props:

Creating React Application And Installing Module:



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

npx create-react-app foldername

Step 2: Use the following command to move to your project folder after creating it, i.e., foldername:

cd foldername

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

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

Project Structure: It will look like the following.

 

Example 1: Now write down the following code in the App.js file. Our default component, App, is where we have written our code in this case.




import './App.css';
import { MultiSelect2 } from "@blueprintjs/select";
import { MenuItem } from "@blueprintjs/core";
import "normalize.css";
import "@blueprintjs/core/lib/css/blueprint.css";
import "@blueprintjs/select/lib/css/blueprint-select.css";
import { useState } from 'react';
  
function App() {
    const [item, setItem] = useState('A');
    const [items, setItems] = useState([]);
  
    return (
        <div className="container" style={{ width: '30%' }}>
            <MultiSelect2
                activeItem={item}
                items={["A", "B", "C"]}
                selectedItems={items}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            onClick={(elm) => {
                                setItem(elm.target.textContent)
                                setItems((items) => {
                                    return [...items, elm.target.textContent]
                                })
                            }}
                            active={itemProps.modifiers.active}
                        />
                    );
                }}
                onItemSelect={() => { }}
                tagRenderer={(item) => item}
                onRemove={(item) => {
                    setItems(items => items.filter(elm => elm !== item))
                }}
                onClear={() => setItems([])}
            />
        </div>
    );
}
  
export default App;

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:

 

Example 2: In this example, let’s change the placeholder for search input. Change your App.js like the below:




import './App.css';
import { MultiSelect2 } from "@blueprintjs/select";
import { MenuItem } from "@blueprintjs/core";
import "normalize.css";
import "@blueprintjs/core/lib/css/blueprint.css";
import "@blueprintjs/select/lib/css/blueprint-select.css";
import { useState } from 'react';
  
function App() {
    const [item, setItem] = useState('USA');
    const [items, setItems] = useState([]);
  
    return (
        <div className="container" style={{ width: '30%' }}>
            <MultiSelect2
                tagInputProps={{ placeholder: 'Search for a country' }}
                activeItem={item}
                items={["India", "USA", "Canada"]}
                selectedItems={items}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            onClick={(elm) => {
                                setItem(elm.target.textContent)
                                setItems((items) => {
                                    return [...items, elm.target.textContent]
                                })
                            }}
                            active={itemProps.modifiers.active}
                        />
                    );
                }}
                onItemSelect={() => { }}
                tagRenderer={(item) => item}
                onRemove={(item) => {
                    setItems(items => items.filter(elm => elm !== item))
                }}
                onClear={() => setItems([])}
            />
        </div>
    );
}
  
export default App;

Output:

 

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


Article Tags :