generated from rawilk/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from rawilk/cleanup-actions
- Loading branch information
Showing
7 changed files
with
167 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\FilamentPasswordInput\Actions; | ||
|
||
use Filament\Forms\Components\Actions\Action; | ||
use Filament\Forms\Components\Component; | ||
use Filament\Support\Facades\FilamentIcon; | ||
use Illuminate\Support\Js; | ||
|
||
class CopyToClipboardAction extends Action | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->label(__('filament-password-input::password.actions.copy.tooltip')); | ||
|
||
$this->icon(FilamentIcon::resolve('filament-password-input::copy') ?? 'heroicon-m-clipboard'); | ||
|
||
$this->color('gray'); | ||
|
||
$this->alpineClickHandler(function (Component $component) { | ||
$copyDuration = Js::from( | ||
rescue( | ||
callback: fn () => $component->getCopyMessageDuration(), | ||
rescue: fn () => 2000, | ||
report: false, | ||
) | ||
); | ||
|
||
$copyMessage = Js::from( | ||
rescue( | ||
callback: fn () => $component->getCopyMessage(), | ||
rescue: fn () => __('filament::components/copyable.messages.copied'), | ||
report: false, | ||
) | ||
); | ||
|
||
return <<<JS | ||
const text = \$wire.get('{$component->getStatePath()}'); | ||
window.navigator.clipboard.writeText(text); | ||
\$tooltip({$copyMessage}, { theme: \$store.theme, timeout: {$copyDuration} }); | ||
JS; | ||
}); | ||
} | ||
|
||
public static function getDefaultName(): ?string | ||
{ | ||
return 'copyToClipboard'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\FilamentPasswordInput\Actions; | ||
|
||
use Closure; | ||
use Filament\Actions\Concerns\CanCustomizeProcess; | ||
use Filament\Forms\Components\Actions\Action; | ||
use Filament\Forms\Components\Component; | ||
use Filament\Forms\Set; | ||
use Filament\Support\Facades\FilamentIcon; | ||
use Illuminate\Support\Str; | ||
|
||
class RegeneratePasswordAction extends Action | ||
{ | ||
use CanCustomizeProcess; | ||
|
||
protected const DEFAULT_MAX_LENGTH = 32; | ||
|
||
protected bool|Closure|null $notifyOnSuccess = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->label(__('filament-password-input::password.actions.regenerate.tooltip')); | ||
|
||
$this->icon(FilamentIcon::resolve('filament-password-input::regenerate') ?? 'heroicon-o-key'); | ||
|
||
$this->color('gray'); | ||
|
||
$this->successNotificationTitle(__('filament-password-input::password.actions.regenerate.success_message')); | ||
|
||
$this->action(function (Set $set, Component $component) { | ||
$secret = $this->process(function (Component $component) { | ||
$maxLength = rescue( | ||
callback: fn () => $component->getNewPasswordLength() ?? static::DEFAULT_MAX_LENGTH, | ||
rescue: fn () => static::DEFAULT_MAX_LENGTH, | ||
report: false, | ||
); | ||
|
||
return Str::password(max(3, $maxLength)); | ||
}); | ||
|
||
// Not sure if I'm doing something wrong here, but using ->getStatePath() | ||
// doesn't work here, but using ->getName() sets the correct path | ||
// to set the value. | ||
$set($component->getName(), $secret); | ||
|
||
if ($this->shouldNotifyOnSuccess()) { | ||
$this->success(); | ||
} | ||
}); | ||
} | ||
|
||
public static function getDefaultName(): ?string | ||
{ | ||
return 'regeneratePassword'; | ||
} | ||
|
||
public function notifyOnSuccess(bool|Closure $condition = true): static | ||
{ | ||
$this->notifyOnSuccess = $condition; | ||
|
||
return $this; | ||
} | ||
|
||
public function shouldNotifyOnSuccess(): bool | ||
{ | ||
return $this->evaluate($this->notifyOnSuccess) ?? true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.