Skip to content

Commit

Permalink
V3 - Filter DTOs (#1503)
Browse files Browse the repository at this point in the history
* Initial Update - Using DTO

* Add filterGenericData

* Remove requirement for method on render for Filter

* Remove Render requirement

* GenericHeaderTest

* Add filter comparison

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored Nov 1, 2023
1 parent 0025311 commit 1c86792
Show file tree
Hide file tree
Showing 29 changed files with 154 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
All notable changes to `laravel-livewire-tables` will be documented in this file

## UNRELEASED
- Modify Filters to use DTO for Generic Property List by @lrljoe in [#1503](https://github.com/rappasoft/laravel-livewire-tables/pull/1503)
- Split ConfigurableAreas, CollapsingColumns and TableAttributes into own Traits/Config/Helper Files for Maintainability by @lrljoe in [#1514](https://github.com/rappasoft/laravel-livewire-tables/pull/1514)
- Add "HasAllTraits" for Maintainability by @lrljoe in [#1514](https://github.com/rappasoft/laravel-livewire-tables/pull/1514)

Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/table/tr/footer.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@aware(['component', 'tableName'])
@props(['rows'])
@props(['rows', 'filterGenericData'])

<x-livewire-tables::table.tr.plain
:customAttributes="$this->getFooterTrAttributes($rows)"
Expand All @@ -20,7 +20,7 @@
@continue($this->columnSelectIsEnabled() && ! $this->columnSelectIsEnabledForColumn($column))
@continue($column->isReorderColumn() && !$this->getCurrentlyReorderingStatus() && $this->getHideReorderColumnUnlessReorderingStatus())
<x-livewire-tables::table.td.plain :displayMinimisedOnReorder="true" wire:key="{{ $tableName .'-footer-shown-'.$colIndex }}" :column="$column" :customAttributes="$this->getFooterTdAttributes($column, $rows, $colIndex)">
{{ $column->getFooterContents($rows) }}
{{ $column->getFooterContents($rows, $filterGenericData) }}
</x-livewire-tables::table.td.plain>
@endforeach
</x-livewire-tables::table.tr.plain>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@aware(['component', 'tableName'])
@props(['rows'])
@props(['rows', 'filterGenericData'])

<x-livewire-tables::table.tr.plain
:customAttributes="$this->getSecondaryHeaderTrAttributes($rows)"
Expand All @@ -22,7 +22,7 @@
@continue($column->isReorderColumn() && !$this->getCurrentlyReorderingStatus() && $this->getHideReorderColumnUnlessReorderingStatus())

<x-livewire-tables::table.td.plain :column="$column" :displayMinimisedOnReorder="true" wire:key="{{ $tableName .'-secondary-header-show-'.$column->getSlug() }}" :customAttributes="$this->getSecondaryHeaderTdAttributes($column, $rows, $colIndex)">
{{ $column->getSecondaryHeaderContents($rows) }}
{{ $column->getSecondaryHeaderContents($rows, $filterGenericData) }}
</x-livewire-tables::table.td.plain>
@endforeach
</x-livewire-tables::table.tr.plain>
5 changes: 3 additions & 2 deletions resources/views/components/tools/toolbar.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@aware(['component', 'tableName'])
@props(['filterGenericData'])

@if ($component->hasConfigurableAreaFor('before-toolbar'))
@include($component->getConfigurableAreaFor('before-toolbar'), $component->getParametersForConfigurableArea('before-toolbar'))
Expand Down Expand Up @@ -29,7 +30,7 @@
@endif

@if ($component->filtersAreEnabled() && $component->filtersVisibilityIsEnabled() && $component->hasVisibleFilters())
<x-livewire-tables::tools.toolbar.items.filter-button />
<x-livewire-tables::tools.toolbar.items.filter-button :$filterGenericData />
@endif

@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
Expand Down Expand Up @@ -72,7 +73,7 @@
$component->hasVisibleFilters() &&
$component->isFilterLayoutSlideDown()
)
<x-livewire-tables::tools.toolbar.items.filter-slidedown />
<x-livewire-tables::tools.toolbar.items.filter-slidedown :$filterGenericData />
@endif


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@aware(['component', 'tableName'])
@props(['filterGenericData'])

<div x-cloak x-show="!currentlyReorderingStatus"
@class([
'ml-0 ml-md-2 mb-3 mb-md-0' => $component->isBootstrap4(),
Expand Down Expand Up @@ -54,7 +56,7 @@
</div>

@if ($component->isFilterLayoutPopover())
<x-livewire-tables::tools.toolbar.items.filter-popover />
<x-livewire-tables::tools.toolbar.items.filter-popover :$filterGenericData />
@endif

</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
])
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->render($component->filterLayout, $tableName, $component->isTailwind(), $component->isBootstrap4(), $component->isBootstrap5()) }}
{{ $filter->setFilterGenericData($filterGenericData)->render($filterGenericData) }}
</div>
@endforeach

Expand Down Expand Up @@ -61,7 +61,7 @@ class="block px-4 py-2 text-sm text-gray-700 space-y-1"
role="menuitem"
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->render($component->filterLayout, $tableName, $component->isTailwind(), $component->isBootstrap4(), $component->isBootstrap5()) }}
{{ $filter->setGenericDisplayData($filterGenericData)->render() }}
</div>
</div>
@endforeach
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@aware(['component', 'tableName'])
@props(['filterGenericData'])

