Skip to content

Commit 8abdfd4

Browse files
Add min and max price filter options
1 parent a46de23 commit 8abdfd4

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

resources/lang/en/price-filter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
// translations for CodeWithDennis/FilamentPriceFilter
43
return [
5-
//
4+
'from' => 'from',
5+
'to' => 'to',
66
];

resources/lang/nl/price-filter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'from' => 'van',
5+
'to' => 'tot',
6+
];

src/Filament/Tables/Filters/PriceFilter.php

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,52 @@ class PriceFilter extends Filter
1212
{
1313
public Closure | string | null $currency = null;
1414

15-
public Closure | string | null $column = null;
16-
1715
public Closure | string | null $locale = null;
1816

1917
public Closure | bool | null $cents = null;
2018

19+
public Closure | int $min = 0;
20+
21+
public Closure | int $max = 100000;
22+
2123
public static function getDefaultName(): ?string
2224
{
2325
return 'priceFilter';
2426
}
2527

26-
public function currency(Closure | string | null $currency = null, Closure | string | null $locale = null, Closure | bool $cents = true, Closure | string | null $column = null): static
28+
public function currency(Closure | string | null $currency = null, Closure | string | null $locale = null, Closure | bool $cents = true): static
2729
{
2830
$this->currency = $currency;
2931
$this->locale = $locale;
3032
$this->cents = $cents;
31-
$this->column = $column;
3233

3334
return $this;
3435
}
3536

37+
public function min(Closure | int $min): static
38+
{
39+
$this->min = $min;
40+
41+
return $this;
42+
}
43+
44+
public function max(Closure | int $max): static
45+
{
46+
$this->max = $max;
47+
48+
return $this;
49+
}
50+
51+
public function getMin(): int
52+
{
53+
return $this->evaluate($this->min);
54+
}
55+
56+
public function getMax(): int
57+
{
58+
return $this->evaluate($this->max);
59+
}
60+
3661
public function getCurrency(): string
3762
{
3863
if ($this->currency === null) {
@@ -51,15 +76,6 @@ public function getLocale(): string
5176
return $this->evaluate($this->locale);
5277
}
5378

54-
public function getColumn(): string
55-
{
56-
if ($this->column === null) {
57-
return config('filament-price-filter.column');
58-
}
59-
60-
return $this->evaluate($this->column);
61-
}
62-
6379
public function getCents(): bool
6480
{
6581
if ($this->cents === null) {
@@ -81,23 +97,29 @@ protected function setUp(): void
8197
{
8298
parent::setUp();
8399

84-
$this->form([
85-
TextInput::make('from')
86-
->label(__('Price range from'))
87-
->prefix(fn () => $this->getCurrencySymbol($this->getCurrency()))
88-
->numeric(),
89-
TextInput::make('to')
90-
->label(__('Price range to'))
91-
->prefix(fn () => $this->getCurrencySymbol($this->getCurrency()))
92-
->numeric(),
93-
]);
100+
$this->form(function () {
101+
return [
102+
TextInput::make('from')
103+
->label($this->getLabel() . ' ' . __('filament-price-filter::price-filter.from'))
104+
->prefix($this->getCurrencySymbol($this->getCurrency()))
105+
->minValue($this->getMin())
106+
->maxValue($this->getMax())
107+
->numeric(),
108+
TextInput::make('to')
109+
->label($this->getLabel() . ' ' . __('filament-price-filter::price-filter.to'))
110+
->prefix($this->getCurrencySymbol($this->getCurrency()))
111+
->minValue($this->getMin())
112+
->maxValue($this->getMax())
113+
->numeric(),
114+
];
115+
});
94116

95117
$this->indicateUsing(function (array $data) {
96118
$from = isset($data['from']) && is_numeric($data['from']) ? Number::currency(number: (float) $data['from'], in: $this->getCurrency(), locale: $this->getlocale()) : null;
97119
$to = isset($data['to']) && is_numeric($data['to']) ? Number::currency(number: (float) $data['to'], in: $this->getCurrency(), locale: $this->getlocale()) : null;
98120

99-
$fromIndicator = $from !== null ? __('Price range from') . ": $from" : null;
100-
$toIndicator = $to !== null ? __('Price range to') . ": $to" : null;
121+
$fromIndicator = $from !== null ? __($this->getLabel() . ' from') . ": $from" : null;
122+
$toIndicator = $to !== null ? __($this->getLabel() . ' to') . ": $to" : null;
101123

102124
if ($from !== null && $to !== null) {
103125
return "{$fromIndicator} - {$toIndicator}";
@@ -111,14 +133,13 @@ protected function setUp(): void
111133

112134
return $query
113135
->when($data['from'] !== null, function (Builder $query) use ($cents, $data) {
114-
return $query->where($this->getColumn(), '>=', $cents ? (float) $data['from'] * 100 : (float) $data['from']);
136+
return $query->where($this->getName(), '>=', $cents ? (float) $data['from'] * 100 : (float) $data['from']);
115137
})
116138
->when($data['to'] !== null, function (Builder $query) use ($data, $cents) {
117139
return $data['to'] > 0
118-
? $query->where($this->getColumn(), '<=', $cents ? (float) $data['to'] * 100 : (float) $data['to'])
119-
: $query->where($this->getColumn(), 0);
140+
? $query->where($this->getName(), '<=', $cents ? (float) $data['to'] * 100 : (float) $data['to'])
141+
: $query->where($this->getName(), 0);
120142
});
121143
});
122-
123144
}
124145
}

0 commit comments

Comments
 (0)