Skip to content
Open
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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Please review the changes and ensure they follow the new convention set by Livew
<-- Before -->
<button wire:click="$emit('openModal', 'users')">Show Users</button>
<!-- After -->
<button wire:click="$dispatch('openModal', {component: 'users'})">Show Users</button>
<button wire:click="$dispatch('openModal', {modalComponent: 'users'})">Show Users</button>

<-- Before -->
<button wire:click="$emit('openModal', 'edit-user', {user: 5})">Edit User</button>
<!-- After -->
<button wire:click="$dispatch('openModal', {component: 'edit-user', arguments: {user: 5}})">Edit User</button>
<button wire:click="$dispatch('openModal', {modalComponent: 'edit-user', arguments: {user: 5}})">Edit User</button>
```

The old component name is being deprecated. Replace `@livewire('livewire-ui-modal')` with `@livewire('wire-elements-modal')`.
Expand Down Expand Up @@ -103,30 +103,30 @@ To open a modal you will need to dispatch an event. To open the `EditUser` modal

```html
<!-- Outside of any Livewire component -->
<button onclick="Livewire.dispatch('openModal', { component: 'edit-user' })">Edit User</button>
<button onclick="Livewire.dispatch('openModal', { modalComponent: 'edit-user' })">Edit User</button>

<!-- Inside existing Livewire component -->
<button wire:click="$dispatch('openModal', { component: 'edit-user' })">Edit User</button>
<button wire:click="$dispatch('openModal', { modalComponent: 'edit-user' })">Edit User</button>

<!-- Taking namespace into account for component Admin/Actions/EditUser -->
<button wire:click="$dispatch('openModal', { component: 'admin.actions.edit-user' })">Edit User</button>
<button wire:click="$dispatch('openModal', { modalComponent: 'admin.actions.edit-user' })">Edit User</button>
```

## Passing parameters
To open the `EditUser` modal for a specific user we can pass the user id:

```html
<!-- Outside of any Livewire component -->
<button onclick="Livewire.dispatch('openModal', { component: 'edit-user', arguments: { user: {{ $user->id }} }})">Edit User</button>
<button onclick="Livewire.dispatch('openModal', { modalComponent: 'edit-user', arguments: { user: {{ $user->id }} }})">Edit User</button>

<!-- Inside existing Livewire component -->
<button wire:click="$dispatch('openModal', { component: 'edit-user', arguments: { user: {{ $user->id }} }})">Edit User</button>
<button wire:click="$dispatch('openModal', { modalComponent: 'edit-user', arguments: { user: {{ $user->id }} }})">Edit User</button>

<!-- If you use a different primaryKey (e.g. email), adjust accordingly -->
<button wire:click="$dispatch('openModal', { component: 'edit-user', arguments: { user: {{ $user->email }} }})">Edit User</button>
<button wire:click="$dispatch('openModal', { modalComponent: 'edit-user', arguments: { user: {{ $user->email }} }})">Edit User</button>

<!-- Example of passing multiple arguments -->
<button wire:click="$dispatch('openModal', { component: 'edit-user', arguments: { user: {{ $user->id }}, advancedMode: true }})">Edit User</button>
<button wire:click="$dispatch('openModal', { modalComponent: 'edit-user', arguments: { user: {{ $user->id }}, advancedMode: true }})">Edit User</button>
```

The parameters are injected into the modal component and the model will be automatically fetched from the database if the type is defined:
Expand Down Expand Up @@ -167,7 +167,7 @@ From an existing modal you can use the exact same event and a child modal will b
<!-- Edit User Modal -->

<!-- Edit Form -->
<button wire:click="$dispatch('openModal', { component: 'delete-user', arguments: { user: {{ $user->id }} }})">Delete User</button>
<button wire:click="$dispatch('openModal', { modalComponent: 'delete-user', arguments: { user: {{ $user->id }} }})">Delete User</button>
```

## Closing a (child) modal
Expand Down
8 changes: 4 additions & 4 deletions src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ public function resetState(): void
$this->activeComponent = null;
}

