Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
How to prevent onBlur for a sibling or related component events?

I am implementing a complex autocomplete functionality, and the point is that when the input gets focused, I should show the result box, and when the user goes to the next input or clicks anywhere on the screen, or presses Escape, then I should close the result box.

To close the result box, I'm using the onBlur event.

The problem is that inside the result box I have a button that takes the user to a more complete search dialog, but when I click it, the onBlur event is fired and prevents the click of this button to happen.

Here is my code:

import { useState } from "react";

export default function IndexPage() {

  const [isOpen, setIsOpen] = useState(false);

  const [text, setText] = useState("");

  return (
    <div>
      <input 
          onBlur={() => setIsOpen(false)} 
          onFocus={() => setIsOpen(true)} 
      />

      {isOpen && (
        <div>
          something
          <br />
          <button 
              onClick={() => setText("i am clicked")}>click me</button>
        </div>
      )}
      <br />
      <input />
      <br />
      <p>{text}</p>
    </div>
  );
}

How can I make it work properly?

You can see a live example in this codesandbox

I'm using Next.js

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Firstly, you need to group input and button to have a wider focusing area. I'm using div for that purpose, but onBlur and onFocus are not applied for a usual div, so we need to have tabIndex="1" which is to make that element interactive with those events.

<div
  tabIndex="1"
  onBlur={() => setIsOpen(false)}
  onFocus={() => setIsOpen(true)}
>
</div>

Secondly, we should use onMouseDown event instead of onClick, which is to avoid focusing state on the button element.

<button onMouseDown={() => setText("i am clicked")}>
    click me
</button>

The full implementation can be (https://codesandbox.io/s/strange-violet-z0u3jr)

import { useState } from "react";
export default function IndexPage() {
  const [isOpen, setIsOpen] = useState(false);
  const [text, setText] = useState("");
  return (
    <div>
      <div
        tabIndex="1"
        onBlur={() => setIsOpen(false)}
        onFocus={() => setIsOpen(true)}
      >
        <input />
        {isOpen && (
          <div>
            something
            <br />
            <button onMouseDown={() => setText("i am clicked")}>
              click me
            </button>
          </div>
        )}
      </div>
      <br />
      <input />
      <br />
      <p>{text}</p>
    </div>
  );
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda