Closing a Bootstrap 5 Dropdown in React without using toggle feature

Currently, I am using a search field in my application that displays data as dropdown items when the user is typing. The library being utilized is React Bootstrap (bootstrap 5) and it's functioning perfectly. However, an issue arises where the dropdown persists even after clicking outside of it or navigating to a new link. This dropdown is positioned in a header component that does not re-render with the rest of the page due to the usage of NextJS. Does anyone have any suggestions on how to close a dropdown that lacks a toggle function?


    <form className="d-none d-sm-inline-block" style={{ zIndex: "1000" }}>
      <div className="input-group input-group-navbar">
        <input type="text" className="form-control" placeholder="Search" aria-label="Search" onChange={(event) => { setSearchTerm(event.target.value); }} />
        
        <button className="btn" type="button">
          <Icon.Search className="align-middle" />
        </button>
      </div>
      
      {searchData.length >= 1 && (
        <Dropdown style={{ position: "absolute", background: "white" }} autoClose="outside">
          {searchData.slice(0, 10).map((element, index) => {
            return (
              <Link key={element.agressoResourceId} href={`employees/69918`} passHref replace={true}>
                <Dropdown.Item>
                  {element.firstname} - {element.lastname}
                </Dropdown.Item>
              </Link>
            );
          })}
        </Dropdown>
      )}
    
    </form>
  

Answer №1

To resolve the issue, I utilized the onBlur event to conceal the dropdown menu. However, a complication arose when the onblur function was activating prior to selecting an item. The workaround involved implementing a setTimeout delay of 200 milliseconds.

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

Looking for a feature where users can easily update their profile using an interactive edit button

I'm currently working on a website project and one of the features I'm tackling is the user's profile page. My objective is to create an edit button within the page that allows the user to modify their name, username, email, and update their ...

Leveraging the power of Devextreme React Scheduler for real-time scheduling modifications (react)

Utilizing the powerful DevExtreme React Scheduler package, I am in the process of creating a dynamic and customizable calendar system. Check out the live demo here: https://codesandbox.io/s/8kqq12o000?fontsize=14 My current challenge involves changing th ...

How can I turn off autocomplete in MUI textfields?

Currently, I am working with the latest version of mui. Within my user contact info form, there is a zip code field that I do not want to be auto completed if the value is null. However, despite my efforts, it continues to autocomplete with the email saved ...

What could be the reason for encountering a Typescript ts(2345) error while trying to pass a mocked constant to .mockResolvedValue()?

Within my test.tsx file, I have the following code snippet: test('Photos will load', async () => { const mockCuratedPhotos = jest.spyOn(endpoints, 'getCuratedPhotos'); mockCuratedPhotos.mockResolvedValue(mockPhotos); awa ...

"Enhancing User Experience: Implementing Internationalization and Nested Layouts in Next.js 13.x

In the midst of working on a cutting-edge Next.js 13 project that utilizes the new /app folder routing, I am delving into the realm of setting up internationalization. Within my project's structure, it is organized as follows: https://i.stack.imgur.c ...

Creating a dynamic state management system for multiple Collapse components can be achieved by utilizing

I am trying to create a Collapse menu from array data, Currently, when I click on any menu all sub menus expand I believe my issue lies in not being able to set a unique "open" state for each Main menu I want to avoid assigning a "state" to accommodate ...

The React Testing Library encountered an error: TypeError - actImplementation function not found

Encountering a TypeError: actImplementation is not a function error while testing out this component import React from 'react'; import { StyledHeaderContainer, StyledTitle } from './styled'; export const Header = () => { return ( ...

Guide to detecting Axios API call error 401 in ReactJS

When using axios to make API calls in React, if a token is not provided or the token has expired, the server will send a 401 status. I need to check this status on the React side. The issue is that when I check the error object in the catch block, the sta ...

Stylish hover effects displayed on disabled button using Styled Components

I am currently working on a button using Styled Components. However, even when the button is in a disabled state (:disabled), it still exhibits hover behavior that should not be present. I am wondering if there is a proper way to prevent hover effects when ...

Issue with Material UI TextField and Redux Form integration

Having trouble with a textfield that I am trying to integrate with Redux Form. Here is how I have set it up: const renderTextField = props => ( <TextField {...props} /> ); Usage example: <Field id="searchCif" name="sear ...

The dropdown menu for `react-google-autocomplete` does not appear when used with an MUI

Currently, I am attempting to create an Address form dialog using the Google Places API. The issue arises when I utilize the react-google-autocomplete's usePlacesWidget along with MUI TextField in a Material UI Dialog. The dropdown menu for Google Pl ...

Troubleshooting the lack of functioning redirects in a Next.js application hosted on Digital Ocean, requiring the addition of a .html suffix to

Are there any methods for deploying a Next.js static site on Digital Ocean and ensuring it functions properly? I managed to deploy an app, but each page must be accessed with the .html suffix, and redirects specified in next.js.config don't work as e ...

method for sorting labels in Select element in ReactJS

Hey there, I'm facing an issue with the code snippet available here. I would really appreciate it if you could assist me in resolving this problem. This is the code: import React from "react"; import { Select } from "antd" ...

Ways to troubleshoot errors that arise while building Next.js with Mui framework

After running npm install @mui/material @emotion/react @emotion/server, I encountered an error while trying to build my Next.js app during linting and type checking. info - Linting and checking validity of types ...Failed to compile. ./node_modules/@mui/ ...

Updates to props values are not being reflected in the React js application running on the webpack

I keep facing an issue where I have to restart the webpack server every time I try to pass or update props values from parent to child components. It's frustrating that the props values are not updating even after saving the file. Take a look at my p ...

Tips for designing unique 404 error pages with NextJS

Is there a way to create a custom layout for my 404 error page that is different from the main layout? I'm facing an issue where components like the footer or navigation buttons from the main layout are appearing on my custom 404 layout. Here's ...

Unable to successfully log out from next-auth using the Keycloak provider

I am currently using next-auth with my Next.js application to handle authentication. Here is how I have configured it: .... export default NextAuth({ // Configure one or more authentication providers providers: [ KeycloakProvider({ id: ' ...

Browserify pulls in entire module even if only specific parts are utilized, such as react-addons

I am currently using Browserify to bundle my server-side react.js code for the client. There is a concern that utilizing a module from an npm package may result in the entire package being bundled by Browserify. Question: Will require('react-addons& ...

MUI's Checkbox with Radio Button feature functionality

In my project, I am looking to implement a Checkbox with radio button behavior inside an Accordion component. The challenge here is that only one item should be selected at a time. If one item is checked, it should automatically uncheck the previously sele ...

Is the behavior of next/router determined by the specific Dom element that is clicked?

I've been working on implementing a dark mode/light mode hook and system for my platform using Next.js and React. Recently, I encountered a strange issue where clicking certain types of DOM elements causes a light mode flicker while others do not. Eve ...