Open In App

React.js Blueprint Suggest

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.

Suggest component offered by BlueprintJS: Suggest component renders a UI that allows us to choose among multiple items from a list. It renders a text input as the Popover component target instead of arbitrary children.



Suggest props:

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 @blueprintjs/core
npm install @blueprintjs/select

Project Structure:  It will look like the following:

 

Example 1: In this example, we will try to create a simple dropdown application that allows us to search among items using Suggest component provided by BlueprintJS.

Now write down the following code in the App.js file. Here, App is our default component where we have written our code:




import './App.css';
import { Suggest } 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';
  
const defaultItems = ['USA', 'CANADA', 'INDIA']
  
function App() {
    const [item, setItem] = useState('A');
  
    return (
        <div style={{ width: '200px' }}>
            <Suggest
                activeItem={item}
                items={defaultItems}
                selectedItem={item}
                itemPredicate={(query, film, _index, exactMatch) => {
                    const normalizedTitle = film?.toLowerCase();
                    const normalizedQuery = query.toLowerCase();
  
                    if (exactMatch) {
                        return normalizedTitle === normalizedQuery;
                    } else {
                        return normalizedTitle.includes(normalizedQuery)
                    }
                }}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            onClick={(elm) => {
                                setItem(elm.target.textContent)
                            }}
                            active={itemProps.modifiers.active}
                        />
                    );
                }}
                onItemSelect={() => { }}
                tagRenderer={(item) => item}
                inputValueRenderer={(item) => item.toString()}
            />
        </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 put an initial content that gets rendered when there is no query. Change your App.js like the one below.




import './App.css';
import { Suggest } 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';
  
const defaultItems = ['USA', 'CANADA', 'INDIA']
  
function App() {
    const [item, setItem] = useState('A');
  
    return (
        <div style={{ width: '200px' }}>
            <Suggest
                initialContent={'GERMANY'}
                activeItem={item}
                items={defaultItems}
                selectedItem={item}
                itemPredicate={(query, film, _index, exactMatch) => {
                    const normalizedTitle = film?.toLowerCase();
                    const normalizedQuery = query.toLowerCase();
  
                    if (exactMatch) {
                        return normalizedTitle === normalizedQuery;
                    } else {
                        return normalizedTitle.includes(normalizedQuery)
                    }
                }}
                itemRenderer={(val, itemProps) => {
                    return (
                        <MenuItem
                            key={val}
                            text={val}
                            onClick={(elm) => {
                                setItem(elm.target.textContent)
                            }}
                            active={itemProps.modifiers.active}
                        />
                    );
                }}
                onItemSelect={() => { }}
                tagRenderer={(item) => item}
                inputValueRenderer={(item) => item.toString()}
            />
        </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:

 

Reference: https://blueprintjs.com/docs/#select/suggest


Article Tags :