Bacancy Bacancy
  • Customers

      Fortune 500 Clients

      Verizon
      Verizon
      Franklin Templeton
      Franklin Templeton
      The Container Store
      The Container Store
      KPMG
      KPMG
      Academy Sports & Outdoors
      Academy Sports & Outdoors
      Shell
      Shell
      Bangkok Bank Ltd
      Bangkok Bank Ltd
      The Children's Place
      The Children's Place
      Edward Jones
      Edward Jones
      United Parcel Service
      United Parcel Service
      AdviceWorks
      AdviceWorks
      NatWest Markets
      NatWest Markets
      Bridgestone
      Bridgestone

      Technologies

      React JS VueJS Angular JS React Native Flutter Full Stack
      Ruby on Rails Node JS Golang AI & ML Data Science Java
      Python Salesforce DevOps Block Chain Mobile App Embedded & Hardware
      Explore All
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • what we do

      Experience

      Digital Workplace Services Product Enhancement

      Insights

      AI & Automation Data Analytics

      Innovate

      Blockchain Software Engineering Services Internet of Things (IoT) Data Science Artificial Intelligence Machine Learning

      Accelerate

      Agile & DevOps Cloud - Bacancy Combalt Digital Process Automation Open Source Microsoft Dynamics Salesforce

      Assure

      Quality Assurance IT Staff Augmentation

      Embedded & Hardware

      Product Engineering Analog Design Services FPGA Engineering Service Wireless Design Embedded Software Design

      not list

      Hardware Engineering Power Engineering IoT Hardware
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • who we work with

      Industries

      Banking Financial services & Insurance Startups Oil & Gas

      not list

      Healthcare Life Science Real Estate & Construction Logistics
      bacancy

      Let’s grow together Partner with us

      get quote
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • about us

      Culture

      Agile Mindset Bacancy Values Bacancy Culture

      About Company

      About Us Leadership Team Awards & Recognition Infrastructure

      not list

      Customer Reviews Our Locations Partnership Media Coverage

      WE ARE

      GREAT PLACE TO WORK - CERTIFIEDTM

      Building and sustaining High-Trust, High-Performance CultureTM

      Bacancy Great Place to Work
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • technology

      Front End

      Angular Reactjs Vue.js UI/UX

      Platforms

      MS Dynamics Salesforce

      Back End

      Ruby on Rails Node.js Golang Laravel .NET Java Python

      Mobile

      React Native Flutter Android iOS

      Cloud

      AWS DevOps

      QA

      Automation Testing Software Testing

      Embedded & Hardware

      Embedded
      bacancy

      World-class expertise,Delivered

      get quote
      Explore All
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
  • PRODUCTS

      EV Products

      Battery Management System Motor Controller AC Charger Explore All

      not list

      CCS2 Controller DC Fast Charger AC Charge Controller
      bacancy

      Let’s grow together Partner with us

      get quote
      skype-icon

      Skype ID

      bacancy
      gmail-icon

      Email Us

      [email protected]
      call-yellow-icon

      USA

      +1 347 441 4161
      wp-icon

      Canada

      +1 416 907 6738
hire us

Bacancy

Bacancy represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.

12+

Countries where we have happy customers

1050+

Agile enabled employees

06

World wide offices

10+

Years of Experience

05

Agile Coaches

14

Certified Scrum Masters

1000+

Clients projects

1458

Happy customers

  • Customers
      Technologies
      • React JS
      • Angular JS
      • Vue JS
      • Node JS
      • AI & ML
      • Explore All
      Fortune 500 Clients
      • Verizon
      • The Container Store
      • KPMG
      • Academy Sports & Outdoors
      • Bangkok Bank Ltd
      • The Children's Place
      • Edward Jones
      • Franklin Templeton
      • AdviceWorks
      • NatWest Markets
      • Bridgestone
      • United Parcel Service
  • What we do
      Experience
      • Digital Workplace Services
      • Product Enhancement
      Insights
      • AI & Automation
      • Data Analytics
      Innovate
      • Blockchain
      • Software Engineering Services
      • Internet of Things (IoT)
      Accelerate
      • Agile & DevOps
      • Cloud - Bacancy Combalt
      • Digital Process Automation
      • Open Source
      • Microsoft Dynamics
      • Salesforce
      • Service Experience Transformation
      Assure
      • Testing
      • Resource Augmentation
      Embedded & Hardware
      • Product Engineering
      • Analog Design Services
      • FPGA Engineering Service
      • Wireless Design
      • Embedded Software Design
      • Hardware Engineering
      • Power Engineering
      • IoT Hardware
      • AL/ML
  • who we work with
      Industries
      • Banking Financial services & Insurance
      • Startups
      • Oil & Gas
      • Healthcare Life Science
      • Real Estate & Construction
      • Logistics
  • About Us
      About Company
      • About Us
      • Leadership Team
      • Awards & Recognition
      • Infrastructure
      • Our Locations
      • Partnership
      Culture
      • Agile Mindset
      • Bacancy Values
      • Bacancy Culture
  • Technology
      Front End
      • Angular
      • Reactjs
      • Vue.js
      • UI/UX
      Platforms
      • MS Dynamics
      • Salesforce
      Back End
      • Ruby on Rails
      • Node.js
      • Golang
      • Laravel
      • .NET
      • Java
      • Python
      Mobile
      • React Native
      • Flutter
      • Android
      • iOS
      Cloud
      • AWS
      • DevOps
      QA
      • Automation Testing
      • Software Testing
      Embedded & Hardware
      • Embedded
      Explore All
  • Products
    • Battery Management System
    • Motor Controller
    • CCS2 Controller
    • AC Charger
    • DC Fast Charger
    • AC Charge Controller
    • Explore All
  • Careers
  • Work @ Bacancy
  • Blogs
  • Resources
  • Customer Reviews
  • Contact
  • Get Quote
