Skip to content

Commit

Permalink
Improve LfTags handling of parameters and not set fields
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Apr 26, 2024
1 parent d426ce6 commit 2c724d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Http/Livewire/LfTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
Expand Down Expand Up @@ -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 = [
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/LfTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 2c724d6

Please sign in to comment.