From 635e47cca8d32691194762bff9b569025e287be3 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Mon, 11 Dec 2023 15:52:30 -0300 Subject: [PATCH] improves --- .../Filters/Builders/BuilderBase.php | 6 +- src/Components/Filters/Builders/Number.php | 64 +++++++++++-------- src/Components/Filters/FilterNumber.php | 4 +- src/DataSource/Builder.php | 16 ++--- src/DataSource/Collection.php | 14 ++-- src/Traits/HasFilter.php | 15 +++-- tests/Feature/Filters/FilterMultipleTest.php | 4 +- tests/Feature/Filters/FilterNumberTest.php | 19 ++---- tests/Pest.php | 9 ++- 9 files changed, 79 insertions(+), 72 deletions(-) diff --git a/src/Components/Filters/Builders/BuilderBase.php b/src/Components/Filters/Builders/BuilderBase.php index 347b4740..0e7299b3 100644 --- a/src/Components/Filters/Builders/BuilderBase.php +++ b/src/Components/Filters/Builders/BuilderBase.php @@ -2,16 +2,18 @@ namespace PowerComponents\LivewirePowerGrid\Components\Filters\Builders; +use Livewire\Component; use PowerComponents\LivewirePowerGrid\Components\Filters\FilterBase; class BuilderBase { - public static function make(FilterBase $filterBase): self + public static function make(Component $component, FilterBase $filterBase): self { - return new self($filterBase); + return new self($component, $filterBase); } public function __construct( + protected Component $component, protected null|array|FilterBase $filterBase = null ) { } diff --git a/src/Components/Filters/Builders/Number.php b/src/Components/Filters/Builders/Number.php index f10df65f..cd5b071d 100644 --- a/src/Components/Filters/Builders/Number.php +++ b/src/Components/Filters/Builders/Number.php @@ -6,10 +6,15 @@ use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Collection; +use function Livewire\store; + class Number extends BuilderBase { public function builder(Builder|QueryBuilder $builder, string $field, int|array|string|null $values): void { + $thousands = store($this->component)->get('filters.number.' . $field . '.thousands'); + $decimal = store($this->component)->get('filters.number.' . $field . '.decimal'); + if (data_get($this->filterBase, 'builder')) { /** @var \Closure $closure */ $closure = data_get($this->filterBase, 'builder'); @@ -23,12 +28,12 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array| if (isset($values['start']) && !isset($values['end'])) { $start = $values['start']; - if (isset($values['thousands'])) { - $start = str_replace($values['thousands'], '', $start); + if (isset($thousands)) { + $start = str_replace($thousands, '', $start); } - if (isset($values['decimal'])) { - $start = str_replace($values['decimal'], '.', $start); + if (isset($decimal)) { + $start = str_replace($decimal, '.', $start); } $builder->where($field, '>=', $start); @@ -37,12 +42,12 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array| if (!isset($values['start']) && isset($values['end'])) { $end = $values['end']; - if (isset($values['decimal'])) { - $end = str_replace($values['thousands'], '', $values['end']); + if (isset($decimal)) { + $end = str_replace($thousands, '', $values['end']); } - if (isset($values['decimal'])) { - $end = (float) str_replace($values['decimal'], '.', $end); + if (isset($decimal)) { + $end = (float) str_replace($decimal, '.', $end); } $builder->where($field, '<=', $end); @@ -52,14 +57,14 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array| $start = $values['start']; $end = $values['end']; - if (isset($values['thousands'])) { - $start = str_replace($values['thousands'], '', $values['start']); - $end = str_replace($values['thousands'], '', $values['end']); + if (isset($thousands)) { + $start = str_replace($thousands, '', $values['start']); + $end = str_replace($thousands, '', $values['end']); } - if (isset($values['decimal'])) { - $start = str_replace($values['decimal'], '.', $start); - $end = str_replace($values['decimal'], '.', $end); + if (isset($decimal)) { + $start = str_replace($decimal, '.', $start); + $end = str_replace($decimal, '.', $end); } $builder->whereBetween($field, [$start, $end]); @@ -68,6 +73,9 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array| public function collection(Collection $collection, string $field, int|array|string|null $values): Collection { + $thousands = store($this->component)->get('filters.number.' . $field . '.thousands'); + $decimal = store($this->component)->get('filters.number.' . $field . '.decimal'); + if (data_get($this->filterBase, 'collection')) { /** @var \Closure $closure */ $closure = data_get($this->filterBase, 'collection'); @@ -79,12 +87,12 @@ public function collection(Collection $collection, string $field, int|array|stri if (isset($values['start']) && !isset($values['end'])) { $start = $values['start']; - if (isset($values['thousands'])) { - $start = str_replace($values['thousands'], '', $values['start']); + if (isset($thousands)) { + $start = str_replace($thousands, '', $values['start']); } - if (isset($values['decimal'])) { - $start = (float) str_replace($values['decimal'], '.', $start); + if (isset($decimal)) { + $start = (float) str_replace($decimal, '.', $start); } return $collection->where($field, '>=', $start); @@ -93,12 +101,12 @@ public function collection(Collection $collection, string $field, int|array|stri if (!isset($values['start']) && isset($values['end'])) { $end = $values['end']; - if (isset($values['thousands'])) { - $end = str_replace($values['thousands'], '', $values['end']); + if (isset($thousands)) { + $end = str_replace($thousands, '', $values['end']); } - if (isset($values['decimal'])) { - $end = (float) str_replace($values['decimal'], '.', $end); + if (isset($decimal)) { + $end = (float) str_replace($decimal, '.', $end); } return $collection->where($field, '<=', $end); @@ -108,14 +116,14 @@ public function collection(Collection $collection, string $field, int|array|stri $start = $values['start']; $end = $values['end']; - if (isset($values['thousands'])) { - $start = str_replace($values['thousands'], '', $values['start']); - $end = str_replace($values['thousands'], '', $values['end']); + if (isset($thousands)) { + $start = str_replace($thousands, '', $values['start']); + $end = str_replace($thousands, '', $values['end']); } - if (isset($values['decimal'])) { - $start = str_replace($values['decimal'], '.', $start); - $end = str_replace($values['decimal'], '.', $end); + if (isset($decimal)) { + $start = str_replace($decimal, '.', $start); + $end = str_replace($decimal, '.', $end); } return $collection->whereBetween($field, [$start, $end]); diff --git a/src/Components/Filters/FilterNumber.php b/src/Components/Filters/FilterNumber.php index 87a618d9..66daaeca 100644 --- a/src/Components/Filters/FilterNumber.php +++ b/src/Components/Filters/FilterNumber.php @@ -41,11 +41,11 @@ public static function getWireAttributes(string $field, array $filter): array { return collect() ->put('inputStartAttributes', new ComponentAttributeBag([ - 'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.start', + // 'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.start', 'wire:input.live.debounce.800ms' => 'filterNumberStart(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)', ])) ->put('inputEndAttributes', new ComponentAttributeBag([ - 'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.end', + // 'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.end', 'wire:input.live.debounce.800ms' => 'filterNumberEnd(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)', ])) ->toArray(); diff --git a/src/DataSource/Builder.php b/src/DataSource/Builder.php index 8ef25a00..d914d6e0 100644 --- a/src/DataSource/Builder.php +++ b/src/DataSource/Builder.php @@ -43,7 +43,7 @@ public function filter(): EloquentBuilder|QueryBuilder } foreach ($this->powerGridComponent->filters as $filterType => $filter) { - if (empty($filter)) { + if (blank($filter)) { continue; } @@ -59,13 +59,13 @@ public function filter(): EloquentBuilder|QueryBuilder ->first(); match ($filterType) { - 'datetime' => (new DateTimePicker($filter))->builder($query, $field, $value), - 'date' => (new DatePicker($filter))->builder($query, $field, $value), - 'multi_select' => (new MultiSelect($filter))->builder($query, $field, $value), - 'select' => (new Select($filter))->builder($query, $field, $value), - 'boolean' => (new Boolean($filter))->builder($query, $field, $value), - 'number' => (new Number($filter))->builder($query, $field, $value), - 'input_text' => (new InputText($filter))->builder($query, $field, [ + 'datetime' => (new DateTimePicker($this->powerGridComponent, $filter))->builder($query, $field, $value), + 'date' => (new DatePicker($this->powerGridComponent, $filter))->builder($query, $field, $value), + 'multi_select' => (new MultiSelect($this->powerGridComponent, $filter))->builder($query, $field, $value), + 'select' => (new Select($this->powerGridComponent, $filter))->builder($query, $field, $value), + 'boolean' => (new Boolean($this->powerGridComponent, $filter))->builder($query, $field, $value), + 'number' => (new Number($this->powerGridComponent, $filter))->builder($query, $field, $value), + 'input_text' => (new InputText($this->powerGridComponent, $filter))->builder($query, $field, [ 'selected' => $this->validateInputTextOptions($this->powerGridComponent->filters, $field), 'value' => $value, 'searchMorphs' => $this->powerGridComponent->searchMorphs, diff --git a/src/DataSource/Collection.php b/src/DataSource/Collection.php index a2d523a4..5aa810c6 100644 --- a/src/DataSource/Collection.php +++ b/src/DataSource/Collection.php @@ -101,13 +101,13 @@ public function filter(): BaseCollection ->first(); $this->collection = match ($filterType) { - 'datetime' => (new DateTimePicker($filter))->collection($this->collection, $field, $value), - 'date' => (new DatePicker($filter))->collection($this->collection, $field, $value), - 'multi_select' => (new MultiSelect($filter))->collection($this->collection, $field, $value), - 'select' => (new Select($filter))->collection($this->collection, $field, $value), - 'boolean' => (new Boolean($filter))->collection($this->collection, $field, $value), - 'number' => (new Number($filter))->collection($this->collection, $field, $value), - 'input_text' => (new InputText($filter))->collection($this->collection, $field, [ + 'datetime' => (new DateTimePicker($this->powerGridComponent, $filter))->collection($this->collection, $field, $value), + 'date' => (new DatePicker($this->powerGridComponent, $filter))->collection($this->collection, $field, $value), + 'multi_select' => (new MultiSelect($this->powerGridComponent, $filter))->collection($this->collection, $field, $value), + 'select' => (new Select($this->powerGridComponent, $filter))->collection($this->collection, $field, $value), + 'boolean' => (new Boolean($this->powerGridComponent, $filter))->collection($this->collection, $field, $value), + 'number' => (new Number($this->powerGridComponent, $filter))->collection($this->collection, $field, $value), + 'input_text' => (new InputText($this->powerGridComponent, $filter))->collection($this->collection, $field, [ 'selected' => $this->validateInputTextOptions($this->powerGridComponent->filters, $field), 'value' => $value, ]), diff --git a/src/Traits/HasFilter.php b/src/Traits/HasFilter.php index 38809f38..191a7f3e 100644 --- a/src/Traits/HasFilter.php +++ b/src/Traits/HasFilter.php @@ -5,6 +5,9 @@ use DateTimeZone; use Illuminate\Support\{Carbon}; use Livewire\Attributes\On; + +use function Livewire\store; + use PowerComponents\LivewirePowerGrid\Column; trait HasFilter @@ -280,10 +283,10 @@ public function filterNumberStart(string $field, array $params, string $value): $this->resetPage(); - $value = filter_var($value, FILTER_SANITIZE_NUMBER_INT); + $this->filters['number'][$field]['start'] = $value; - $this->filters['number'][$field]['thousands'] = $thousands; - $this->filters['number'][$field]['decimal'] = $decimal; + store($this)->set('filters.number.' . $field . '.thousands', $thousands); + store($this)->set('filters.number.' . $field . '.decimal', $decimal); $this->enabledFilters[$field]['id'] = $field; $this->enabledFilters[$field]['label'] = $title; @@ -303,10 +306,10 @@ public function filterNumberEnd(string $field, array $params, string $value): vo $this->resetPage(); - $value = filter_var($value, FILTER_SANITIZE_NUMBER_INT); + store($this)->set('filters.number.' . $field . '.thousands', $thousands); + store($this)->set('filters.number.' . $field . '.decimal', $decimal); - $this->filters['number'][$field]['thousands'] = $thousands; - $this->filters['number'][$field]['decimal'] = $decimal; + $this->filters['number'][$field]['end'] = $value; $this->enabledFilters[$field]['id'] = $field; $this->enabledFilters[$field]['label'] = $title; diff --git a/tests/Feature/Filters/FilterMultipleTest.php b/tests/Feature/Filters/FilterMultipleTest.php index 247ee77a..33ecba73 100644 --- a/tests/Feature/Filters/FilterMultipleTest.php +++ b/tests/Feature/Filters/FilterMultipleTest.php @@ -92,7 +92,7 @@ public function filters(): array $component->assertSee('Barco-Sushi da Sueli'); - $filters = array_merge($component->filters, filterNumber('price', '80,00', '100', '.', ',')); + $filters = array_merge($component->filters, filterNumber('price', '80.00', '100')); $component->set('filters', $filters) ->assertDontSee('Barco-Sushi da Sueli') @@ -118,7 +118,7 @@ public function filters(): array 'input_text' => [], 'input_text_options' => [], ], - filterNumber('price', '80,00', '100', '.', ','), + filterNumber('price', '80.00', '100'), filterBoolean('in_stock', 'true') ); diff --git a/tests/Feature/Filters/FilterNumberTest.php b/tests/Feature/Filters/FilterNumberTest.php index 9b8d3bf4..7fdfa6b1 100644 --- a/tests/Feature/Filters/FilterNumberTest.php +++ b/tests/Feature/Filters/FilterNumberTest.php @@ -22,10 +22,6 @@ ]) ->call($params->theme) ->set('filters', filterNumber($params->field, '2', null)) - ->assertSeeHtmlInOrder([ - 'wire:model.live.debounce.800ms="filters.number.' . $params->field . '.start', - 'wire:model.live.debounce.800ms="filters.number.' . $params->field . '.end', - ]) ->assertSee('Peixada da chef Nábia') ->assertSee('Francesinha') ->assertSee('борщ') @@ -42,10 +38,8 @@ public function filters(): array Filter::number('id') ->builder(function ($builder, $values) { expect($values)->toBe([ - 'start' => '2', - 'end' => null, - 'thousands' => '', - 'decimal' => '', + 'start' => '2', + 'end' => null, ]) ->and($builder)->toBeInstanceOf(\Illuminate\Database\Eloquent\Builder::class); @@ -220,14 +214,15 @@ public function filters(): array })->group('filters', 'filterNumber')->with('filter_number_themes_collection', 'filter_number_themes_array'); it('properly filters by "min & max" formatted', function (string $component, object $params) { - livewire($component) + $instance = livewire($component) ->call($params->theme) ->set('testFilters', [ - Filter::number('price', 'price') + Filter::number('price') ->thousands('.') ->decimal('.'), - ]) - ->set('filters', filterNumber('price', '1,50', '20,51', '.', ',')) + ]); + + $instance->set('filters', filterNumber('price', '1.50', '20.51')) ->assertSee('Pastel de Nata') ->assertSee('Peixada da chef Nábia') ->assertDontSee('Carne Louca'); diff --git a/tests/Pest.php b/tests/Pest.php index 2de9a447..6a830f3c 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -100,15 +100,14 @@ function expectInputText(object $params, mixed $component, string $value, string } } -function filterNumber(string $field, ?string $min, ?string $max, ?string $thousands = '', ?string $decimal = ''): array +function filterNumber(string $field, ?string $min, ?string $max): array { return [ 'number' => [ $field => [ - 'start' => $min, - 'end' => $max, - 'thousands' => $thousands, - 'decimal' => $decimal, + 'start' => $min, + 'end' => $max, + ], ], ];