Skip to content

Commit

Permalink
ComponentFilterTests
Browse files Browse the repository at this point in the history
  • Loading branch information
lrljoe committed Sep 12, 2023
1 parent 03d31d2 commit de50930
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<livewire:dynamic-component :key="$wirekey" :is=$livewireComponent wire:model="{{ $modelableValue }}" />
5 changes: 3 additions & 2 deletions resources/views/components/tools/toolbar/bootstrap.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@
])
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->render($component->filterLayout, $tableName, $component->isTailwind(), $component->isBootstrap4(), $component->isBootstrap5()) }}

{{ $filter->setFilterDTO($component->getFilterRenderDTO())->render() }}
</div>
@endforeach

Expand Down Expand Up @@ -408,7 +409,7 @@ class="{{ $loop->last ? 'mb-0' : 'mb-1' }} form-check-label"
])
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->render($component->filterLayout, $tableName, $component->isTailwind(), $component->isBootstrap4(), $component->isBootstrap5()) }}
{{ $filter->setFilterDTO($component->getFilterRenderDTO())->render() }}
</div>
@endforeach
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/tools/toolbar/tailwind.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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->setFilterRenderDTO($component->getFilterRenderDTO())->render() }}
</div>
</div>
@endforeach
Expand Down Expand Up @@ -352,7 +352,7 @@ class="grid grid-cols-12 gap-6 px-4 md:p-0 mb-6"
])
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->render($component->filterLayout, $tableName, $component->isTailwind(), $component->isBootstrap4(), $component->isBootstrap5()) }}
{{ $filter->setFilterRenderDTO($component->getFilterRenderDTO())->render() }}
</div>
@endforeach
</div>
Expand Down
5 changes: 4 additions & 1 deletion src/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ public function customView(): string
return 'livewire-tables::stubs.custom';
}

public function render(): \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
public function rendering()
{
$this->setupColumnSelect();
$this->setupPagination();
$this->setupSecondaryHeader();
$this->setupFooter();
$this->setupReordering();
}

public function render(): \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
return view('livewire-tables::datatable')
->with([
'columns' => $this->getColumns(),
Expand Down
48 changes: 48 additions & 0 deletions src/DataTransferObjects/FilterRenderData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rappasoft\LaravelLivewireTables\DataTransferObjects;

use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Filter;

class FilterRenderData
{
public DataTableComponent $component;

public array $globalData = [];

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

$this->setupGlobalData();
}

protected function setupGlobalData()
{
$this->globalData = [
'filterLayout' => $this->component->getFilterLayout(),
'tableName' => $this->component->getTableName(),
'isTailwind' => $this->component->isTailwind(),
'isBootstrap' => $this->component->isBootstrap(),
'isBootstrap4' => $this->component->isBootstrap4(),
'isBootstrap5' => $this->component->isBootstrap5(),
'filter' => null,
'filter-key' => null,
'filter-id' => null,
];
}

public function filterDataToArray(Filter $filter): array
{
$tempArray = $this->globalData;
$tempArray['filter'] = $filter;

return $tempArray;
}

public function toArray(): array
{
return $this->globalData;
}
}
9 changes: 9 additions & 0 deletions src/Traits/Configuration/FilterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

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

trait FilterConfiguration
{
Expand Down Expand Up @@ -121,4 +122,12 @@ public function setFilterPopoverAttributes(array $attributes = []): self

return $this;
}

public function setFilterRenderDTO(): self
{
$this->filterRenderDTO = (new FilterRenderData($this));

return $this;
}

}
2 changes: 2 additions & 0 deletions src/Traits/Configuration/SearchConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,6 @@ public function setSearchFieldAttributes(array $attributes = []): self

return $this;
}


}
25 changes: 21 additions & 4 deletions src/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Rappasoft\LaravelLivewireTables\Traits\Helpers;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\{Arr,Collection};
use Livewire\Attributes\On;
use Rappasoft\LaravelLivewireTables\Views\Filter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectDropdownFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\{MultiSelectDropdownFilter,MultiSelectFilter};
use Rappasoft\LaravelLivewireTables\DataTransferObjects\FilterRenderData;

