-
-
Save wmandai/e22a9c235b890ec439cba824d5725378 to your computer and use it in GitHub Desktop.
Building a search drop down component with Laravel Livewire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
use App\Contact; | |
use Livewire\Component; | |
class ContactSearchBar extends Component | |
{ | |
public $query; | |
public $contacts; | |
public $highlightIndex; | |
public function mount() | |
{ | |
$this->reset(); | |
} | |
public function reset() | |
{ | |
$this->query = ''; | |
$this->contacts = []; | |
$this->highlightIndex = 0; | |
} | |
public function incrementHighlight() | |
{ | |
if ($this->highlightIndex === count($this->contacts) - 1) { | |
$this->highlightIndex = 0; | |
return; | |
} | |
$this->highlightIndex++; | |
} | |
public function decrementHighlight() | |
{ | |
if ($this->highlightIndex === 0) { | |
$this->highlightIndex = count($this->contacts) - 1; | |
return; | |
} | |
$this->highlightIndex--; | |
} | |
public function selectContact() | |
{ | |
$contact = $this->contacts[$this->highlightIndex] ?? null; | |
if ($contact) { | |
$this->redirect(route('show-contact', $contact['id'])); | |
} | |
} | |
public function updatedQuery() | |
{ | |
$this->contacts = Contact::where('name', 'like', '%' . $this->query . '%') | |
->get() | |
->toArray(); | |
} | |
public function render() | |
{ | |
return view('livewire.contact-search-bar'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="relative"> | |
<input | |
type="text" | |
class="form-input" | |
placeholder="Search Contacts..." | |
wire:model="query" | |
wire:keydown.escape="reset" | |
wire:keydown.tab="reset" | |
wire:keydown.ArrowUp="decrementHighlight" | |
wire:keydown.ArrowDown="incrementHighlight" | |
wire:keydown.enter="selectContact" | |
/> | |
<div wire:loading class="absolute z-10 list-group bg-white w-full rounded-t-none shadow-lg"> | |
<div class="list-item">Searching...</div> | |
</div> | |
@if(!empty($query)) | |
<div class="fixed top-0 right-0 bottom-0 left-0" wire:click="reset"></div> | |
<div class="absolute z-10 list-group bg-white w-full rounded-t-none shadow-lg"> | |
@if(!empty($contacts)) | |
@foreach($contacts as $i => $contact) | |
<a | |
href="{{ route('show-contact', $contact['id']) }}" | |
class="list-item {{ $highlightIndex === $i ? 'highlight' : '' }}" | |
>{{ $contact['name'] }}</a> | |
@endforeach | |
@else | |
<div class="list-item">No results!</div> | |
@endif | |
</div> | |
@endif | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment