Skip to content

Commit

Permalink
Add clear method and clear option support to LfRangeFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Jan 25, 2025
1 parent 4696075 commit b780694
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Http/Livewire/LfRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function mount($default = null)

public function dispatchEvent()
{
// Clear the filter if we are back to default
if ($this->selected === ($this->default ?? $this->min)) {
$this->clearFilters();

return;
}

$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
Expand All @@ -54,6 +61,24 @@ public function setPresetValues($params)
}
}

public function clear(): void
{
$this->selected = $this->default ?? $this->min;

$this->clearFilters();
}

#[On('clear-option')]
public function clearOption($tag)
{
if ($tag['field'] !== $this->field) {
return;
}

$this->selected = $this->default ?? $this->min;
$this->dispatchEvent();
}

public function render()
{
return view('statamic-livewire-filters::livewire.filters.'.$this->view);
Expand Down
50 changes: 50 additions & 0 deletions tests/Feature/LfRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,56 @@ public function it_changes_the_value_of_selected_property_when_slider_changes()
);
}

/** @test */
public function it_can_be_reset_using_the_clear_method()
{
Livewire::test(LfRangeFilter::class, [
'field' => 'max_items',
'blueprint' => 'pages.pages',
'condition' => 'gte',
'min' => 1,
'max' => 4,
'default' => 2,
])
->set('selected', 3)
->assertDispatched('filter-updated',
field: 'max_items',
condition: 'gte',
payload: 3,
)
->call('clear')
->assertSet('selected', 2)
->assertDispatched('clear-filter',
field: 'max_items',
condition: 'gte',
);
}

/** @test */
public function it_can_be_reset_using_the_clear_option_event()
{
Livewire::test(LfRangeFilter::class, [
'field' => 'max_items',
'blueprint' => 'pages.pages',
'condition' => 'gte',
'min' => 1,
'max' => 4,
'default' => 2,
])
->set('selected', 3)
->assertDispatched('filter-updated',
field: 'max_items',
condition: 'gte',
payload: 3,
)
->dispatch('clear-option', ['field' => 'max_items', 'value' => 3])
->assertSet('selected', 2)
->assertDispatched('clear-filter',
field: 'max_items',
condition: 'gte',
);
}

/** @test */
public function it_loads_a_param_that_is_preset()
{
Expand Down

0 comments on commit b780694

Please sign in to comment.