<div x-cloak x-show="!currentlyReorderingStatus && filtersOpen"
@class([
'container' => $component->isBootstrap(),
Expand Down Expand Up @@ -56,7 +58,7 @@
])
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->render($component->filterLayout, $tableName, $component->isTailwind(), $component->isBootstrap4(), $component->isBootstrap5()) }}
{{ $filter->setGenericDisplayData($filterGenericData)->render() }}
</div>
@endforeach
</div>
Expand Down
8 changes: 4 additions & 4 deletions resources/views/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<x-livewire-tables::tools>
<x-livewire-tables::tools.sorting-pills />
<x-livewire-tables::tools.filter-pills />
<x-livewire-tables::tools.toolbar />
<x-livewire-tables::tools.toolbar :$filterGenericData />
</x-livewire-tables::tools>

<x-livewire-tables::table>
Expand All @@ -28,7 +28,7 @@
</x-slot>

@if($this->secondaryHeaderIsEnabled() && $this->hasColumnsWithSecondaryHeader())
<x-livewire-tables::table.tr.secondary-header :rows="$rows" />
<x-livewire-tables::table.tr.secondary-header :rows="$rows" :$filterGenericData />
@endif
@if($this->hasDisplayLoadingPlaceholder())
<x-livewire-tables::includes.loading colCount="{{ $this->columns->count()+1 }}" />
Expand Down Expand Up @@ -62,9 +62,9 @@
@if ($this->footerIsEnabled() && $this->hasColumnsWithFooter())
<x-slot name="tfoot">
@if ($this->useHeaderAsFooterIsEnabled())
<x-livewire-tables::table.tr.secondary-header :rows="$rows" />
<x-livewire-tables::table.tr.secondary-header :rows="$rows" :$filterGenericData />
@else
<x-livewire-tables::table.tr.footer :rows="$rows" />
<x-livewire-tables::table.tr.footer :rows="$rows" :$filterGenericData />
@endif
</x-slot>
@endif
Expand Down
1 change: 1 addition & 0 deletions src/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function render(): \Illuminate\Contracts\Foundation\Application|\Illumina

return view('livewire-tables::datatable')
->with([
'filterGenericData' => $this->getFilterGenericData(),
'columns' => $this->getColumns(),
'rows' => $this->getRows(),
'customView' => $this->customView(),
Expand Down
27 changes: 27 additions & 0 deletions src/DataTransferObjects/FilterGenericData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rappasoft\LaravelLivewireTables\DataTransferObjects;

use Rappasoft\LaravelLivewireTables\DataTableComponent;

class FilterGenericData
{
public DataTableComponent $component;

public function __construct(DataTableComponent $component)
{
$this->component = $component;
}

public function toArray(): array
{
return [
'tableName' => $this->component->getTableName(),
'filterLayout' => $this->component->getFilterLayout(),
'isTailwind' => $this->component->isTailwind(),
'isBootstrap' => ($this->component->isBootstrap4() || $this->component->isBootstrap5()),
'isBootstrap4' => $this->component->isBootstrap4(),
'isBootstrap5' => $this->component->isBootstrap5(),
];
}
}
11 changes: 11 additions & 0 deletions src/Traits/Configuration/FilterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

use Rappasoft\LaravelLivewireTables\DataTransferObjects\FilterGenericData;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;

trait FilterConfiguration
Expand Down Expand Up @@ -114,4 +115,14 @@ public function setFilterSlideDownDefaultStatusEnabled(): self

return $this;
}

public function generateFilterGenericData(): array
{
return (new FilterGenericData($this))->toArray();
}

public function setFilterGenericData(array $filterGenericData = [])
{
$this->filterGenericData = $filterGenericData;
}
}
14 changes: 14 additions & 0 deletions src/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,18 @@ public function getFiltersByRow(): array

