From bc91ad112b990a3d06c0422653bb7263aaf256f4 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Tue, 5 Nov 2024 11:29:46 +0200 Subject: [PATCH] Handle preset values --- .../livewire/filters/lf-dual-range.blade.php | 7 ++++++- src/Http/Livewire/LfDualRangeFilter.php | 20 ++++++++---------- src/Http/Livewire/Traits/IsLivewireFilter.php | 14 +++++++++++++ tests/Feature/LfDualRangeFilterTest.php | 21 ++++++++++++++++++- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/resources/views/livewire/filters/lf-dual-range.blade.php b/resources/views/livewire/filters/lf-dual-range.blade.php index 25a73a1..5d45dea 100644 --- a/resources/views/livewire/filters/lf-dual-range.blade.php +++ b/resources/views/livewire/filters/lf-dual-range.blade.php @@ -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); + }); } }" @@ -58,4 +63,4 @@ class="w-full my-8" - \ No newline at end of file + diff --git a/src/Http/Livewire/LfDualRangeFilter.php b/src/Http/Livewire/LfDualRangeFilter.php index dabb184..1c01107 100644 --- a/src/Http/Livewire/LfDualRangeFilter.php +++ b/src/Http/Livewire/LfDualRangeFilter.php @@ -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() diff --git a/src/Http/Livewire/Traits/IsLivewireFilter.php b/src/Http/Livewire/Traits/IsLivewireFilter.php index 89b3695..4d04175 100644 --- a/src/Http/Livewire/Traits/IsLivewireFilter.php +++ b/src/Http/Livewire/Traits/IsLivewireFilter.php @@ -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; } } diff --git a/tests/Feature/LfDualRangeFilterTest.php b/tests/Feature/LfDualRangeFilterTest.php index 71befd8..d72e2d2 100644 --- a/tests/Feature/LfDualRangeFilterTest.php +++ b/tests/Feature/LfDualRangeFilterTest.php @@ -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();