Skip to content

Commit

Permalink
Only show the global search if any column is searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonrietdijk committed Aug 25, 2024
1 parent 461c6ef commit 0bd03c1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 1 addition & 7 deletions resources/views/bar/bar.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<div class="flex flex-col gap-3">
<div class="flex flex-col lg:flex-row gap-3">
<div class="flex gap-3">
<input type="search"
placeholder="@lang('Search all columns...')"
class="w-full md:w-64 border border-neutral-200 shadow-sm rounded-md outline-none focus:border-blue-300 px-3 py-2 bg-white text-black transition ease-in-out dark:bg-neutral-800 dark:border-neutral-700 dark:focus:border-blue-600 dark:text-white"
wire:model.live.debounce.500ms="globalSearch">
@includeWhen($this->canClearSearch(), 'livewire-table::bar.buttons.clear-search')
</div>
@includeWhen($this->canSearch(), 'livewire-table::bar.search')
<div class="justify-center items-center w-full border-y border-transparent" wire:loading.flex>
<span class="inline-block border border-4 border-blue-500 border-r-transparent motion-safe:animate-spin rounded-full my-2 p-2"></span>
</div>
Expand Down
7 changes: 7 additions & 0 deletions resources/views/bar/search.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="flex gap-3">
<input type="search"
placeholder="@lang('Search all columns...')"
class="w-full md:w-64 border border-neutral-200 shadow-sm rounded-md outline-none focus:border-blue-300 px-3 py-2 bg-white text-black transition ease-in-out dark:bg-neutral-800 dark:border-neutral-700 dark:focus:border-blue-600 dark:text-white"
wire:model.live.debounce.500ms="globalSearch">
@includeWhen($this->canClearSearch(), 'livewire-table::bar.buttons.clear-search')
</div>
5 changes: 5 additions & 0 deletions src/Concerns/HasSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function updatedSearch(mixed $value, ?string $key): void
$this->resetPage();
}

protected function canSearch(): bool
{
return $this->resolveColumns()->filter(fn (BaseColumn $column): bool => $column->isSearchable())->isNotEmpty();
}

public function clearSearch(): void
{
$this->globalSearch = '';
Expand Down

0 comments on commit 0bd03c1

Please sign in to comment.