React Filter Component

A Comprehensive Guide on Developing Responsive and Common React Filter Component

Archita Nayak
Archita Nayak Technical Writer
Last Updated on April 29, 2022
January 20, 2021 7 min read

Your end-users always expect the UI to be more consistent, comfortable, and attractive in web and mobile applications. With that consideration, the product should be developed, keeping the user interface’s consistency in their minds. This task can be challenging when there’s a need for a responsive website for different screen widths and layouts. So, I have come up with this blog that will help you build a Responsive React Filter Component. Without much ado, let’s start coding.

Table of Index

1. User Interface for Responsive React Filter Component

2. Steps for building React Filter Component:

  • Building a Button controlling React Filter Component
  • Design of the Button using CSS
  • Use of Ref and useEffect
  • Implementing Modal for Mobile Devices using Reach UI
  • Putting all the pieces together

3. Conclusion

User Interface for Responsive React Filter Component

Before discussing how to build a responsive React Filter Component, let’s see the UI and its use case. So, here is the mockup of the component that we are going to build. The main idea of it is – on clicking the button named JS Frameworks, a dropdown will be displayed for web applications, and for mobile applications, it will cover the entire screen. I hope you have understood the UI, structure, and need.

Responsive React Filter Component

Steps for building React Filter Component

As we discussed earlier, we wanted to build a component that satisfies various screen widths. Thus, I would create one common component for this filter, which would decide which component should be displayed depending on different screen widths. But, before that, we need to do a lot more code. For simplifying it, I have divided it into five parts, maintaining the flow to code at ease.

Looking for assistance that built React Filter Component in your app?
You click away. Connect with the best ReactJS Development company for building React Filter Components with visually appealing apps.

Building a Button controlling React Filter Component

In the first step, I will implement a Button having the state value for opening and closing the filter. I have also given a basic styling to the button to make it look good; you can change the design at your convenience.

// FilterComponent.js

import React, { useState } from 'react';
import './FilterComponent.css';
‍
function FilterComponent() {
 // Declaring a new state variable named "isOpen"
 const [isOpen, setIsOpen] = useState(false);
 
 return (
   < div className="filter_wrapper" >
     < button
       onClick={() => setIsOpen(!isOpen)}
       className="filter_button"
     >
       JS Frameworks
     < /button >
   < /div >
 );
}

Design of the Button using CSS

// FilterComponent.css

.filter_wrapper {
 position: relative;
 display: inline-block;
}
.filter_button {
 border-radius: 5px;
 padding: 8px 16px;
 background-color: #408aeb;
 cursor: pointer;
 font-weight: 500;
 color: white;
 font-size: 18px;
 line-height: 1.5;
}
.filter_button:hover {
 background-color: #092b58;
}
.filter_button:focus {
 outline: 1px dashed;
 outline: 1px auto -webkit-focus-ring-color;
}

Use of Ref and useEffect

So far, we have made our React Filter component and styled it with a basic design. One more thing to be done is whenever users click outside an opened dropdown; they would expect the dropdown to be closed. For that, I will use the Ref and useEffect.

useEffect(() => {
   const handleClick = event => {
     const isDropdownClicked =
       dropdownRef.current && dropdownRef.current.contains(event.target);
     const isButtonClicked =
       buttonRef.current && buttonRef.current.contains(event.target);
 
     
if (isDropdownClicked || isButtonClicked) {
       // We would do nothing if the ref is undefined or the user clicks on the menu.
       return;
     }
     // Or else close the menu.
     setIsOpen(false);
   };
    
     document.addEventListener("mousedown", handleClick); 
     document.addEventListener("touchstart", handleClick); 
 
   // cleanup
   return () => {
     document.removeEventListener("mousedown", handleClick);  
     document.removeEventListener("touchstart", handleClick);   
   };
 }, [dropdownRef, buttonRef]);

