From b6890e920324c7366533b346d14d3c3323bdfb26 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 18 Aug 2024 03:10:38 +0100 Subject: [PATCH] Add dispatch on updated filter components (#1861) * Add additional dispatch * Add FilterApplied Event * Add Filter Event Dispatch Tests --------- Co-authored-by: lrljoe --- CHANGELOG.md | 4 ++++ src/Traits/WithFilters.php | 5 +++++ tests/Traits/Visuals/FilterVisualsTest.php | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55abcde31..5843df9ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-livewire-tables` will be documented in this file +## UNRELEASED +### New Features +- Add an event dispatch for Filter Was Set when filterComponents is updated by @lrljoe + ## [v3.4.7] - 2024-08-18 ### Bug Fixes - Correct a locked property that is entangled in js, and add comments around some key properties to prevent reoccurrence by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1859 diff --git a/src/Traits/WithFilters.php b/src/Traits/WithFilters.php index d843f2a71..49c91da8c 100644 --- a/src/Traits/WithFilters.php +++ b/src/Traits/WithFilters.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Livewire\Attributes\Locked; +use Rappasoft\LaravelLivewireTables\Events\FilterApplied; use Rappasoft\LaravelLivewireTables\Traits\Configuration\FilterConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\FilterHelpers; @@ -103,6 +104,10 @@ public function updatedFilterComponents(string|array|null $value, string $filter } elseif ($filter) { $this->callHook('filterUpdated', ['filter' => $filter->getKey(), 'value' => $value]); $this->callTraitHook('filterUpdated', ['filter' => $filter->getKey(), 'value' => $value]); + if ($this->getEventStatusFilterApplied() && $filter->getKey() != null && $value != null) { + event(new FilterApplied($this->getTableName(), $filter->getKey(), $value)); + } + $this->dispatch('filter-was-set', tableName: $this->getTableName(), filterKey: $filter->getKey(), value: $value); } } diff --git a/tests/Traits/Visuals/FilterVisualsTest.php b/tests/Traits/Visuals/FilterVisualsTest.php index 221ee7836..5b34da945 100644 --- a/tests/Traits/Visuals/FilterVisualsTest.php +++ b/tests/Traits/Visuals/FilterVisualsTest.php @@ -49,6 +49,20 @@ public function test_filter_pills_show_when_enabled(): void ->assertSee('Applied Filters'); } + public function test_event_dispatched_when_filterComponents_set(): void + { + Livewire::test(PetsTable::class) + ->set('filterComponents.breed', [1]) + ->assertDispatched('filter-was-set'); + } + + public function test_event_dispatched_when_setFilter_dispatched(): void + { + Livewire::test(PetsTable::class) + ->dispatch('setFilter', filterKey: 'breed', value: [1]) + ->assertDispatched('filter-was-set'); + } + public function test_filter_pills_show_when_visible(): void { Livewire::test(PetsTable::class)