Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/close modal hook #14942

Open
wants to merge 5 commits into
base: 4.x
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions packages/actions/docs/03-modals.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ use Filament\Support\View\Components\Modal;
Modal::closedByEscaping(false);
```

## Hooks for when action modal is closed

You may want to execute some code before or after the modal is closed.

```php
Action::make('updateAuthor')
->beforeClose(function () {
// Do something before the modal is closed.
})
->afterClose(function () {
// Do something after the modal is closed.
})
```

## Hiding the modal close button

By default, modals have a close button in the top right corner. If you wish to hide the close button, you can use the `modalCloseButton(false)` method:
Expand Down
34 changes: 34 additions & 0 deletions packages/actions/src/Concerns/HasLifecycleHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ trait HasLifecycleHooks

protected ?Closure $after = null;

protected ?Closure $beforeClose = null;

protected ?Closure $afterClose = null;

protected ?Closure $beforeFormFilled = null;

protected ?Closure $afterFormFilled = null;
Expand All @@ -35,6 +39,20 @@ public function after(?Closure $callback): static
return $this;
}

public function beforeClose(?Closure $callback): static
{
$this->beforeClose = $callback;

return $this;
}

public function afterClose(?Closure $callback): static
{
$this->afterClose = $callback;

return $this;
}

public function beforeFormFilled(?Closure $callback): static
{
$this->beforeFormFilled = $callback;
Expand Down Expand Up @@ -79,6 +97,22 @@ public function callAfter(): mixed
}
}

public function callBeforeClose(): mixed
{
Event::dispatch(ActionCalling::class, $this);

return $this->evaluate($this->beforeClose);
}

public function callAfterClose(): mixed
{
try {
return $this->evaluate($this->afterClose);
} finally {
Event::dispatch(ActionCalled::class, $this);
}
}

public function callBeforeFormFilled(): mixed
{
return $this->evaluate($this->beforeFormFilled);
Expand Down
3 changes: 2 additions & 1 deletion packages/infolists/src/Components/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Filament\Actions\Action;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Components\Concerns\CanOpenUrl;
use Filament\Schemas\Components\Decorations\Layouts\DecorationsLayout;
use Filament\Support\Concerns\HasAlignment;
use Filament\Support\Concerns\HasPlaceholder;
Expand All @@ -14,11 +15,11 @@

class Entry extends Component
{
use CanOpenUrl;
use Concerns\HasExtraEntryWrapperAttributes;
use Concerns\HasHelperText;
use Concerns\HasHint;
use Concerns\HasName;
use \Filament\Schemas\Components\Concerns\CanOpenUrl;
use HasAlignment;
use HasPlaceholder;
use HasTooltip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Filament\Facades\Filament;
use Filament\GlobalSearch\GlobalSearchResults;
use Filament\GlobalSearch\Providers;

class DefaultGlobalSearchProvider implements Providers\Contracts\GlobalSearchProvider
class DefaultGlobalSearchProvider implements Contracts\GlobalSearchProvider
{
public function getResults(string $query): ?GlobalSearchResults
{
Expand Down
2 changes: 1 addition & 1 deletion packages/tables/src/Columns/ToggleColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function toEmbeddedHtml(): string
<div aria-hidden="true">
<?= generate_icon_html(
$onIcon,
attributes: (new ComponentattributeBag)->merge(['x-cloak' => true], escape: false),
attributes: (new ComponentAttributeBag)->merge(['x-cloak' => true], escape: false),
)?->toHtml() ?>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
});

it('can render the challenge form after valid login credentials are successfully used', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand Down Expand Up @@ -52,7 +52,7 @@
});

it('will authenticate the user after a valid challenge code is used', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand All @@ -79,7 +79,7 @@
});

it('can resend the code to the user', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand Down Expand Up @@ -140,7 +140,7 @@
});

it('will not authenticate the user when an invalid challenge code is used', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand Down Expand Up @@ -171,7 +171,7 @@
});

test('challenge codes are required', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand Down Expand Up @@ -200,7 +200,7 @@
});

test('challenge codes must be numeric', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand Down Expand Up @@ -229,7 +229,7 @@
});

test('challenge codes must be 6 digits', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$userToAuthenticate = User::factory()
->hasEmailCodeAuthentication()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
});

it('can remove authentication when valid challenge code is used', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -76,7 +76,7 @@
});

it('will not remove authentication when an invalid code is used', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -128,7 +128,7 @@
});

test('codes must be 6 digits', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
});

it('can generate a secret when the action is mounted', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$livewire = livewire(EditProfile::class)
->mountAction(TestAction::make('setUpEmailCodeAuthentication')
Expand Down Expand Up @@ -58,7 +58,7 @@
});

it('can save the secret to the user when the action is submitted', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -102,7 +102,7 @@
});

it('will not set up authentication when an invalid code is used', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -159,7 +159,7 @@
});

test('codes must be 6 digits', function () {
$emailCodeAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$emailCodeAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
});

it('can generate new recovery codes when valid challenge code is used', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -114,7 +114,7 @@
});

it('will not generate new recovery codes when an invalid code is used', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -162,7 +162,7 @@
});

test('codes must be 6 digits', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
beforeEach(function () {
Filament::setCurrentPanel('google-two-factor-authentication');

$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$this->recoveryCodes = $googleTwoFactorAuthentication->generateRecoveryCodes();

Expand All @@ -26,7 +26,7 @@
});

it('can remove authentication when valid challenge code is used', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -95,7 +95,7 @@
});

it('will not remove authentication when an invalid code is used', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -163,7 +163,7 @@
});

test('codes must be 6 digits', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
});

it('can save the secret and recovery codes to the user when the action is submitted', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -104,7 +104,7 @@
});

it('will not set up authentication when an invalid code is used', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down Expand Up @@ -177,7 +177,7 @@
});

test('codes must be 6 digits', function () {
$googleTwoFactorAuthentication = Arr::first(filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());
$googleTwoFactorAuthentication = Arr::first(Filament::getCurrentPanel()->getMultiFactorAuthenticationProviders());

$user = auth()->user();

Expand Down
Loading
Loading