Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BETA: Livewire Custom Filter (Array) #2025

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div wire:key="filterComponents.{{ $filter->getKey() }}-wrapper">
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />
<livewire:dynamic-component :is="$livewireComponent" :tableComponent="get_class($this)" :filterKey="$filter->getKey()" :$tableName :key="'filterComponents-'.$filter->getKey()" wire:model="filterComponents.{{ $filter->getKey() }}" />
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div wire:key="filterComponents.{{ $filter->getKey() }}-wrapper">
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />

<livewire:dynamic-component :is="$livewireComponent" :filterKey="$filter->getKey()" :key="'filterComponents-'.$filter->getKey()" wire:model.live="filterComponents.{{ $filter->getKey() }}" />
<livewire:dynamic-component :is="$livewireComponent" :tableComponent="get_class($this)" :filterKey="$filter->getKey()" :$tableName :key="'filterComponents-'.$filter->getKey()" wire:model.live="filterComponents.{{ $filter->getKey() }}" />
</div>
10 changes: 10 additions & 0 deletions src/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,14 @@
{
return $this->filtersAreEnabled() && $this->filterPillsAreEnabled() && $this->hasAppliedVisibleFiltersForPills();
}

#[On('livewireArrayFilterUpdateValues')]

Check warning on line 349 in src/Traits/Helpers/FilterHelpers.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Helpers/FilterHelpers.php#L349

Added line #L349 was not covered by tests
public function updateLivewireArrayFilterValues(string $filterKey, string $tableName, array $values): void
{
if ($this->tableName == $tableName) {
$filter = $this->getFilterByKey($filterKey);
$filter->options($values);

Check warning on line 354 in src/Traits/Helpers/FilterHelpers.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Helpers/FilterHelpers.php#L352-L354

Added lines #L352 - L354 were not covered by tests
}

}
}
58 changes: 58 additions & 0 deletions src/Views/Filters/LivewireComponentArrayFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Filters;

use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Views\Filter;
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasWireables;
use Rappasoft\LaravelLivewireTables\Views\Traits\Filters\{HasOptions,IsArrayFilter, IsLivewireComponentFilter};

class LivewireComponentArrayFilter extends Filter
{
use HasWireables;
use IsArrayFilter;
use HasOptions;
use IsLivewireComponentFilter;

public string $wireMethod = 'blur';

protected string $view = 'livewire-tables::components.tools.filters.livewire-component-array-filter';

public function validate(array $value): array|bool

Check warning on line 21 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L21

Added line #L21 was not covered by tests
{

return $value;

Check warning on line 24 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L24

Added line #L24 was not covered by tests
}

public function isEmpty(array $value = []): bool

Check warning on line 27 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L27

Added line #L27 was not covered by tests
{
return empty($value);

Check warning on line 29 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L29

Added line #L29 was not covered by tests
}

/**
* Gets the Default Value for this Filter via the Component
*/
public function getFilterDefaultValue(): ?string

Check warning on line 35 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L35

Added line #L35 was not covered by tests
{
return $this->filterDefaultValue ?? null;

Check warning on line 37 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L37

Added line #L37 was not covered by tests
}

public function getFilterPillValue($value): array|string|bool|null

Check warning on line 40 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L40

Added line #L40 was not covered by tests
{
$values = [];
foreach ($value as $key => $item) {

Check warning on line 43 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L42-L43

Added lines #L42 - L43 were not covered by tests

$found = $this->getCustomFilterPillValue($item) ?? ($this->options[$item] ?? $item);
if ($found) {
$values[] = $found;

Check warning on line 47 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L45-L47

Added lines #L45 - L47 were not covered by tests
}
}

return $values;

Check warning on line 51 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L51

Added line #L51 was not covered by tests
}

public function getKeys(): array

Check warning on line 54 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L54

Added line #L54 was not covered by tests
{
return array_keys($this->options ?? []);

Check warning on line 56 in src/Views/Filters/LivewireComponentArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/LivewireComponentArrayFilter.php#L56

Added line #L56 was not covered by tests
}
}
38 changes: 2 additions & 36 deletions src/Views/Filters/LivewireComponentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Views\Filter;
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasWireables;
use Rappasoft\LaravelLivewireTables\Views\Traits\Filters\IsLivewireComponentFilter;

class LivewireComponentFilter extends Filter
{
use HasWireables;
use IsLivewireComponentFilter;

public string $wireMethod = 'blur';

protected string $view = 'livewire-tables::components.tools.filters.livewire-component-filter';

public string $livewireComponent = '';

public function validate(string $value): string|bool
{
return $value;
Expand All @@ -33,38 +33,4 @@ public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function setLivewireComponent(string $livewireComponent): self
{

$class = '\\'.config('livewire.class_namespace').'\\'.collect(str($livewireComponent)->explode('.'))->map(fn ($segment) => (string) str($segment)->studly())->join('\\');

if (! class_exists($class)) {
throw new DataTableConfigurationException('You must specify a valid path to your Livewire Component Filter.');
}

if (! is_subclass_of($class, \Livewire\Component::class)) {
throw new DataTableConfigurationException('Your Livewire Component Filter MUST Extend Livewire\Component.');
}

$this->livewireComponent = $livewireComponent;

return $this;
}

public function getLivewireComponent(): string
{
return $this->livewireComponent ?? '';
}

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
if ($this->livewireComponent == '') {
throw new DataTableConfigurationException('You must specify a valid path to your Livewire Component Filter.');
}

return view($this->getViewPath(), $this->getFilterDisplayData())->with([
'livewireComponent' => $this->livewireComponent,
]);
}
}
2 changes: 1 addition & 1 deletion src/Views/Traits/Filters/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function options(array $options = []): self

