React JS Bootstrap Dropdown Troubleshooting

I'm facing a challenge in my React.js project while trying to implement a filter. The issue is that the list appears when I click on the button, but it doesn't disappear on the second click or if I click outside the list.

Initially, I imported the bootstrap CSS which solved the button problem but caused some styling conflicts, so I ended up commenting out the import. Can anyone assist me in resolving either of these issues?

import React, { useEffect, useState } from 'react';
// import '../bootstrap/css/bootstrap.css';
import "bootstrap/dist/js/bootstrap.min.js";
import Popper from 'popper.js';
import Dropdown from 'react-bootstrap/Dropdown';
import DropdownButton from 'react-bootstrap/DropdownButton';
import './Filterbar.css';

export default function Filterbar() {
    function handlePropertyTypeChange(event){
        console.log(event.target.value);
        event = setPropteryType(event.target.value);
    };
    function handlePriceRangeChange(event){
        console.log(event.target.value);
        event = setPriceRange(event.target.value);
    };
    function handleBedroomChange(event){
        console.log(event.target.value);
        event = setBedroomQTY(event.target.value);
    };
    function handleBathroomChange(event){
        console.log(event.target.value);
        event = setBathroomQTY(event.target.value);
    };
    function handleCityChange(event){
        console.log(event.target.value);
        event = setCityChoice(event.target.value);
    };

    const [propertyType, setPropteryType] = useState('');
    const [priceRange, setPriceRange] = useState('');
    const [BedroomQTY, setBedroomQTY] = useState('');
    const [BathroomQTY, setBathroomQTY] = useState('');
    const [cityChoice, setCityChoice] = useState('');

    return (
        <div className='FilterbarBody'>

            <DropdownButton type="button" id="dropdown-basic-button" title="Property Type &#8964;" data-bs-toggle="dropdown" aria-expanded="false">
                <Dropdown.Item value={0} name="Property Type" onChange={handlePropertyTypeChange} aria-labelledby="dropdown-basic-button">All</Dropdown.Item>
                <Dropdown.Item value={1} name="Property Type" onChange={handlePropertyTypeChange} aria-labelledby="dropdown-basic-button">Single Family Home</Dropdown.Item>
                <Dropdown.Item value={2} name="Property Type" onChange={handlePropertyTypeChange} aria-labelledby="dropdown-basic-button">Multi Family Home</Dropdown.Item>
            </DropdownButton>

            <DropdownButton id="dropdown-basic-button2" title="Price &#8964;">
                <Dropdown.Item value={0} name="Price Range" onChange={handlePriceRangeChange}>All</Dropdown.Item>
                <Dropdown.Item value={500} name="Price Range" onChange={handlePriceRangeChange}>500+</Dropdown.Item>
                <Dropdown.Item value={1000} name="Price Range" onChange={handlePriceRangeChange}>1,000+</Dropdown.Item>
                <Dropdown.Item value={1500} name="Price Range" onChange={handlePriceRangeChange}>1,500+</Dropdown.Item>
                <Dropdown.Item value={2000} name="Price Range" onChange={handlePriceRangeChange}>2,000+</Dropdown.Item>
                <Dropdown.Item value={2500} name="Price Range" onChange={handlePriceRangeChange}>2,500+</Dropdown.Item>
                <Dropdown.Item value={3000} name="Price Range" onChange={handlePriceRangeChange}>3,000+</Dropdown.Item>
            </DropdownButton>

            <DropdownButton id="dropdown-basic-button3" title="Bedroom &#8964;">
                <Dropdown.Item value={0} name="Bedroom" onChange={handleBedroomChange}>All</Dropdown.Item>
                <Dropdown.Item value={1} name="Bedroom" onChange={handleBedroomChange}>1+</Dropdown.Item>
                <Dropdown.Item value={2} name="Bedroom" onChange={handleBedroomChange}>2+</Dropdown.Item>
                <Dropdown.Item value={3} name="Bedroom" onChange={handleBedroomChange}>3+</Dropdown.Item>
                <Dropdown.Item value={4} name="Bedroom" onChange={handleBedroomChange}>4+</Dropdown.Item>
                <Dropdown.Item value={5} name="Bedroom" onChange={handleBedroomChange}>5+</Dropdown.Item>
                <Dropdown.Item value={6} name="Bedroom" onChange={handleBedroomChange}>6+</Dropdown.Item>
            </DropdownButton>

            <DropdownButton id="dropdown-basic-button4" title="Bathroom &#8964;">
                <Dropdown.Item value={0} name="Bathroom" onChange={handleBathroomChange}>All</Dropdown.Item>
                <Dropdown.Item value={1} name="Bathroom" onChange={handleBathroomChange}>1+</Dropdown.Item>
                <Dropdown.Item value={2} name="Bathroom" onChange={handleBathroomChange}>2+</Dropdown.Item>
                <Dropdown.Item value={3} name="Bathroom" onChange={handleBathroomChange}>3+</Dropdown.Item>
                <Dropdown.Item value={4} name="Bathroom" onChange={handleBathroomChange}>4+</Dropdown.Item>
                <Dropdown.Item value={5} name="Bathroom" onChange={handleBathroomChange}>5+</Dropdown.Item>
                <Dropdown.Item value={6} name="Bathroom" onChange={handleBathroomChange}>6+</Dropdown.Item>
            </DropdownButton>

            <DropdownButton id="dropdown-basic-button5" title="City &#8964;">
                <Dropdown.Item value={""} name="City" onChange={handleCityChange}>All</Dropdown.Item>
                <Dropdown.Item value={"Cape Coral"} name="City" onChange={handleCityChange}>Cape Coral</Dropdown.Item>
                <Dropdown.Item value={"Fort Myers"} name="City" onChange={handleCityChange}>Fort Myers</Dropdown.Item>
                <Dropdown.Item value={"Lehigh Acres"} name="City" onChange={handleCityChange}>Lehigh Acres</Dropdown.Item>
            </DropdownButton>
        </div>
    )
}
.FilterbarBody {
    display: block;
    background-color:gainsboro;
    width: 53em;
    height: 2.50em;
    margin-top: 1em;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 1.50em;
    border: .13em solid #808080;
    font-style: 'sans-serif';
    font-size: 1.25em;
}
.FilterbarBody > .dropdown {
    display:inline-block;
    margin-top: auto;
    margin-bottom: auto;
    margin-left: auto;
    margin-right: auto;
    text-decoration: none;
    font-style: 'sans-serif';
}
.FilterbarBody > .dropdown > button {
    display: block;
    margin-left: 1.23em;
    margin-right: 1.23em;
    text-decoration: none;
    background-color: rgba(250, 235, 215, 0);
    padding: .75em 1.75em;
    font-size: 1em;
    list-style: none;
    border: none;
    border-radius: .50em;
}
.FilterbarBody > .dropdown > button:hover {
    background-color:rgba(250, 240, 230, 0);
    color: red;
}
.dropdown-item {
    display:block;
    text-decoration: none;
    color: white;
    background-color: black;
    border-bottom: 0.010em solid linen;
    font-style: 'sans-serif';
    font-weight: .25em;
    padding: 1em 1.75em;
}
.dropdown-item:hover {
    background-color: linen;
    color: red;
}

