Skip to content

Commit

Permalink
Handle preset values
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Nov 5, 2024
1 parent 74663f0 commit bc91ad1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
7 changes: 6 additions & 1 deletion resources/views/livewire/filters/lf-dual-range.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
$wire.set('selectedMin', values[0]);
$wire.set('selectedMax', values[1]);
});
// Handle preset values
$wire.on('dual-range-preset-values', (presetValues) => {
slider.noUiSlider.set([presetValues.min, presetValues.max], false);
});
}
}"

Expand All @@ -58,4 +63,4 @@ class="w-full my-8"
</div>
</div>
</div>
</div>
</div>
20 changes: 9 additions & 11 deletions src/Http/Livewire/LfDualRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,20 @@ public function updatedSelectedMax($value)
$this->dispatchEvent();
}

// #[On('livewire:initialized')]
// public function livewireComponentReady()
// {
// $this->dispatchEvent();
// }

#[On('preset-params')]
public function setPresetSort($params)
{
$paramKey = $this->getParamKey();
if (isset($params[$paramKey]['min'])) {
$this->selectedMin = $params[$paramKey]['min'];
$paramKeys = $this->getParamKey();
if (isset($params[$paramKeys['min']])) {
$this->selectedMin = $params[$paramKeys['min']];
}
if (isset($params[$paramKey]['max'])) {
$this->selectedMax = $params[$paramKey]['max'];
if (isset($params[$paramKeys['max']])) {
$this->selectedMax = $params[$paramKeys['max']];
}
$this->dispatch('dual-range-preset-values',
min: $this->selectedMin,
max: $this->selectedMax,
);
}

public function render()
Expand Down
14 changes: 14 additions & 0 deletions src/Http/Livewire/Traits/IsLivewireFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ protected function getParamKey()
return $this->modifier.':'.$this->field;
}

if ($this->condition === 'dual_range') {
$minModifier = 'gte';
$maxModifier = 'lte';

if ($this->modifier !== 'any') {
[$minModifier, $maxModifier] = explode('|', $this->modifier);
}

return [
'min' => $this->field.':'.$minModifier,
'max' => $this->field.':'.$maxModifier,
];
}

return $this->field.':'.$this->condition;
}
}
21 changes: 20 additions & 1 deletion tests/Feature/LfDualRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,30 @@ public function it_loads_preset_params_correctly()
'max' => 10,
'minRange' => 2,
])
->dispatch('preset-params', ['cabins:lte' => 5, 'cabins:gte' => 8])
->dispatch('preset-params', ['cabins:gte' => 5, 'cabins:lte' => 8])
->assertDispatched('dual-range-preset-values', min: 5, max: 8)
->assertSet('selectedMin', 5)
->assertSet('selectedMax', 8);
}

#[Test]
public function it_loads_preset_params_with_custom_modifier_correctly()
{
Livewire::test(LfDualRangeFilter::class, [
'field' => 'cabins',
'blueprint' => 'yachts.yachts',
'condition' => 'dual_range',
'min' => 2,
'max' => 10,
'minRange' => 2,
'modifier' => 'gt|lt',
])
->dispatch('preset-params', ['cabins:gt' => 5])
->assertDispatched('dual-range-preset-values', min: 5, max: 10)
->assertSet('selectedMin', 5)
->assertSet('selectedMax', 10);
}

protected function makeEntry($collection, $slug)
{
return EntryFactory::id($slug)->collection($collection)->slug($slug)->make();
Expand Down

0 comments on commit bc91ad1

Please sign in to comment.