FilamentF
Filament•2y ago
ChrisR

How to know when a modal, opened via a Table action, has been closed?

I need to dispatch an event when a modal is closed. Right now I can do it when the "submit" button is clicked with

->action(function (array $data, Order $order) {
    $this->dispatch('order-scanner-modal-closed', true);
})

But I can not figure out how to do this on cancel. I've tried overriding by way of modalFooterActions with something like this:

->modalFooterActions([
    Action::make('submit')
        ->label('Link item to inventory')
        ->action(function (Action $action) {
            $this->dispatch('order-scanner-modal-closed', true);
        }),
    Action::make('cancel')
        ->label('Cancel')
        ->action(function () {
            $this->dispatch('order-scanner-modal-closed', false);
        }),
])


This feels like progress because the correct code gets executed when the respective button gets clicked; however, it also feels like I'm moving backwards, because the modal doesn't automatically close like it would do out of the box using just action. So I solve one problem but encounter a new 🙂

I thought that modalCancelAction would be the way, but that is getting executed when the modal is being rendered so I must be misunderstanding its intention.
Solution
Ok. I think it’s because you’re trying to override the existing actions without actually replacing the existing submit and close that are already cached. Try this approach instead: https://filamentphp.com/docs/3.x/actions/modals#modifying-a-default-modal-footer-action-button
Was this page helpful?