public function getOptions(): array
{
return $this->options ?? $this->options = config($this->optionsPath, []);
return $this->options ?? $this->options = (property_exists($this, 'optionsPath') ? config($this->optionsPath, []) : []);
}

public function getKeys(): array
Expand Down
44 changes: 44 additions & 0 deletions src/Views/Traits/Filters/IsLivewireComponentFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Filters;

use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;

trait IsLivewireComponentFilter
{
public string $livewireComponent = '';

public function setLivewireComponent(string $livewireComponent): self

Check warning on line 11 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L11

Added line #L11 was not covered by tests
{

$class = '\\'.config('livewire.class_namespace').'\\'.collect(str($livewireComponent)->explode('.'))->map(fn ($segment) => (string) str($segment)->studly())->join('\\');

Check warning on line 14 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L14

Added line #L14 was not covered by tests

if (! class_exists($class)) {
throw new DataTableConfigurationException('You must specify a valid path to your Livewire Component Filter.');

Check warning on line 17 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L16-L17

Added lines #L16 - L17 were not covered by tests
}

if (! is_subclass_of($class, \Livewire\Component::class)) {
throw new DataTableConfigurationException('Your Livewire Component Filter MUST Extend Livewire\Component.');

Check warning on line 21 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L20-L21

Added lines #L20 - L21 were not covered by tests
}

$this->livewireComponent = $livewireComponent;

Check warning on line 24 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L24

Added line #L24 was not covered by tests

return $this;

Check warning on line 26 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L26

Added line #L26 was not covered by tests
}

public function getLivewireComponent(): string

Check warning on line 29 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L29

Added line #L29 was not covered by tests
{
return $this->livewireComponent ?? '';

Check warning on line 31 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L31

Added line #L31 was not covered by tests
}

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory

Check warning on line 34 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L34

Added line #L34 was not covered by tests
{
if ($this->livewireComponent == '') {
throw new DataTableConfigurationException('You must specify a valid path to your Livewire Component Filter.');

Check warning on line 37 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L36-L37

Added lines #L36 - L37 were not covered by tests
}

return view($this->getViewPath(), $this->getFilterDisplayData())->with([
'livewireComponent' => $this->livewireComponent,
]);

Check warning on line 42 in src/Views/Traits/Filters/IsLivewireComponentFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/Filters/IsLivewireComponentFilter.php#L40-L42

Added lines #L40 - L42 were not covered by tests
}
}
73 changes: 73 additions & 0 deletions src/Views/Traits/IsExternalArrayFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits;

use Livewire\Attributes\{Modelable,On,Renderless};

trait IsExternalArrayFilter
{
#[Modelable]
public array $value = [];

public string $filterKey = '';

public string $tableName = '';

public string $tableComponent = '';

protected bool $needsUpdating = false;

protected array $returnValues = [];

public array $selectedItems = [];

public array $selectOptions = [];

#[On('filter-was-set')]

Check warning on line 26 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L26

Added line #L26 was not covered by tests
public function setFilterValues(string $tableName, string $filterKey, array $value): void
{
if ($tableName == $this->tableName && $filterKey == $this->filterKey && $this->selectedItems != $value) {
$this->selectedItems = $value;
$this->needsUpdating = false;

Check warning on line 31 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L29-L31

Added lines #L29 - L31 were not covered by tests

}
}

protected function clearFilter() {}

Check warning on line 36 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L36

Added line #L36 was not covered by tests

#[Renderless]

Check warning on line 38 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L38

Added line #L38 was not covered by tests
public function updatedSelectedItems(string $value): void
{
if (! $this->needsUpdating) {
$this->needsUpdating = true;

Check warning on line 42 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L41-L42

Added lines #L41 - L42 were not covered by tests

}
}

#[Renderless]

Check warning on line 47 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L47

Added line #L47 was not covered by tests
protected function sendUpdateDispatch(array $returnValues): void
{
if ($this->needsUpdating) {
if (! empty($returnValues)) {
$this->value = array_keys($returnValues);

Check warning on line 52 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L50-L52

Added lines #L50 - L52 were not covered by tests
} else {
$this->value = [];

Check warning on line 54 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L54

Added line #L54 was not covered by tests
}
$this->dispatch('livewireArrayFilterUpdateValues', tableName: $this->tableName, filterKey: $this->filterKey, values: $returnValues)->to($this->tableComponent);

Check warning on line 56 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L56

Added line #L56 was not covered by tests

}
}

#[Renderless]

Check warning on line 61 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L61

Added line #L61 was not covered by tests
public function renderingIsExternalArrayFilter(): void
{
$returnValues = [];

Check warning on line 64 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L64

Added line #L64 was not covered by tests

if ($this->needsUpdating == true && ! empty($this->selectedItems)) {
foreach ($this->selectedItems as $selectedItem) {
$returnValues[$selectedItem] = $this->selectOptions[$selectedItem] ?? 'Unknown';

Check warning on line 68 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L66-L68

Added lines #L66 - L68 were not covered by tests
}
$this->sendUpdateDispatch($returnValues);

Check warning on line 70 in src/Views/Traits/IsExternalArrayFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Traits/IsExternalArrayFilter.php#L70

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