From 2c724d6a8f1ef1d8884cf8b06b64f1ab19c5ca1c Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Fri, 26 Apr 2024 20:11:43 +0300 Subject: [PATCH] Improve LfTags handling of parameters and not set fields --- src/Http/Livewire/LfTags.php | 13 +++++++++++-- tests/Feature/LfTagsTest.php | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Http/Livewire/LfTags.php b/src/Http/Livewire/LfTags.php index 9494f52..7c756cd 100644 --- a/src/Http/Livewire/LfTags.php +++ b/src/Http/Livewire/LfTags.php @@ -58,7 +58,9 @@ public function statamicFields(): Collection #[On('tags-updated')] public function updateTags($params) { - $this->params = collect($params)->reject(fn ($value, $key) => $key === 'sort'); + $this->params = collect($params) + ->reject(fn ($value, $key) => $key === 'sort') + ->reject(fn ($value, $key) => ! Str::contains($key, ':') && ! Str::startsWith($key, 'query_scope')); $this->tags = collect(); @@ -70,7 +72,6 @@ public function updateTags($params) public function parseConditions() { - $this->params->each(function ($value, $key) { [$field, $condition] = explode(':', $key); $values = collect(explode('|', $value)); @@ -135,6 +136,9 @@ public function handleQueryScopeCondition($values) public function addFieldOptionToTags($field, $value, $condition = null) { + if ($this->isNotTaggable($field)) { + return; + } $fieldLabel = $this->statamicFields->get($field)['display'] ?? $field; $optionLabel = $this->statamicFields->get($field)['options'][$value] ?? $value; $tag = [ @@ -154,6 +158,11 @@ public function removeOption($field, $value) $this->dispatch('clear-option', $tag); } + public function isNotTaggable($field) + { + return ! in_array($field, $this->fields); + } + public function render() { return view('statamic-livewire-filters::livewire.ui.'.$this->view); diff --git a/tests/Feature/LfTagsTest.php b/tests/Feature/LfTagsTest.php index 3c23328..2cd22c1 100644 --- a/tests/Feature/LfTagsTest.php +++ b/tests/Feature/LfTagsTest.php @@ -144,6 +144,14 @@ public function it_renders_the_tag_when_a_filter_is_updated() ->assertSee('Checkbox: Option 1'); } + /** @test */ + public function it_does_not_render_the_tag_when_a_filter_is_not_in_the_fields_array() + { + Livewire::test(LfTags::class, ['fields' => 'some_other_option', 'blueprint' => 'pages.pages']) + ->dispatch('tags-updated', ['item_options:is' => 'option1']) + ->assertDontSee('Checkbox: Option 1'); + } + /** @test */ public function it_dispatches_the_clear_option_event() {