-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
'default' => 'Default', | ||
'all' => 'All', | ||
'value' => 'Value', | ||
'to' => 'to', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<div> | ||
<div | ||
x-data="dualRange({ | ||
initialMin: @entangle('selectedMin'), | ||
initialMax: @entangle('selectedMax'), | ||
step: {{ $step }}, | ||
min: {{ $min }}, | ||
max: {{ $max }}, | ||
format: '{{ $format }}', | ||
minRange: {{ $minRange }} | ||
})" | ||
class="w-full my-8" | ||
> | ||
<div class="relative"> | ||
<div x-ref="slider" class="w-[93%] mx-auto"></div> | ||
<div class="flex justify-center mt-3"> | ||
<span class="font-bold" x-text="min"></span> | ||
<span class="mx-2">{{ __('statamic-livewire-filters::ui.to') }}</span> | ||
<span class="font-bold" x-text="max"></span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
@script | ||
<script> | ||
Alpine.data('dualRange', ({ initialMin, initialMax, step, min, max, format, minRange }) => ({ | ||
min: initialMin, | ||
max: initialMax, | ||
init() { | ||
const slider = this.$refs.slider; | ||
console.log(window); | ||
window.noUiSlider.create(slider, { | ||
start: [this.min, this.max], | ||
connect: true, | ||
step: step, | ||
range: { | ||
'min': min, | ||
'max': max | ||
}, | ||
format: { | ||
to: (value) => { | ||
return format === 'date' | ||
? new Date(value).getFullYear() | ||
: Math.round(value); | ||
}, | ||
from: (value) => { | ||
return format === 'date' | ||
? new Date(value, 0).getTime() | ||
: parseFloat(value); | ||
} | ||
} | ||
}); | ||
slider.noUiSlider.on('update', (values) => { | ||
this.min = values[0]; | ||
this.max = values[1]; | ||
}); | ||
slider.noUiSlider.on('slide', (values, handle) => { | ||
if (handle === 0 && (values[1] - values[0]) < minRange) { | ||
slider.noUiSlider.set([values[1] - minRange, values[1]]); | ||
} | ||
if (handle === 1 && (values[1] - values[0]) < minRange) { | ||
slider.noUiSlider.set([values[0], values[0] + minRange]); | ||
} | ||
}); | ||
} | ||
})) | ||
</script> | ||
@endscript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Reach\StatamicLivewireFilters\Http\Livewire; | ||
|
||
use Livewire\Attributes\On; | ||
use Livewire\Attributes\Validate; | ||
use Livewire\Component; | ||
|
||
class LfDualRangeFilter extends Component | ||
{ | ||
use Traits\IsLivewireFilter; | ||
|
||
public $view = 'lf-dual-range'; | ||
|
||
public $selectedMin; | ||
|
||
public $selectedMax; | ||
|
||
#[Validate('required')] | ||
public $min; | ||
|
||
#[Validate('required')] | ||
public $max; | ||
|
||
public $step = 1; | ||
|
||
public $minRange = 1; | ||
|
||
public $format = 'number'; | ||
|
||
public function mount($defaultMin = null, $defaultMax = null) | ||
{ | ||
$this->condition = 'dual-range'; | ||
$this->selectedMin = $defaultMin ?? $this->min; | ||
$this->selectedMax = $defaultMax ?? $this->max; | ||
} | ||
|
||
public function dispatchEvent() | ||
{ | ||
$this->dispatch('filter-updated', | ||
field: $this->field, | ||
condition: $this->condition, | ||
payload: [ | ||
'min' => $this->selectedMin, | ||
'max' => $this->selectedMax, | ||
], | ||
command: 'replace', | ||
modifier: $this->modifier, | ||
) | ||
->to(LivewireCollection::class); | ||
} | ||
|
||
public function updatedSelectedMin($value) | ||
{ | ||
// Ensure min doesn't exceed max - minRange | ||
if ($value > $this->selectedMax - $this->minRange) { | ||
$this->selectedMin = $this->selectedMax - $this->minRange; | ||
} | ||
$this->dispatchEvent(); | ||
} | ||
|
||
public function updatedSelectedMax($value) | ||
{ | ||
// Ensure max doesn't go below min + minRange | ||
if ($value < $this->selectedMin + $this->minRange) { | ||
$this->selectedMax = $this->selectedMin + $this->minRange; | ||
} | ||
$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']; | ||
} | ||
if (isset($params[$paramKey]['max'])) { | ||
$this->selectedMax = $params[$paramKey]['max']; | ||
} | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('statamic-livewire-filters::livewire.filters.'.$this->view); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.