Skip to content

Commit

Permalink
Add clear method and support clear option
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Jan 25, 2025
1 parent e7774f8 commit 4696075
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Http/Livewire/LfDualRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function dispatchEvent()
$min = $this->selectedMin;
$max = $this->selectedMax;

// If we are back to the default values clear the filter
if ($min == $this->min && $max == $this->max) {
$this->clearFilters();

return;
}

if ($this->statamic_field['type'] === 'date') {
$min = Carbon::createFromDate($this->selectedMin)->startOfYear()->format('Y-m-d');
$max = Carbon::createFromDate($this->selectedMax)->endOfYear()->format('Y-m-d');
Expand Down Expand Up @@ -86,6 +93,41 @@ public function updatedSelectedMax($value)
$this->dispatchEvent();
}

public function clear(): void
{
$this->selectedMin = $this->min;
$this->selectedMax = $this->max;

$this->dispatch('dual-range-preset-values',
min: $this->selectedMin,
max: $this->selectedMax,
);

$this->clearFilters();
}

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

if ($tag['value'] == $this->selectedMin) {
$this->selectedMin = $this->min;
}

if ($tag['value'] == $this->selectedMax) {
$this->selectedMax = $this->max;
}

$this->dispatchEvent();
$this->dispatch('dual-range-preset-values',
min: $this->selectedMin,
max: $this->selectedMax,
);
}

#[On('preset-params')]
public function setPresetValues($params)
{
Expand Down
92 changes: 92 additions & 0 deletions tests/Feature/LfDualRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ public function it_dispatches_filter_updated_event_when_values_change()
);
}

#[Test]
public function it_can_be_reset_using_clear_method()
{
Livewire::test(LfDualRangeFilter::class, [
'field' => 'cabins',
'blueprint' => 'yachts.yachts',
'condition' => 'dual_range',
'min' => 2,
'max' => 10,
'minRange' => 2,
])
->set('selectedMin', 5)
->call('clear')
->assertSet('selectedMin', 2)
->assertDispatched('clear-filter',
field: 'cabins',
condition: 'dual_range',
)
->assertDispatched('dual-range-preset-values',
min: 2,
max: 10,
);
}

#[Test]
public function collection_component_handles_dual_range_filter_events()
{
Expand All @@ -175,6 +199,74 @@ public function collection_component_handles_dual_range_filter_events()
]);
}

#[Test]
public function it_sends_a_clear_filter_event_if_the_values_are_back_to_default()
{
Livewire::test(LfDualRangeFilter::class, [
'field' => 'cabins',
'blueprint' => 'yachts.yachts',
'condition' => 'dual_range',
'min' => 2,
'max' => 10,
'minRange' => 2,
])
->set('selectedMin', 5)
->assertDispatched('filter-updated',
field: 'cabins',
condition: 'dual_range',
payload: ['min' => 5, 'max' => 10],
)
->set('selectedMin', 2)
->assertDispatched('clear-filter',
field: 'cabins',
condition: 'dual_range',
);
}

#[Test]
public function it_clears_a_filter_using_the_clear_option_event()
{
Livewire::test(LfDualRangeFilter::class, [
'field' => 'cabins',
'blueprint' => 'yachts.yachts',
'condition' => 'dual_range',
'min' => 2,
'max' => 10,
'minRange' => 2,
])
->set('selectedMin', 5)
->assertDispatched('filter-updated',
field: 'cabins',
condition: 'dual_range',
payload: ['min' => 5, 'max' => 10],
)
->set('selectedMax', 8)
->assertDispatched('filter-updated',
field: 'cabins',
condition: 'dual_range',
payload: ['min' => 5, 'max' => 8],
)
->dispatch('clear-option', ['field' => 'cabins', 'value' => 5])
->assertSet('selectedMin', 2)
->assertSet('selectedMax', 8)
->assertDispatched('filter-updated',
field: 'cabins',
condition: 'dual_range',
payload: ['min' => 2, 'max' => 8],
)
->assertDispatched('dual-range-preset-values',
min: 2,
max: 8,
)
->dispatch('clear-option', ['field' => 'cabins', 'value' => 8])
->assertSet('selectedMin', 2)
->assertSet('selectedMax', 10)
->assertDispatched('clear-filter',
field: 'cabins',
condition: 'dual_range',
);
}

#[Test]
public function collection_component_handles_different_conditions_by_modifier()
{
Expand Down

0 comments on commit 4696075

Please sign in to comment.