mirror of
https://github.com/filamentphp/filament.git
synced 2026-10-28 03:44:19 +08:00
added global search result limit
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);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Tests\Admin\GlobalSearch;
|
||||
|
||||
use Filament\PluginServiceProvider;
|
||||
use Filament\Tests\Admin\Fixtures\Resources\PostResource;
|
||||
|
||||
class LimitedGlobalSearchServiceProvider extends PluginServiceProvider
|
||||
{
|
||||
public static string $name = 'limited-global-search';
|
||||
|
||||
protected function getResources(): array
|
||||
{
|
||||
return [
|
||||
LimitedPostResource::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class LimitedPostResource extends PostResource
|
||||
{
|
||||
protected static int $globalSearchResultsLimit = 1;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\GlobalSearch\Contracts\GlobalSearchProvider;
|
||||
use Filament\GlobalSearch\GlobalSearchResult;
|
||||
use Filament\GlobalSearch\GlobalSearchResults;
|
||||
use Filament\Http\Livewire\GlobalSearch;
|
||||
use Filament\Tests\Admin\GlobalSearch\LimitedTestCase;
|
||||
use Filament\Tests\Models\Post;
|
||||
use Illuminate\Database\Eloquent\Factories\Sequence;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
uses(LimitedTestCase::class);
|
||||
|
||||
it('can be mounted', function () {
|
||||
livewire(GlobalSearch::class)
|
||||
->assertOk();
|
||||
});
|
||||
|
||||
it('can retrieve search results', function () {
|
||||
$posts = Post::factory()->count(2)->state(new Sequence(
|
||||
['title' => 'post title 1'],
|
||||
['title' => 'post title 2']
|
||||
))->create();
|
||||
|
||||
livewire(GlobalSearch::class)
|
||||
->set('search', 'post title')
|
||||
->assertDispatchedBrowserEvent('open-global-search-results')
|
||||
->assertSee($posts[0]->title)
|
||||
->assertDontSee($posts[1]->title);
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Tests\Admin\GlobalSearch;
|
||||
|
||||
use Filament\Tests\Models\User;
|
||||
use Filament\Tests\TestCase as BaseTestCase;
|
||||
|
||||
class LimitedTestCase extends BaseTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->actingAs(User::factory()->create());
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app): array
|
||||
{
|
||||
return array_merge(parent::getPackageProviders($app), [
|
||||
LimitedGlobalSearchServiceProvider::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user