Files
2026-10-31 18:54:18 +02:00

99 lines
2.9 KiB
PHP

<?php
use App\Models\Post;
use Livewire\Attributes\Layout;
use Livewire\Volt\Component;
use Livewire\WithPagination;
new #[Layout('components.layouts.public')] class extends Component
{
use WithPagination;
public string $search = '';
public function updatedSearch(): void
{
$this->resetPage();
}
public function clearSearch(): void
{
$this->search = '';
$this->resetPage();
}
public function highlightSearch(string $text, string $search): string
{
if (empty($search)) {
return e($text);
}
$escapedText = e($text);
$escapedSearch = e($search);
return preg_replace(
'/('.preg_quote($escapedSearch, '/').')/iu',
'<mark class="bg-cta/30 rounded px-1">$1</mark>',
$escapedText
);
}
public function with(): array
{
return [
'posts' => Post::published()
->when($this->search, function ($query) {
$search = $this->search;
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('body', 'like', "%{$search}%");
});
})
->latest()
->paginate(10),
];
}
}; ?>
<section class="sec" style="padding-top:56px">
<div class="wrap">
<x-site.section-head tag="h1" :label="__('site.hero_eyebrow')" :title="__('site.nav_articles')">
<div style="position:relative">
<label class="sr-only" for="posts-search">{{ __('posts.search_placeholder') }}</label>
<input
id="posts-search"
type="search"
class="search"
wire:model.live.debounce.300ms="search"
placeholder="{{ __('posts.search_placeholder') }}"
>
</div>
</x-site.section-head>
@if($search)
<p class="search-info">
@if($posts->total() > 0)
{{ __('posts.search_results', ['count' => $posts->total(), 'query' => $search]) }}
@else
{{ __('posts.no_results', ['query' => $search]) }}
@endif
<button type="button" class="btn btn-ghost" wire:click="clearSearch">{{ __('posts.clear_search') }}</button>
</p>
@endif
@if($posts->count() > 0)
<div class="posts posts-index">
@foreach($posts as $post)
<x-site.post-card :post="$post" wire:key="post-{{ $post->id }}" />
@endforeach
</div>
@elseif(! $search)
<p class="text-center" style="color:var(--muted)">{{ __('posts.no_posts') }}</p>
@endif
<div class="pagination-wrap">
{{ $posts->links() }}
</div>
</div>
</section>