return $orderedFilters;
}

public function hasFilterGenericData()
{
return ! empty($this->filterGenericData);
}

public function getFilterGenericData()
{
if (! $this->hasFilterGenericData()) {
$this->setFilterGenericData($this->generateFilterGenericData());
}

return $this->filterGenericData;
}
}
2 changes: 2 additions & 0 deletions src/Traits/WithFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ trait WithFilters

public array $appliedFilters = [];

public array $filterGenericData = [];

public function filters(): array
{
return [];
Expand Down
4 changes: 3 additions & 1 deletion src/Views/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ abstract class Filter

protected mixed $filterDefaultValue = null;

public array $genericDisplayData = [];

public function __construct(string $name, string $key = null)
{
$this->name = $name;
Expand All @@ -67,5 +69,5 @@ public static function make(string $name, string $key = null): Filter

abstract public function isEmpty(string $value): bool;

abstract public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory;
abstract public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory;
}
12 changes: 2 additions & 10 deletions src/Views/Filters/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,8 @@ public function getFilterDefaultValue(): ?string
return $this->filterDefaultValue ?? null;
}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.date', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.date', $this->getFilterDisplayData());
}
}
17 changes: 2 additions & 15 deletions src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,13 @@ public function getDateString(string|array $dateInput): string
}

return $startDate.' to '.$endDate;

}

return '';

}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory

Check warning on line 191 in src/Views/Filters/DateRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/DateRangeFilter.php#L191

Added line #L191 was not covered by tests
{
$this->getOptions();
$this->getConfigs();

return view('livewire-tables::components.tools.filters.date-range', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.date-range', $this->getFilterDisplayData());

Check warning on line 193 in src/Views/Filters/DateRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/DateRangeFilter.php#L193

Added line #L193 was not covered by tests
}
}
12 changes: 2 additions & 10 deletions src/Views/Filters/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,8 @@ public function getFilterDefaultValue(): ?string
return $this->filterDefaultValue ?? null;
}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.datetime', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.datetime', $this->getFilterDisplayData());
}
}
12 changes: 2 additions & 10 deletions src/Views/Filters/MultiSelectDropdownFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,8 @@ public function isEmpty($value): bool
return false;
}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.multi-select-dropdown', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.multi-select-dropdown', $this->getFilterDisplayData());
}
}
12 changes: 2 additions & 10 deletions src/Views/Filters/MultiSelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,8 @@ public function isEmpty($value): bool
return ! is_array($value);
}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.multi-select', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.multi-select', $this->getFilterDisplayData());
}
}
12 changes: 2 additions & 10 deletions src/Views/Filters/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ public function getFilterDefaultValue(): ?string
return $this->filterDefaultValue ?? null;
}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.number', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.number', $this->getFilterDisplayData());
}
}
13 changes: 2 additions & 11 deletions src/Views/Filters/NumberRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,8 @@ public function getFilterPillValue($values): ?string
return '';
}

public function render(string $filterLayout, string $tableName, bool $isTailwind, bool $isBootstrap4, bool $isBootstrap5): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory

Check warning on line 89 in src/Views/Filters/NumberRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/NumberRangeFilter.php#L89

Added line #L89 was not covered by tests
{

return view('livewire-tables::components.tools.filters.number-range', [
'filterLayout' => $filterLayout,
'tableName' => $tableName,
'isTailwind' => $isTailwind,
'isBootstrap' => ($isBootstrap4 || $isBootstrap5),
'isBootstrap4' => $isBootstrap4,
'isBootstrap5' => $isBootstrap5,
'filter' => $this,
]);
return view('livewire-tables::components.tools.filters.number-range', $this->getFilterDisplayData());

Check warning on line 91 in src/Views/Filters/NumberRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/NumberRangeFilter.php#L91

Added line #L91 was not covered by tests
}
}
Loading

0 comments on commit 1c86792

Please sign in to comment.