diff --git a/README.md b/README.md index 8dca764..220d4a3 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,12 @@ Please review the changes and ensure they follow the new convention set by Livew <-- Before --> - + <-- Before --> - + ``` The old component name is being deprecated. Replace `@livewire('livewire-ui-modal')` with `@livewire('wire-elements-modal')`. @@ -103,13 +103,13 @@ To open a modal you will need to dispatch an event. To open the `EditUser` modal ```html - + - + - + ``` ## Passing parameters @@ -117,16 +117,16 @@ To open the `EditUser` modal for a specific user we can pass the user id: ```html - + - + - + - + ``` The parameters are injected into the modal component and the model will be automatically fetched from the database if the type is defined: @@ -167,7 +167,7 @@ From an existing modal you can use the exact same event and a child modal will b - + ``` ## Closing a (child) modal diff --git a/src/Modal.php b/src/Modal.php index 9b14c76..fd4b5ce 100644 --- a/src/Modal.php +++ b/src/Modal.php @@ -27,17 +27,17 @@ 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())) @@ -45,7 +45,7 @@ public function openModal($component, $arguments = [], $modalAttributes = []): v $this->components[$id] = [ - 'name' => $component, + 'name' => $modalComponent, 'attributes' => $arguments, // Deprecated 'arguments' => $arguments, 'modalAttributes' => array_merge([ diff --git a/src/WireElementsModalUpgrade.php b/src/WireElementsModalUpgrade.php index 55861b0..ba369de 100644 --- a/src/WireElementsModalUpgrade.php +++ b/src/WireElementsModalUpgrade.php @@ -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' ); @@ -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'] ); @@ -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 })"; }, diff --git a/tests/LivewireModalTest.php b/tests/LivewireModalTest.php index 76a3ca5..3f15548 100644 --- a/tests/LivewireModalTest.php +++ b/tests/LivewireModalTest.php @@ -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 => [ @@ -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, @@ -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') @@ -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'); } }