From 4dd07161ea9cfe0bd4c4420acad288aed6575d15 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Thu, 21 Dec 2023 01:31:01 +0000 Subject: [PATCH] BETA - Livewire Component Filter (#1604) * Initial Commit --------- Co-authored-by: lrljoe --- .../filters-livewire-component.md | 102 ++++++++++++++++++ .../livewire-component-filter.blade.php | 5 + src/Views/Filters/LivewireComponentFilter.php | 65 +++++++++++ src/Views/Traits/IsExternalFilter.php | 13 +++ 4 files changed, 185 insertions(+) create mode 100644 docs/filter-types/filters-livewire-component.md create mode 100644 resources/views/components/tools/filters/livewire-component-filter.blade.php create mode 100644 src/Views/Filters/LivewireComponentFilter.php create mode 100644 src/Views/Traits/IsExternalFilter.php diff --git a/docs/filter-types/filters-livewire-component.md b/docs/filter-types/filters-livewire-component.md new file mode 100644 index 000000000..d11d337f9 --- /dev/null +++ b/docs/filter-types/filters-livewire-component.md @@ -0,0 +1,102 @@ +--- +title: Livewire Custom Filter (Beta) +weight: 11 +--- + +## Livewire Custom Filter + +**IN BETA** +This feature is currently in beta, and use in production is not recommended. + +### Usage +This allows you to use a child/nested Livewire Component in place of the existing Filters, giving you more control over the look/feel/behaviour of a filter. + +To use a LivewireComponentFilter, you must include it in your namespace: +```php +use Rappasoft\LaravelLivewireTables\Views\Filters\LivewireComponentFilter; +``` + +When creating a filter: +- Specify a unique name +- Set the path to a valid Livewire Component +- Define a filter() callback to define how the returned value will be used. + +```php + public function filters(): array + { + return [ + LivewireComponentFilter::make('My External Filter') + ->setLivewireComponent('my-test-external-filter') + ->filter(function (Builder $builder, string $value) { + $builder->where('name', 'like', '%'.$value.'%'); + }), + ]; + } +``` + +### Configuring Your Livewire Filter Component + +A basic example (replicating the Text Filter) looks like the below, note the usage of the "IsExternalFilter" trait. +```php + +
+ +
+ +``` + +### Important Notes For The livewire-component-filter.blade.php +- It is **strongly** recommmended not to publish, nor update this file, while this feature is in beta, as it is subject to change at short notice, which may lead to breaking changes. \ No newline at end of file diff --git a/resources/views/components/tools/filters/livewire-component-filter.blade.php b/resources/views/components/tools/filters/livewire-component-filter.blade.php new file mode 100644 index 000000000..89b407133 --- /dev/null +++ b/resources/views/components/tools/filters/livewire-component-filter.blade.php @@ -0,0 +1,5 @@ +
+ + + +
diff --git a/src/Views/Filters/LivewireComponentFilter.php b/src/Views/Filters/LivewireComponentFilter.php new file mode 100644 index 000000000..6a5a31fc7 --- /dev/null +++ b/src/Views/Filters/LivewireComponentFilter.php @@ -0,0 +1,65 @@ +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, + ]); + } +} diff --git a/src/Views/Traits/IsExternalFilter.php b/src/Views/Traits/IsExternalFilter.php new file mode 100644 index 000000000..b2073c126 --- /dev/null +++ b/src/Views/Traits/IsExternalFilter.php @@ -0,0 +1,13 @@ +