Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
onFocus and onBlur events for multiple elements

I'm trying to handle the onFocus and onBlur events for 2 elements - the input and the textarea elements. I even tried to implement it as one state with the object but it's just not possible, so I separated it into two states. Whenever I try to focus on the textarea, it'll expand, however, if I start focusing on the input element, it'll collapse instead of staying expanded. How do I implement this?

  const [titleFocused, setTitleFocus] = useState(null);
  const [contentFocused, setContentFocus] = useState(null);
  
  function handleFocus(event) {
    const {name} = event.target;

    setTitleFocus(name === 'title' && true);
    setContentFocus(name === 'content' && true);
  }

  function handleUnfocus(event) {
    const {name} = event.target;

    setTitleFocus(name === 'title' && false);
    setContentFocus(name === 'content' && false);
  }

  return (
    <div >
      <form 
          className="create-note">
        {titleFocused || contentFocused && (
          <input
          name="title"
          onChange={handleChange}
          value={note.title}
          placeholder="Title"
          onFocus={handleFocus}
          onBlur={handleUnfocus}
          />)
        }
        <textarea
          name="content"
          onChange={handleChange}
          value={note.content}
          placeholder="Take a note..."
          onFocus={handleFocus}
          onBlur={handleUnfocus}
          rows={titleFocused || contentFocused ? "3" : "1"}
        />
        <Zoom in={titleFocused || contentFocused} appear={true}>
          <Fab onClick={submitNote}>
            <AddIcon fontSize="large" />
          </Fab>
        </Zoom>
      </form>
    </div>
  );
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Probably caused by event handles toggling both states even when the event is not concerning the component.

This should work

function handleFocus(event) {
    const {name} = event.target;
    name === 'title' && setTitleFocus(true);
    name === 'content' && setContentFocus(true);
 }

 function handleUnfocus(event) {
    const {name} = event.target;
    name === 'title' && setTitleFocus(false);
    name === 'content' && setContentFocus( false);
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda