Hello there, I have made a Dropdown Select Menu in React. In this tutorial, I will teach you How to create a custom Dropdown Select Menu in React Js.
You may already know about the select
element in regular HTML, It creates a dropdown select menu. Inside select
we use the option
element to define the menu options, as in the below example:
Basic Dropdown Select Menu in HTML
This is known as a basic method of implementing a Dropdown Select Menu in HTML.
As I have said it's a basic method, a basic method in the sense not about its functionality it's about the design, user experience, user-friendly, etc.
So, Here I will teach you and explain to you how to create a Dropdown Select Menu in React, without using select
elements. Let's continue.
Dropdown Select Menu in React JS Example
Learn: How to create a Dropdown Select Menu in React JS?
We will walk through the process of creating a dropdown select menu in React JS step by step, by making a country name selection dropdown menu.
Prerequisites
Before we dive into the implementation, make sure you have a basic understanding of ReactJs.
Step 1: Set up your project by creating a new React app
First, you go inside any directory in your system and create a new React app by using the create-react-app
command.
Then go to dropdown-select-menu
folder by running this command
Then run your React App
Step 2: Create a basic structure using the Functional Component
In your project, navigate to the src
folder, then go to the root component App.js
in this file React provides you with some code by default. Delete all the code here.
Then create the basic structure of the Dropdown Select Menu, as in the below code example:
Here, first, import the App.css
file, to apply CSS styles to this component.
Then import the FontAwesomeIcon
component from the @fortawesome/react-fontawesome
library. This component is used to display icons from the FontAwesome icon library in your React application.
Read also: CSS Gradient Border Glowing Animation Hover Effect
To use @fortawesome library you need to first install it in your project. So, install these following packages by using npm
After installing these packages first go to the index.js file, here you have to import the library object and icons as given example below:
As you can see in the above example,
- The
library
object is imported from@fortawesome/fontawesome-svg-core
and- The
faAngleDown
icon is imported from@fortawesome/free-solid-svg-icons
Then add the imported icon (
faAngleDown
) to thelibrary
like this:Now you can use this icon in any component throughout your application without the need to import them individually in each component. For more details you can read React FontAwesome Docs
Then inside the return
statement add the JSX code to create the basic structure.
In the JSX:
<div className="App">
: Thisdiv
element serves as the main container for the entire component.<div className="selectOption">
: Thisdiv
element represents the container for the custom select input.<input type="text" placeholder="Select Your Country" readOnly/>
: This is an<input>
element of type “text”. It acts as a text input field where users can input or select a country. Theplaceholder
attribute provides a hint or prompt to the user (“Select Your Country”), and thereadOnly
attribute indicates that the input field is not editable by the user.<span className="icon">
: Thisspan
element is used to hold the dropdown icon.<FontAwesomeIcon icon={["fas", "angle-down"]} />
: This seems to use theFontAwesomeIcon
component from a library@fortawesome/react-fontawesome
to display an icon. Theicon
prop specifies the icon to be displayed, in this case, the “angle-down” icon from the Font Awesome icon set.<div className="options">
: Thisdiv
element is the container for the dropdown options.<li></li>
: Thisli
element is for the actual dropdown options. Basically, here we will use the array map() method to render each option dynamically as li elements. We will do it further.
Step 3: Add functionality and logic
After creating the basic structure of the dropdown select menu in React, let's add the functionality,
First import the useState
and useRef
hooks which will be used for managing component state and creating a reference to a DOM element. Like this:
Then create an Array called optionsArray
contains a list of country names.
After creating this array, inside the div
element whose class name is options
, we will use a map()
function to iterate over each item of optionsArray
and render a corresponding <li>
element for each option. Like this:
Then create a state variable named openselect
and a setter function setOpenSelect
using the useState
hook.
Open/Close Dropdown Select Menu in React JS
This functionality is for when users click on the input element the dropdown menu will open or its state change to true
and when the user clicks outside the dropdown menu it will be closed or its state change to false
.
The initial state openselect
is false
. This state is used to control whether the dropdown options are displayed or not.
We use the useRef
hook** to create a reference object called inputRef
.
Add this inputRef
object using ref attribute into the input
element, Like this:
This reference will be used to directly manipulate the input
element representing the selected option in the custom dropdown select menu in react.
Update the dropdown select menu selected value on the selection.
Then create an event handler function called selectValue
, like this:
The selectValue
function for handling the selection of an option from the dropdown. When an option ( li
element ) is clicked, this function sets value
of the input element (using inputRef
) to the text content of the clicked option or li
element text value.
Additionally, it closes the dropdown by setting openselect
to false
. So that, after selecting the option or clicking on the li
element the dropdown menu will be closed.
So add this function into the li
element using the onClick
attribute, like this:
Then, create another event handler function named openOption
Add this openOption
function into the input
element using the onClick
attribute, like this:
The openOption
function is triggered when the input
element is clicked. It opens the dropdown by updating the state of openselect
to true
.
Also as you can see onBlur={() => { setOpenSelect(false); }}
: This attribute attaches an anonymous arrow function to the onBlur
event of the <input>
element.
The onBlur
event occurs when the element loses focus, it means when the user clicks outside the input element, this function is invoked, updating the state of openselect
to false
. So the dropdown will be closed.
After doing all these things, The <div>
the element whose class name we defined options
, change its className
to className={openselect ? "options active" : "options"}
As you can see this is a conditional expression.
The condition is used to determine the class name. If openselect
is true
, the class "options active"
will be applied, and when active
is applied the options menu will be open (We will implement it in the CSS part) otherwise, if openselect
is false
, the class options
will be applied.
Secondly, The <div>
the element whose class name we defined icon
, also change its className
to className={openselect ? "icon active" : "icon"}
Here we have also added an active
class for active styling based on the value of the openselect
state.
So, finally, the full code of our App.js
functional root component is given below:
Step 4: Styling the component using CSS
Then we will add the styles by writing CSS in App.css
file. I have given the CSS code below you can copy it and use it:
Conclusion
Finally, we have completed our project and successfully built the Dropdown Select Menu in React JS. You can use this custom Dropdown Select Menu in your personal project and freelance project as well. Happy Coding 😀
Read also the viral blog: Email Input Validation using HTML, CSS, & JavaScript