Skip to content

Commit

Permalink
Handle dates in DualRangeFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Nov 5, 2024
1 parent af3925b commit b3e0b69
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
8 changes: 4 additions & 4 deletions resources/views/livewire/filters/lf-dual-range.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
},
format: {
to: (value) => {
return this.format === 'date'
? new Date(value).getFullYear()
return this.format === 'float'
? value
: Math.round(value);
},
from: (value) => {
return this.format === 'date'
? new Date(value, 0).getTime()
return this.format === 'float'
? value
: parseFloat(value);
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/Http/Livewire/LfDualRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Reach\StatamicLivewireFilters\Http\Livewire;

use Carbon\Carbon;
use Livewire\Attributes\Locked;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
Expand Down Expand Up @@ -33,7 +34,7 @@ class LfDualRangeFilter extends Component
public $minRange = 1;

#[Locked]
public $format = 'number';
public $format = 'integer';

public function mount()
{
Expand All @@ -44,12 +45,21 @@ public function mount()

public function dispatchEvent()
{
$min = $this->selectedMin;
$max = $this->selectedMax;

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');
$this->modifier = 'is_after|is_before';
}

$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
payload: [
'min' => $this->selectedMin,
'max' => $this->selectedMax,
'min' => $min,
'max' => $max,
],
command: 'replace',
modifier: $this->modifier,
Expand All @@ -63,6 +73,7 @@ public function updatedSelectedMin($value)
if ($value > $this->selectedMax - $this->minRange) {
$this->selectedMin = $this->selectedMax - $this->minRange;
}

$this->dispatchEvent();
}

Expand All @@ -72,6 +83,7 @@ public function updatedSelectedMax($value)
if ($value < $this->selectedMin + $this->minRange) {
$this->selectedMax = $this->selectedMin + $this->minRange;
}

$this->dispatchEvent();
}

Expand Down
46 changes: 42 additions & 4 deletions tests/Feature/LfDualRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function setUp(): void
[
'handle' => 'year',
'field' => [
'type' => 'text',
'type' => 'date',
'display' => 'Year',
'listable' => 'hidden',
],
Expand All @@ -57,9 +57,9 @@ public function setUp(): void
]);
$this->blueprint->setHandle('yachts')->setNamespace('collections.'.$this->collection->handle())->save();

$this->makeEntry($this->collection, 'yacht-a')->set('title', 'Luxury Yacht A')->set('cabins', 4)->set('year', 2020)->save();
$this->makeEntry($this->collection, 'yacht-b')->set('title', 'Luxury Yacht B')->set('cabins', 6)->set('year', 2022)->save();
$this->makeEntry($this->collection, 'yacht-c')->set('title', 'Luxury Yacht C')->set('cabins', 8)->set('year', 2024)->save();
$this->makeEntry($this->collection, 'yacht-a')->set('title', 'Luxury Yacht A')->set('cabins', 4)->set('year', '2020-05-22')->save();
$this->makeEntry($this->collection, 'yacht-b')->set('title', 'Luxury Yacht B')->set('cabins', 6)->set('year', '2022-02-02')->save();
$this->makeEntry($this->collection, 'yacht-c')->set('title', 'Luxury Yacht C')->set('cabins', 8)->set('year', '2024-12-30')->save();
}

#[Test]
Expand Down Expand Up @@ -195,6 +195,44 @@ public function collection_component_handles_different_conditions_by_modifier()
]);
}

#[Test]
public function it_can_handle_date_field_and_dispatch_correct_event()
{
Livewire::test(LfDualRangeFilter::class, [
'field' => 'year',
'blueprint' => 'yachts.yachts',
'condition' => 'dual_range',
'min' => 2018,
'max' => 2024,
'minRange' => 1,
])
->set('selectedMin', 2020)
->assertDispatched('filter-updated',
field: 'year',
condition: 'dual_range',
payload: ['min' => '2020-01-01', 'max' => '2024-12-31'],
command: 'replace',
modifier: 'is_after|is_before',
);
}

#[Test]
public function collection_component_handles_date_field()
{
Livewire::test(LivewireCollection::class, ['params' => ['from' => 'yachts']])
->dispatch('filter-updated',
field: 'year',
condition: 'dual_range',
payload: ['min' => '2020-01-01', 'max' => '2024-12-31'],
command: 'replace',
modifier: 'is_after|is_before',
)
->assertSet('params', [
'year:is_after' => '2020-01-01',
'year:is_before' => '2024-12-31',
]);
}

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

0 comments on commit b3e0b69

Please sign in to comment.