Skip to content

Commit

Permalink
Merge pull request #7 from buddhaCode/addMoreConditions
Browse files Browse the repository at this point in the history
add not equal, greater than equal and less than equal conditions
  • Loading branch information
andreia authored Aug 5, 2024
2 parents eec680e + 35e6cd8 commit eb9cb94
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions resources/lang/en/filament-value-range-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

'range.placeholder' => 'Select condition',
'range.options.equal' => 'is equal to',
'range.options.not_equal' => 'is not equal to',
'range.options.between' => 'is between',
'range.options.greater_than' => 'is greater than',
'range.options.greater_than_equal' => 'is greater than or equal to',
'range.options.less_than' => 'is less than',
'range.options.less_than_equal' => 'is less than or equal to',
'range.indicator.equal' => ':label is equal to :value',
'range.indicator.not_equal' => ':label is not equal to :value',
'range.indicator.between' => ':label is between :fromValue and :toValue',
'range.indicator.greater_than' => ':label is greater than :value',
'range.indicator.greater_than_equal' => ':label is greater than or equal to :value',
'range.indicator.less_than' => ':label is less than :value',
'range.indicator.less_than_equal' => ':label is less than or equal to :value',

];
99 changes: 99 additions & 0 deletions src/Filters/ValueRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ class ValueRangeFilter extends Filter

protected ?string $indicatorEqualLabel = null;

protected ?string $indicatorNotEqualLabel = null;

protected ?string $indicatorGreaterThanLabel = null;

protected ?string $indicatorGreaterThanEqualLabel = null;

protected ?string $indicatorLessThanLabel = null;

protected ?string $indicatorLessThanEqualLabel = null;

protected string|Closure $locale = 'en';

