From 365ecdd4b2d267d4e4ec09ff17af27a1878640d4 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Thu, 25 Jan 2024 10:27:08 +0200 Subject: [PATCH] Add option to disable allowing filters if needed --- config/config.php | 4 +++ src/Http/Livewire/Traits/GenerateParams.php | 4 ++- .../LivewireCollectionComponentTest.php | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/config/config.php b/config/config.php index f25d5c0..21ac1f1 100644 --- a/config/config.php +++ b/config/config.php @@ -1,6 +1,10 @@ false, + // Only allow filters for fields and conditions already loaded in the page to be applied + 'only_allow_active_filters' => true, + ]; diff --git a/src/Http/Livewire/Traits/GenerateParams.php b/src/Http/Livewire/Traits/GenerateParams.php index a5989b3..e8786c0 100644 --- a/src/Http/Livewire/Traits/GenerateParams.php +++ b/src/Http/Livewire/Traits/GenerateParams.php @@ -9,7 +9,9 @@ trait GenerateParams { protected function generateParams() { - $params = $this->removeParamsNotInFiltersCollection(); + $params = config('statamic-livewire-filters.only_allow_active_filters') + ? $this->removeParamsNotInFiltersCollection() + : $this->params; return Parameters::make(array_merge( ['from' => $this->collections], diff --git a/tests/Feature/LivewireCollectionComponentTest.php b/tests/Feature/LivewireCollectionComponentTest.php index 47bcfbb..23cc1d6 100644 --- a/tests/Feature/LivewireCollectionComponentTest.php +++ b/tests/Feature/LivewireCollectionComponentTest.php @@ -4,6 +4,7 @@ use Facades\Reach\StatamicLivewireFilters\Tests\Factories\EntryFactory; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Config; use Livewire\Livewire; use Reach\StatamicLivewireFilters\Http\Livewire\LivewireCollection as LivewireCollectionComponent; use Reach\StatamicLivewireFilters\Tests\PreventSavingStacheItemsToDisk; @@ -226,6 +227,31 @@ public function check_that_filter_gets_ignored_if_not_registered() ->assertSee('Black Shirt'); } + /** @test */ + public function check_that_filter_gets_applied_if_check_is_disabled_in_config() + { + Config::set('statamic-livewire-filters.only_allow_active_filters', false); + + $params = [ + 'from' => 'clothes', + ]; + + Livewire::test(LivewireCollectionComponent::class, ['params' => $params]) + ->assertSet('collections', 'clothes') + ->dispatch('filter-updated', + field: 'colors', + condition: 'taxonomy', + payload: 'red', + command: 'add', + modifier: 'any', + ) + ->assertSet('params', [ + 'taxonomy:colors:any' => 'red', + ]) + ->assertSee('Red Shirt') + ->assertDontSee('Yellow Shirt'); + } + /** @test */ public function it_sets_query_scope_parameters() {