Handling dispatch events with named parameters in Filament Action
Hi đ
I'm trying to dispatch a Wire Element Pro event to open a slide-over from within a reusable Filament action. While this works seamlessly when the action is defined within a Livewire component, it doesn't seem to work as expected in a Filament action.
Hereâs the code that works well in a Livewire component:
However, when I try to achieve the same in a Filament action extended class, I encounter issues. Specifically, I believe the
dispatch
method from the CanDispatchEvent
trait differs from Livewire's HandlesEvent
logic, causing the following problems:
- Named Parameters: The first approach using named parameters (component: 'settings.team.switch-team-slide-over'
) results in an "Unknown named parameter $component" error.
- Array Syntax: The 2nd approach using an associative array doesn't throw an error, but it fails to trigger the slide-over event as intended.
I also realise that $this->js()
is not usable in this context either.
Is there a different method or workaround that would allow me to trigger the slide-over event in this context? đSolution:Jump to solution
Actions arenât livewire components so â$thisâ is the action in this context. You can inject $livewire into the action callback and call $livewire->dispatch() etc.
7 Replies
Solution
Actions arenât livewire components so â$thisâ is the action in this context. You can inject $livewire into the action callback and call $livewire->dispatch() etc.
That's a great idea! And thanks for all your work with Filament.
Quick question, how would you deal with component injection inside a Livewire component?
According to the documentation I just need to pass
Component $livewire
.
I end up with something like:
Filament\Forms\ComponentContainer::make(): Argument #1 ($livewire) must be of type Filament\Forms\Contracts\HasForms, App\Livewire\Header\Dropdown\SwitchTeam given
Am I missing something here?
Any reason you use WireElements over Filament Actions here?
To be honest, I am slowly moving away from wire elements to filament elements, but I have to work with a bit of both at the moment.
I end up with something like: Filament\Forms\ComponentContainer::make(): Argument #1 ($livewire) must be of type Filament\Forms\Contracts\HasForms, App\Livewire\Header\Dropdown\SwitchTeam givenWhere is this coming from? Can you share the stack trace.
Yes, of course!
The trace is attached. I also took the time to create a minimal repo. Just go to
/test
to reproduce it.
Let me know if I can do anything to assist.Never mind! After changing the file several times, I forgot to put back
implements HasActions, HasForms
, which caused the problem.
Thanks for your time both of you! â€ïž