Tip – You can use a custom user hook, useClickOutside, and write your logic of useEffect there to avoid cluttering in your component.

Let’s have a glance at our component. So far, so good!

Building responsive filter component

Now further, we need to implement this for mobile devices.

Implementing the Modal for Mobile Devices using Reach UI

The concept of modal might simple- to cover the entire screen with the title at the top. But, when it comes to implement and start coding, believe me, it can be challenging. We would be using Reach UI for managing focus states, semantics, and other difficulties related to the modal components. You can prefer any library which has a Modal component; it’s totally up to your choice. Since I’m familiar with Reach UI, I would go with that.

I’ll create a component – FilterModalComponent, that would contain my Modal. And later, I’ll import this into my FilterComponent.

// FilterModalComponent.js

import React from "react";
import { DialogOverlay } from "@reach/dialog";
‍import "./FilterModalComponent.css";
 
const FilterModalComponent = React.forwardRef(‍
 ({ children, onSelect, onCancel }, ref) => {
   return (
    < div className="modal_wrapper" >
     < DialogOverlay
       ref={ref}
       className="modal_container"
       aria-label="modal window"
     >
       < div className="modal_header" >
         < button onClick={() => onCancel()} >x< /button >
       < /div >
       < div className="modal_content" >{children}< /div >
       < div className="modal_actions" >
         < button onClick={() => onSelect()} >Select< /button >
       < /div >
     < /DialogOverlay >
    < /div >
   );
 });
export default FilterModal;

And the CSS file of FilterModal is here –

// FilterModalComponent.css

.modal_wrapper {
 display: block;
 background-color: transparent;
}
 
 
@media (min-width: 768px) {
 .modal_wrapper {
   display: none;
 }
}
 
.modal_container {
 z-index: 60;
 display: flex;
 background-color: white;
 right: 0;
 flex-direction: column;
 position: fixed;
 top: 5px;
}
 
.modal_header {
 padding: 20px 12px;
 border-bottom-color: #e4e6eb;
 display: flex;
 border-bottom-width: 10px;
}
 
.modal_content {
 display: flex;
}
 
.modal_actions {
 display: flex;
 align-items: center;
 border-top-width: 3px;
 justify-content: flex-end;
 margin-top: 5px;
 border-top-color: #e4e7eb;
 padding: 8px;
}
 
.modal_actions button {
 font-weight: 500;
 border-radius: 2px;
 padding: 5px 8px;
 cursor: pointer;
 background-color: #2b7de9;
 width: auto;
 color: white;
}

Our Filter Modal component is ready, which will only appear for small screens because of this –

@media (min-width: 768px) {
 .modal_wrapper {
   display: none;
 }
}

This is how our component will look like.

our component

Now, it’s time to import this FilterModalComponent into the FilterComponent. Keep in mind that modal expects Ref to prevent it from closing unexpectedly, and other props like – onSelect and onCancel, for controlling the user’s actions.

import React, { useState, useRef, useEffect } from "react";
import "./FilterComponent.css";
‍import FilterModalComponent from "./FilterModalComponent";
 
export default function FilterComponent() {
 const [isOpen, setIsOpen] = useState(false);
 const dropdownRef = useRef(undefined);
 const buttonRef = useRef(undefined);
 const modalRef = useRef(undefined);
 
useEffect(() => {
 const handleClick = event => {
 
const isDropdownClicked = dropdownRef.current && dropdownRef.current.contains(event.target);
 const isButtonClicked = buttonRef.current && buttonRef.current.contains(event.target);
 const isModalClicked = modalRef.current && modalRef.current.contains(event.target);
 
 if (isDropdownClicked || isButtonClicked || isModalClicked) {
  // We would do nothing if the ref is undefined or user clicks on menu.
   return;
 }
 
 // Or else close the menu.  
 setIsOpen(false);
 };
 
 document.addEventListener("mousedown", handleClick);
 document.addEventListener("touchstart", handleClick); 
 
 // cleanup
 return () => {
   document.removeEventListener("mousedown", handleClick);
   document.removeEventListener("touchstart", handleClick);
 };
}, [dropdownRef, buttonRef, modalRef]);
‍
const handleSelect = () => {‍
 alert("Yay! Filters applied!");‍
 setIsOpen(false);
‍};
 
return (
 < div className="filter_wrapper" > 
   < button 
     ref={buttonRef}
     onClick={() => setIsOpen(!isOpen)}
     className="filter_button"
   >
     JS Frameworks
  < /button >
‍   {isOpen && (
   < div ref={dropdownRef} className="filter_dropdown" >
‍     < div >
‍       Dropdown content goes here
‍       < div className="filter_dropdown_actions" >
‍         < button onClick={() => handleSelect()}      className="filter_dropdown_button" >
         Select
        < /button >
      < /div >
    < /div >
  < /div >
)}
      
 {isOpen && (
   < FilterModalComponent
‍     ref={modalRef}
     onSelect={() => handleSelect()}
     onCancel={() => setIsOpen(false)}
   >
    Modal content goes here.
‍   < /FilterModalComponent >
  )}
‍  < /div >
 );
}