trait FilterHelpers
{
Expand All @@ -16,6 +15,9 @@ trait FilterHelpers
*/
public function mountFilterHelpers(): void
{

$tmp = $this->getFilterRenderDTO();

foreach ($this->getFilters() as $filter) {
if (! isset($this->appliedFilters[$filter->getKey()])) {
if ($filter->hasFilterDefaultValue()) {
Expand Down Expand Up @@ -315,4 +317,19 @@ public function getFilterPopoverAttributes(): array
{
return count($this->filterPopoverAttributes) ? $this->filterPopoverAttributes : ['default' => true];
}

public function getFilterRenderDTO(): FilterRenderData
{
if (!$this->hasFilterRenderDTO())
{
$this->setFilterRenderDTO();
}
return $this->filterRenderDTO;
}

public function hasFilterRenderDTO()
{
return $this->filterRenderDTO != null;
}

}
6 changes: 6 additions & 0 deletions src/Traits/WithFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\FilterConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\FilterHelpers;
use Rappasoft\LaravelLivewireTables\DataTransferObjects\FilterRenderData;

trait WithFilters
{
Expand All @@ -31,6 +32,10 @@ trait WithFilters

protected array $filterPopoverAttributes = [];

protected ?FilterRenderData $filterRenderDTO = null;

public array $filterRenderGlobalData = [];

public function filters(): array
{
return [];
Expand All @@ -57,4 +62,5 @@ public function applyFilters(): Builder

return $this->getBuilder();
}

}
9 changes: 7 additions & 2 deletions src/Views/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\FilterConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\FilterHelpers;
use Rappasoft\LaravelLivewireTables\DataTransferObjects\FilterRenderData;

abstract class Filter
{
Expand Down Expand Up @@ -43,6 +44,10 @@ abstract class Filter

protected mixed $filterDefaultValue = null;

protected array $filterViewArray = [];

protected ?FilterRenderData $filterRenderDataDTO = null;

public function __construct(string $name, string $key = null)
{
$this->name = $name;
Expand All @@ -64,6 +69,6 @@ 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;
}
13 changes: 3 additions & 10 deletions src/Views/Filters/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ 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,
]);
//dd("FILTERLAYOUTTEST - ".$array['filterLayout']);
return view('livewire-tables::components.tools.filters.date', $this->getFilterSpecificViewData());
}
}
12 changes: 2 additions & 10 deletions src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,11 @@ public function isEmpty(array|string $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
{
$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->getFilterSpecificViewData());
}
}
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->getFilterSpecificViewData());
}
}
78 changes: 78 additions & 0 deletions src/Views/Filters/LivewireComponentFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Filters;

use Rappasoft\LaravelLivewireTables\Views\Filter;
use Illuminate\Support\Str;

class LivewireComponentFilter extends Filter
{
public string $filterComponentPath = '';
public array $filterComponentOptions = [];

public function validate(mixed $value): mixed
{
return $value;
}

public function isEmpty($value): bool
{
return $value === '';
}

/**
* Gets the Default Value for this Filter via the Component
*/
public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function setFilterComponentPath(string $filterComponentPath): self
{
$this->filterComponentPath = (Str::startsWith($filterComponentPath, 'livewire:')) ? substr($filterComponentPath, 9) : $filterComponentPath;

return $this;
}

public function setFilterComponentOptions(array $filterComponentOptions): self
{
$this->filterComponentOptions = $filterComponentOptions;

return $this;
}

public function config(array $config = []): LivewireComponentFilter
{
$this->config = $config;

if ($this->filterComponentPath == '' && $this->hasConfig('filterComponentPath'))
{
$this->setFilterComponentPath($this->getConfig('filterComponentPath'));
}

if ($this->filterComponentOptions == [] && $this->hasConfig('filterComponentOptions'))
{
$this->setFilterComponentOptions($this->getConfig('filterComponentOptions'));
}

return $this;
}





public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.livewire-component-filter', [...$this->getFilterSpecificViewData(), ...[
'wirekey' => 'test12312' . '-filter-'. $this->getKey() . ($this->hasCustomPosition() ? '-'.$this->getCustomPosition() : ''),
'livewireComponent' => $this->filterComponentPath,
'modelableValue' => 'filterComponents.'.$this->getKey(),
...$this->filterComponentOptions,
]]);



}
}
14 changes: 3 additions & 11 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
{
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,
]);
public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.multi-select-dropdown', $this->getFilterSpecificViewData());
}
}
Loading

0 comments on commit de50930

Please sign in to comment.