Answer №1

After exploring different options, I decided to implement React Select, which is proving to be much more effective for my needs. You can see a demo below, and I will continue to provide updates here if any issues arise.

import React from 'react';
import ReactDOM from 'react-dom';
import Select from 'react-select';
import './Filterbar.css'

const propertyTypeOptions = [
  { value: 'All', label: 'All' },
  { value: 'Single Family Home', label: 'Single Family Home' },
  { value: 'Multi Family Home', label: 'Multi Family Home' },
];
// More code snippets...
form {
    margin-left: auto;
    margin-right: auto;
    width: 70em;
}
form > label {
    display: inline-block;
    font-family: 'sans-serif';
    font-size: 1.15em;
}
// More CSS styles...

<div id="root"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to remove elements from a JavaScript array: exploring the potential use of the delete function in JavaScript

I'm currently using Flot JS charts and I am attempting to completely remove a specific plot series from my data array either through jquery or plain javascript. Below is an example of what my data array consists of: [ { "label" : "Citrix PV Ether ...

Create geometric shapes on Google Maps version 2

Can anyone guide me on how to draw a polygon on Google Maps with user-input coordinates? I have two input fields for latitude and longitude, and when the user clicks the button, I want the polygon to be drawn. The code I currently have doesn't seem to ...

What is the method for obtaining a unique id that changes dynamically?

Having trouble accessing the dynamically generated ID in jQuery? It seems to work fine in JavaScript but not in jQuery. Here's an example of the issue: My jQuery code: var img = $("#MAP"+current_img_height); $("#map").css({'height': img.h ...

JavaScript: A step-by-step guide to extracting the file name and content from a base64 encoded CSV

