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

548
Visualizações
Laravel livewire: Stop wire:click and wire:focus sending 2 actions

I have a form with an search input field as laravel livewire component. I want to search suggestions (other component) to be shown if the user clicks into the field or if the user tabs into the field. So I added wire:focus="handleFocus", which emits an event for the suggestions component to show. Works fine so far.

If the field has focus and the user clicks in the field again the suggestions should hide again. wire:focus does not trigger so I added wire:click="handleClick". So I ended up with

<input type="text" wire:model="searchTerm" wire:focus="handleFocus" wire:click="handleClick">

It works for the case that a user tabs into the field: suggestions appear, then clicks into the field: suggestions disappear. Good.

Problem: If a user clicks into the field, both event handlers wire:focus and wire:click are triggered so the field appears and immediately disappears again.

I already spent hours playing with debounce, prevent, adding timer to my component but without any success. In CSS there is :focus-visible which would solve the problem actually but I could not find something like for Javascript.

My stack also includes Javascript/AlpineJS if it helps finding a solution.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your issue resides in the fact that the click event triggers a focus. Lucky for you, Livewire has access to JS methods and data, therefore you can access the active element. If we combine the mousedown event with your click event, we can pass some data to determine if the handleClick should be processed:

<input type="text" wire:model="searchTerm" wire:focus="handleFocus" 
       wire:mousedown="handleMousedown(document.activeElement.getAttribute('id')"
       wire:click="handleClick($event.currentTarget.getAttribute('id'))">

Then, in your component:

public $activeElementId = null;

public function handleMousedown($activeElementId)
{
    $this->activeElementId = $activeElementId;
}

public function handleClick($inputId)
{
    if ($this->activeElementId == $inputId) {
        // Do Stuff
    }
}

This works because mousedown triggers before focusing the element. This will send the active element's ID to your component. If this is the same as your input, then you know that the input was already focused.

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