Putting all the pieces together

Now, it’s time to give actual user inputs and see how it works. We are going to implement multiple select filters from which users can choose the JS framework.

import React, { useState } from "react";
‍import "./main.css";
‍import FilterComponent from "./FilterComponent";
 
const frameworks = ["ReactJS", "AngularJS", "VueJS", "NodeJS", "EmberJS"];
 
export default function DemoApp() {
‍
 const [selectedFrameworks, setSelectedFrameworks] = useState([]);
 
 const handleSelect = framework => {
   const isSelected = selectedFrameworks.includes(framework);
   const newSelection = isSelected
   ? selectedFrameworks.filter(currentTech => currentTech !== framework)
   : [...selectedFrameworks, framework];
   setSelectedFrameworks(newSelection);};
 
return (
 < div className="app_container" >
   < h2 >Building responsive filter component< /h2 >
   < FilterComponent
      label="JS Frameworks" 
      onSelect={() => alert(selectedFrameworks)}
    >
‍     < div className="frameworks-list" >
       {frameworks.map((framework, index) => {
         const isSelected = selectedFrameworks.includes(framework);
         return (
           < label key={index} >
             < input
               type="checkbox"
               checked={isSelected}
               onChange={() => handleSelect(framework)}
             / >
‍             < span className="ml-2 text-base text-gray-500 font-heading" >
‍               {framework}
‍             < /span >
           < /label >   
           ); 
         })}
     < /div >
‍     < /FilterComponent >
 < /div >
 );
}

This is how our Filter component with checkboxes of JS frameworks looks like –

 Filter component

And on mobile screens –

mobile screens

Conclusion

So, this was about how to build a responsive filter component. I hope you are pretty clear about its implementation. If you are looking for any development support regarding ReactJS development, then Bacancy Technology is a one-stop solution to hire ReactJS developer to build an optimal product.


Expand Your Digital Horizons With Us.

Start a new project or take an existing one to the next level. Get in touch to start small, scale-up, and go Agile.


Or
E-mail us : [email protected]

Your Success Is Guaranteed !

Related Articles

React Storefront
Paridhi Wadhwani

January 6, 2023

React JS

Leverage React Storefront to Empower your Ailing eCommerce Business

By : Paridhi Wadhwani

Quick Summary: Although digital businesses exist earlier, the pandemic forced maximum mortar-and-brick businesses to go digital and adopt advanced eCommerce platforms and backend systems, thereby...

12 Minute Read
Read More
React Performance
Paridhi Wadhwani

December 7, 2022

React JS

Top 11 React Performance Optimization Techniques in 2023

By : Paridhi Wadhwani

Quick Summary: Developers and product owners are often scared of the performance issues when dealing with a React Js application. This unpopular belief has kept...

10 Minute Read
Read More
React Micro Frontend
Paridhi Wadhwani
Bhumik Prajapati

November 7, 2022

React JS

Everything About React Micro Frontend

By : Paridhi Wadhwani & Bhumik Prajapati

Quick Summary: This blog covers a complete guide on how one can implement React Micro frontend along with a firm understanding of the React Microservices...

8 Minute Read
Read More

india India (HQ)

Corporate House
15-16, Times Corporate Park, Thaltej, Ahmedabad, Gujarat 380059

USA USA

601 Brickell Key Drive, Suite 700, Miami, Florida, 33131, USA

canada Canada

71 Dawes Road, Brampton, On L6X 5N9, Toronto

australia-flag Australia

351A Hampstead Rd, Northfield SA 5085

UAE UAE

1608 Clover Bay, Business Bay, Dubai, UAE. PO Box 62049

sweden Sweden

Junkergatan 4, 126 53 Hagersten


Great Place to Work

Get in Touch

call-yellow-icon

Contact Number

+1 347 441 4161

gmail-icon

Email Us

[email protected]


  • Bacancy Behance
  • Bacancy Uplabs
  • Bacancy Pinterest
  • Brochure
  • Quality Assurance
  • Resources
  • Tutorials
  • Customer Reviews
  • Privacy Policy
  • FAQs
  • Contact Us
  • Sitemap
  • Employee
bacancy google review 4.6
bacancy google review
bacancy glassdoor review 4.5
bacancy glassdoor review
bacancy clutch review 4.8
bacancy clutch review
bacancy goodfirms review 4.5
bacancy goodfirms review
X

Get Our Newsletter

Be The First To Get The Latest Updates And Tutorials.

Request A Free Consultation