I have a base64 string that was generated by encoding a csv file, const base64 = 'LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTExNDc2MDgwNjM5MTM4ODk4MTc2NTYwNA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlIjsgZmlsZW5hbWU9ImNoYXJ0T2ZBY2NvdW50LmNzd ...

My CSS disappears when using an ASP.Net MVC form

Unable to find any similar issue on Google... seeking some assistance. When I apply CSS formatting to style my form as desired, it displays correctly on the page without any issues. However... When I place the actual form within the following code snipp ...

Avoid altering the Vuex store state directly without using mutation handlers in VueJS

I am currently working on developing a listenAuth function that monitors the "onAuthStateChanged" event in firebase to inform the vuex store whenever a user logs in or out. From what I can gather, I am only updating state.authData using the mutation handle ...

"The issue with AngularJS ng-init, preventing the initialization of a variable at

For my project, I have implemented ng-repeat from the AngularJS directive to display an array containing JSON values with subarrays. <div ng-repeat="data in MENULIST" > //MENULIST contains an array of data I then perform certain conditions checks ...

Getting your JQuery ready() statement right is crucial for the proper

I have come across all three variations below: $().ready(); $(document).ready(); $(document.body).ready(); All of them seem to work, but I'm wondering which one is the most appropriate or recommended to use when considering the usage of the ready() ...

Triggering a form submission in JavaScript when a selection is changed

Welcome to my WordPress site! If you would like to check it out, visit this link. Currently, I have a form located next to the text "Filter By Location:". My goal is to have the form submitted automatically when one of the options is selected (onChange). ...

Next app unable to fetch cookie set by Express app

My project involves an app built on react/next and a server built on express. The process begins with a user authenticating via Github Oauth using passport.js, after which the server sends a connect.sid cookie. This cookie is crucial for rendering data to ...

Verify if the form has been refreshed in React JS

In my React application, I have a form inside a modal pop-up. When the user closes the pop-up, I want to check for any changes made in the form fields. If there are changes, I will display a confirmation modal; if not, I will simply close the pop-up. <F ...

NuxtJS Static generated HTML page fails to load JavaScript while accessing /index.html

I'm currently working on a project using Nuxt.js to generate static content, which involves utilizing JavaScript for tasks such as navigation and form functionality. Everything works smoothly when running the page with npm run dev. However, after ex ...

The effectiveness of the javascript widget is hindered by the absence of the meta viewport tag,

As I work on creating a widget to appear at the bottom of webpages, I've encountered a styling issue. The widget's display varies based on whether the webpage includes a specified meta viewport tag or not. <meta name="viewport" content="width ...

retrieving data from GET variables and sending to a PHP array

Is there a way to retrieve form variables and store them in an array in memory without reloading the page? I'm not very experienced with this, so any guidance would be appreciated. My goal is to update a JSON file using PHP based on form inputs. JSON ...

Tips for retaining input field content within a BootstrapVue table

Below is a BootstrapVue table I'm working with; https://i.sstatic.net/xu5Et.png The code, courtesy of this response, is showcased below; new Vue({ el: '#app', data() { return { filter: '', items: [ { i ...

Concentrate on implementing Cross-domain Ajax functionality in Opera browser

For a unique and interesting browsing experience, try using Opera 9.62 to explore the nuances of cross-sub-domain JavaScript calls with Ajax. To understand this better, follow these instructions by placing three simple files on appropriate domains. foo.ht ...

Ways to incorporate style links in Angular v2 components?

Currently, I am working through the Angular tutorial available on their website. In my quest to create various components, templates, and styles, I have hit a roadblock. The issue lies in incorporating my styles into the components using the 'styleUR ...

Customizing the default button in Ant Design Popconfirm to display "Cancel" instead

When the Ant Design Popconfirm modal is opened, the Confirm ("Yes") button is already preselected. https://i.stack.imgur.com/bs7W7.png The code for the modal is as follows: import { Popconfirm, message } from 'antd'; function confirm(e) { c ...

CSS animation keyframes failing to initialize

How can I fix my CSS @keyframe animation that is not starting as expected? http://jsfiddle.net/sadmicrowave/VKzXp/ @-webkit-keyframes slideNotificationsHide { 0%{ width:70%; } 100%{ width:94%; left:0; right:0; } } #left-container{ position:abosl ...

What is preventing HTML from triggering JavaScript when loaded inside a <div> with a script?

I'm working on creating a collapsible menu that I can easily customize on any page without the use of iframes. As someone new to web design, I have knowledge of CSS and HTML but I am currently learning JavaScript with limited experience in jQuery or A ...