Skip to content

Commit

Permalink
Add dispatch on updated filter components (#1861)
Browse files Browse the repository at this point in the history
* Add additional dispatch

* Add FilterApplied Event

* Add Filter Event Dispatch Tests

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored Aug 18, 2024
1 parent b3137d9 commit b6890e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 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 Illuminate\Support\Collection;
use Livewire\Attributes\Locked;
use Rappasoft\LaravelLivewireTables\Events\FilterApplied;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\FilterConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\FilterHelpers;

Expand Down Expand Up @@ -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);

}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Traits/Visuals/FilterVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b6890e9

Please sign in to comment.