public function openModal($component, $arguments = [], $modalAttributes = []): void
public function openModal($modalComponent, $arguments = [], $modalAttributes = []): void
{
$requiredInterface = \LivewireUI\Modal\Contracts\ModalComponent::class;
$componentClass = $this->resolveComponentClass($component);
$componentClass = $this->resolveComponentClass($modalComponent);
$reflect = new ReflectionClass($componentClass);

if ($reflect->implementsInterface($requiredInterface) === false) {
throw new Exception("[{$componentClass}] does not implement [{$requiredInterface}] interface.");
}

$id = md5($component.serialize($arguments));
$id = md5($modalComponent.serialize($arguments));

$arguments = collect($arguments)
->merge($this->resolveComponentProps($arguments, new $componentClass()))
->all();


$this->components[$id] = [
'name' => $component,
'name' => $modalComponent,
'attributes' => $arguments, // Deprecated
'arguments' => $arguments,
'modalAttributes' => array_merge([
Expand Down
14 changes: 7 additions & 7 deletions src/WireElementsModalUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public function handle(UpgradeCommand $console, \Closure $next)
console: $console,
title: 'The $dispatch helper expects named arguments.',
before: '$dispatch(\'openModal\', \'component-name\', {user: 1})',
after: '$dispatch(\'openModal\', {component: \'component-name\', arguments: {user: 1}})',
after: '$dispatch(\'openModal\', {modalComponent: \'component-name\', arguments: {user: 1}})',
pattern: '/\$(?:dispatch|emit)\(\'openModal\'(?:,\s?)([^,|\)]*)(?:,\s?)?((?:(?:.|\s)*?).*)\)/',
replacement: function($matches) {
$component = $matches[1];
$arguments = $matches[2];
if (empty($arguments)) {
return "\$dispatch('openModal', { component: $component })";
return "\$dispatch('openModal', { modalComponent: $component })";
}
return "\$dispatch('openModal', { component: $component, arguments: $arguments })";
return "\$dispatch('openModal', { modalComponent: $component, arguments: $arguments })";
},
directories: 'resources'
);
Expand All @@ -30,15 +30,15 @@ public function handle(UpgradeCommand $console, \Closure $next)
console: $console,
title: '$this->dispatch now expects named arguments.',
before: '$this->dispatch(\'openModal\', \'component-name\', [\'user\' => 1])',
after: '$this->dispatch(\'openModal\', component: \'component-name\', arguments: [\'user\' => 1])',
after: '$this->dispatch(\'openModal\', modalComponent: \'component-name\', arguments: [\'user\' => 1])',
pattern: '/\$this->(?:dispatch|emit)\(\'openModal\'(?:,\s?)([^,|\)]*)(?:,\s?)?((?:(?:.|\s)*?).*)\)/',
replacement: function($matches) {
$component = $matches[1];
$arguments = $matches[2];
if (empty($arguments)) {
return "\$this->dispatch('openModal', component: $component)";
return "\$this->dispatch('openModal', modalComponent: $component)";
}
return "\$this->dispatch('openModal', component: $component, arguments: $arguments)";
return "\$this->dispatch('openModal', modalComponent: $component, arguments: $arguments)";
},
directories: ['app', 'tests']
);
Expand All @@ -53,7 +53,7 @@ public function handle(UpgradeCommand $console, \Closure $next)
$component = $matches[1];
$arguments = $matches[2];
if (empty($arguments)) {
return "Livewire.dispatch('openModal', { component: $component })";
return "Livewire.dispatch('openModal', { modalComponent: $component })";
}
return "Livewire.dispatch('openModal', {component: $component, arguments: $arguments })";
},
Expand Down
8 changes: 4 additions & 4 deletions tests/LivewireModalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testOpenModalEventListener(): void
$id = md5($component.serialize($arguments));

Livewire::test(Modal::class)
->dispatch('openModal', component: $component, arguments: $arguments, modalAttributes: $modalAttributes)
->dispatch('openModal', modalComponent: $component, arguments: $arguments, modalAttributes: $modalAttributes)
// Verify component is added to $components
->assertSet('components', [
$id => [
Expand Down Expand Up @@ -54,7 +54,7 @@ public function testDestroyComponentEventListener(): void
$id = md5($component.serialize($arguments));

Livewire::test(Modal::class)
->dispatch('openModal', component: $component, arguments: $arguments, modalAttributes: $modalAttributes)
->dispatch('openModal', modalComponent: $component, arguments: $arguments, modalAttributes: $modalAttributes)
->assertSet('components', [
$id => [
'name' => $component,
Expand All @@ -72,7 +72,7 @@ public function testModalReset(): void
Livewire::component('demo-modal', DemoModal::class);

Livewire::test(Modal::class)
->dispatch('openModal', component: 'demo-modal', arguments: ['message' => 'Test'])
->dispatch('openModal', modalComponent: 'demo-modal', arguments: ['message' => 'Test'])
->assertNotSet('activeComponent', null)
->assertNotSet('components', [])
->call('resetState')
Expand All @@ -88,6 +88,6 @@ public function testIfExceptionIsThrownIfModalDoesNotImplementContract(): void
$this->expectExceptionMessage("[{$component}] does not implement [LivewireUI\Modal\Contracts\ModalComponent] interface.");

Livewire::component('invalid-modal', $component);
Livewire::test(Modal::class)->dispatch('openModal', component: 'invalid-modal');
Livewire::test(Modal::class)->dispatch('openModal', modalComponent: 'invalid-modal');
}
}