mirror of
https://github.com/filamentphp/filament.git
synced 2026-10-28 03:44:19 +08:00
Merge pull request #4283 from cameroncox/add-global-search-result-limit
feature: configure global search results limit in a resource
This commit is contained in:
@@ -57,6 +57,8 @@ class Resource
|
||||
|
||||
protected static string | array $middlewares = [];
|
||||
|
||||
protected static int $globalSearchResultsLimit = 50;
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form;
|
||||
@@ -227,6 +229,11 @@ class Resource
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getGlobalSearchResultsLimit(): int
|
||||
{
|
||||
return static::$globalSearchResultsLimit;
|
||||
}
|
||||
|
||||
public static function getGlobalSearchResults(string $searchQuery): Collection
|
||||
{
|
||||
$query = static::getGlobalSearchEloquentQuery();
|
||||
@@ -242,7 +249,7 @@ class Resource
|
||||
}
|
||||
|
||||
return $query
|
||||
->limit(50)
|
||||
->limit(static::getGlobalSearchResultsLimit())
|
||||
->get()
|
||||
->map(function (Model $record): ?GlobalSearchResult {
|
||||
$url = static::getGlobalSearchResultUrl($record);
|
||||
|
||||
@@ -21,6 +21,8 @@ class PostResource extends Resource
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'title';
|
||||
|
||||
protected static int $globalSearchResultsLimit = 3;
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
|
||||
@@ -5,8 +5,11 @@ use Filament\GlobalSearch\Contracts\GlobalSearchProvider;
|
||||
use Filament\GlobalSearch\GlobalSearchResult;
|
||||
use Filament\GlobalSearch\GlobalSearchResults;
|
||||
use Filament\Http\Livewire\GlobalSearch;
|
||||
use Filament\Tests\Admin\Fixtures\Resources\PostResource;
|
||||
use Filament\Tests\Admin\GlobalSearch\TestCase;
|
||||
use Filament\Tests\Models\Post;
|
||||
use Illuminate\Database\Eloquent\Factories\Sequence;
|
||||
use Illuminate\Support\Str;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
uses(TestCase::class);
|
||||
@@ -25,6 +28,28 @@ it('can retrieve search results', function () {
|
||||
->assertSee($post->title);
|
||||
});
|
||||
|
||||
it('can retrieve limited search results', function () {
|
||||
$title = Str::random();
|
||||
|
||||
$posts = Post::factory()
|
||||
->count(4)
|
||||
->state(new Sequence(
|
||||
['title' => "{$title} 0"],
|
||||
['title' => "{$title} 1"],
|
||||
['title' => "{$title} 2"],
|
||||
['title' => "{$title} 3"],
|
||||
))
|
||||
->create();
|
||||
|
||||
livewire(GlobalSearch::class)
|
||||
->set('search', $title)
|
||||
->assertDispatchedBrowserEvent('open-global-search-results')
|
||||
->assertSee($posts[0]->title)
|
||||
->assertSee($posts[1]->title)
|
||||
->assertSee($posts[2]->title)
|
||||
->assertDontSee($posts[3]->title);
|
||||
});
|
||||
|
||||
it('can retrieve results via custom search provider', function () {
|
||||
Filament::globalSearchProvider(CustomSearchProvider::class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user