Skip to content

Commit

Permalink
Allow emission of events with parameters and no target. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev authored Apr 15, 2021
1 parent 7b964f5 commit 22e8e94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ModalComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public function closeModalWithEvents(array $events): void
private function emitModalEvents(array $events): void
{
foreach ($events as $component => $event) {
if (is_array($event)) {
[$event, $params] = $event;
}

if (is_numeric($component)) {
$this->emit($event);
$this->emit($event, ...$params ?? []);
} else {
if (is_array($event)) {
[$event, $params] = $event;
}

$this->emitTo($component, $event, ...$params ?? []);
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/LivewireModalComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ public function testModalSkipping(): void

public function testModalEventEmitting(): void
{
Livewire::test(DemoModal::class)
->call('closeModalWithEvents', [
'someEvent',
])
->assertEmitted('someEvent');

Livewire::test(DemoModal::class)
->call('closeModalWithEvents', [
DemoModal::getName() => 'someEvent',
])
->assertEmitted('someEvent');

Livewire::test(DemoModal::class)
->call('closeModalWithEvents', [
['someEventWithParams', ['param1', 'param2']],
])
->assertEmitted('someEventWithParams', 'param1', 'param2');

Livewire::test(DemoModal::class)
->call('closeModalWithEvents', [
DemoModal::getName() => ['someEventWithParams', ['param1', 'param2']],
Expand Down

0 comments on commit 22e8e94

Please sign in to comment.