protected function setUp(): void
Expand All @@ -40,22 +46,33 @@ protected function setUp(): void
->live()
->options([
'equal' => __('filament-value-range-filter::filament-value-range-filter.range.options.equal'),
'not_equal' => __('filament-value-range-filter::filament-value-range-filter.range.options.not_equal'),
'between' => __('filament-value-range-filter::filament-value-range-filter.range.options.between'),
'greater_than' => __('filament-value-range-filter::filament-value-range-filter.range.options.greater_than'),
'greater_than_equal' => __('filament-value-range-filter::filament-value-range-filter.range.options.greater_than_equal'),
'less_than' => __('filament-value-range-filter::filament-value-range-filter.range.options.less_than'),
'less_than_equal' => __('filament-value-range-filter::filament-value-range-filter.range.options.less_than_equal'),
])
->afterStateUpdated(function (Set $set) {
$set('range_equal', null);
$set('range_not_equal', null);
$set('range_between_from', null);
$set('range_between_to', null);
$set('range_greater_than', null);
$set('range_greater_than_equal', null);
$set('range_less_than', null);
$set('range_less_than_equal', null);
}),
Forms\Components\TextInput::make('range_equal')
->hiddenLabel()
->numeric()
->placeholder(fn (): string => $this->getFormattedValue(0))
->visible(fn (Get $get): bool => $get('range_condition') === 'equal' || empty($get('range_condition'))),
Forms\Components\TextInput::make('range_not_equal')
->hiddenLabel()
->numeric()
->placeholder(fn (): string => $this->getFormattedValue(0))
->visible(fn (Get $get): bool => $get('range_condition') === 'not_equal'),
Forms\Components\Grid::make([
'default' => 1,
'sm' => 2,
Expand All @@ -76,11 +93,21 @@ protected function setUp(): void
->numeric()
->placeholder(fn (): string => $this->getFormattedValue(0))
->visible(fn (Get $get): bool => $get('range_condition') === 'greater_than'),
Forms\Components\TextInput::make('range_greater_than_equal')
->hiddenLabel()
->numeric()
->placeholder(fn (): string => $this->getFormattedValue(0))
->visible(fn (Get $get): bool => $get('range_condition') === 'greater_than_equal'),
Forms\Components\TextInput::make('range_less_than')
->hiddenLabel()
->numeric()
->placeholder(fn (): string => $this->getFormattedValue(0))
->visible(fn (Get $get): bool => $get('range_condition') === 'less_than'),
Forms\Components\TextInput::make('range_less_than_equal')
->hiddenLabel()
->numeric()
->placeholder(fn (): string => $this->getFormattedValue(0))
->visible(fn (Get $get): bool => $get('range_condition') === 'less_than_equal'),
])
->columns(1),
])
Expand All @@ -90,6 +117,10 @@ protected function setUp(): void
$data['range_equal'],
fn (Builder $query, $value): Builder => $query->where($this->getName(), '=', $this->getValue($value)),
)
->when(
$data['range_not_equal'],
fn (Builder $query, $value): Builder => $query->where($this->getName(), '!=', $this->getValue($value)),
)
->when(
$data['range_between_from'] && $data['range_between_to'],
function (Builder $query, $value) use ($data) {
Expand All @@ -100,9 +131,17 @@ function (Builder $query, $value) use ($data) {
$data['range_greater_than'],
fn (Builder $query, $value): Builder => $query->where($this->getName(), '>', $this->getValue($value)),
)
->when(
$data['range_greater_than_equal'],
fn (Builder $query, $value): Builder => $query->where($this->getName(), '>=', $this->getValue($value)),
)
->when(
$data['range_less_than'],
fn (Builder $query, $value): Builder => $query->where($this->getName(), '<', $this->getValue($value)),
)
->when(
$data['range_less_than_equal'],
fn (Builder $query, $value): Builder => $query->where($this->getName(), '<=', $this->getValue($value)),
);
})
->indicateUsing(function (array $data): array {
Expand All @@ -126,6 +165,14 @@ function (Builder $query, $value) use ($data) {
->removeField('range_equal');
}

if ($data['range_not_equal']) {
$indicators[] = Indicator::make(__('filament-value-range-filter::filament-value-range-filter.range.indicator.not_equal', [
'label' => $this->getIndicatorEqualLabel() ?? $this->getLabel(),
'value' => $this->getFormattedValue($data['range_not_equal'])
]))
->removeField('range_not_equal');
}

if ($data['range_greater_than']) {
$indicators[] = Indicator::make(__('filament-value-range-filter::filament-value-range-filter.range.indicator.greater_than', [
'label' => $this->getIndicatorGreaterThanLabel() ?? $this->getLabel(),
Expand All @@ -134,6 +181,14 @@ function (Builder $query, $value) use ($data) {
->removeField('range_greater_than');
}

if ($data['range_greater_than_equal']) {
$indicators[] = Indicator::make(__('filament-value-range-filter::filament-value-range-filter.range.indicator.greater_than_equal', [
'label' => $this->getIndicatorGreaterThanEqualLabel() ?? $this->getLabel(),
'value' => $this->getFormattedValue($data['range_greater_than_equal'])
]))
->removeField('range_greater_than_equal');
}

if ($data['range_less_than']) {
$indicators[] = Indicator::make(__('filament-value-range-filter::filament-value-range-filter.range.indicator.less_than', [
'label' => $this->getIndicatorLessThanLabel() ?? $this->getLabel(),
Expand All @@ -142,6 +197,14 @@ function (Builder $query, $value) use ($data) {
->removeField('range_less_than');
}

if ($data['range_less_than_equal']) {
$indicators[] = Indicator::make(__('filament-value-range-filter::filament-value-range-filter.range.indicator.less_than_equal', [
'label' => $this->getIndicatorLessThanEqualLabel() ?? $this->getLabel(),
'value' => $this->getFormattedValue($data['range_less_than_equal'])
]))
->removeField('range_less_than_equal');
}

return $indicators;
});
}
Expand Down Expand Up @@ -188,6 +251,18 @@ public function getIndicatorEqualLabel(): ?string
return $this->evaluate($this->indicatorEqualLabel);
}

public function indicatorNotEqualLabel(?string $label): static
{
$this->indicatorNotEqualLabel = $label;

return $this;
}

public function getIndicatorNotEqualLabel(): ?string
{
return $this->evaluate($this->indicatorNotEqualLabel);
}

public function indicatorGreaterThanLabel(?string $label): static
{
$this->indicatorGreaterThanLabel = $label;
Expand All @@ -200,6 +275,18 @@ public function getIndicatorGreaterThanLabel(): ?string
return $this->evaluate($this->indicatorGreaterThanLabel);
}

public function indicatorGreaterThanEqualLabel(?string $label): static
{
$this->indicatorGreaterThanEqualLabel = $label;

return $this;
}

public function getIndicatorGreaterThanEqualLabel(): ?string
{
return $this->evaluate($this->indicatorGreaterThanEqualLabel);
}

public function indicatorLessThanLabel(?string $label): static
{
$this->indicatorLessThanLabel = $label;
Expand All @@ -212,6 +299,18 @@ public function getIndicatorLessThanLabel(): ?string
return $this->evaluate($this->indicatorLessThanLabel);
}

public function indicatorLessThanEqualLabel(?string $label): static
{
$this->indicatorLessThanEqualLabel = $label;

return $this;
}

public function getIndicatorLessThanEqualLabel(): ?string
{
return $this->evaluate($this->indicatorLessThanEqualLabel);
}

public function locale(string|Closure $locale = 'en'): static
{
$this->locale = $locale;
Expand Down

0 comments on commit eb9cb94

Please sign in to comment.