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

feat: confirmation before any action. #1599

8 changes: 8 additions & 0 deletions resources/lang/en/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'Multiple sort is active',
],
'buttons-macros' => [
hemant-kr-meena marked this conversation as resolved.
Show resolved Hide resolved
'confirm' => [
'message' => 'Are you sure you want to perform this action?',
],
'confirm-prompt' => [
'message' => "Are you sure you want to perform this action? \n\n Enter :confirm_value to confirm.",
]
],
];
2 changes: 2 additions & 0 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @method static bladeComponent(string $component, array $params)
* @method static can(bool|\Closure $allowed = true)
* @method static id(string $id = null)
* @method static confirm(string $message = 'Are you sure you want to perform this action?')
* @method static confirmPrompt(string $message = 'Are you sure you want to perform this action?', string $confirm_value = 'Confirm')
*
*/
final class Button implements Wireable
Expand Down
22 changes: 22 additions & 0 deletions src/Components/Actions/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,27 @@ public static function boot(): void

return $this;
});

Button::macro('confirm', function (?string $message = null) {
$this->dynamicProperties['confirm'] = [
'component' => 'button',
'attribute' => 'wire:confirm',
'value' => $message ?? trans('livewire-powergrid::datatable.buttons-macros.confirm.message'),
];

return $this;
});

Button::macro('confirmPrompt', function (?string $message = null, string $confirm_value = 'Confirm') {
$message = $message ?? trans('livewire-powergrid::datatable.buttons-macros.confirm-prompt.message', ['confirm_value' => $confirm_value]);
$confirm_value = trim($confirm_value);
$this->dynamicProperties['confirmPrompt'] = [
'component' => 'button',
'attribute' => 'wire:confirm.prompt',
'value' => "$message |$confirm_value",
hemant-kr-meena marked this conversation as resolved.
Show resolved Hide resolved
];

return $this;
});
}
}
39 changes: 39 additions & 0 deletions tests/Feature/Buttons/ConfirmPromptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishTableBase;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

$component = new class () extends DishTableBase {
public function actions($row): array
{
return [
Button::make('confirm-prompt')
->slot('confirm-prompt: ' . $row->id)
->confirmPrompt("$row->id Are you sure? Enter CONFIRM to confirm","CONFIRM"),
];
}
};

dataset('action:confirm-prompt', [
'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]],
]);

it('properly displays "confirm" on button click', function (string $component, object $params) {
livewire($component, [
'join' => $params->join,
])
->call($params->theme)
->set('search', 'Pastel de Nata')
->assertSeeHtml('wire:confirm.prompt="1 Are you sure? Enter CONFIRM to confirm |CONFIRM"')
->assertDontSeeHtml('wire:confirm.prompt="2 Are you sure? Enter CONFIRM to confirm |CONFIRM"')
->set('search', 'Peixada da chef Nábia')
->assertSeeHtml('wire:confirm.prompt="2 Are you sure? Enter CONFIRM to confirm |CONFIRM"')
->assertDontSeeHtml('wire:confirm.prompt="1 Are you sure? Enter CONFIRM to confirm |CONFIRM"');
})
->with('action:confirm-prompt')
->group('action');
39 changes: 39 additions & 0 deletions tests/Feature/Buttons/ConfirmTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishTableBase;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

$component = new class () extends DishTableBase {
public function actions($row): array
{
return [
Button::make('confirm')
->slot('confirm: ' . $row->id)
->confirm('Are you sure? ' . $row->id),
];
}
};

dataset('action:confirm', [
'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]],
]);

it('properly displays "confirm" on button click', function (string $component, object $params) {
livewire($component, [
'join' => $params->join,
])
->call($params->theme)
->set('search', 'Pastel de Nata')
->assertSeeHtml('wire:confirm="Are you sure? 1"')
->assertDontSeeHtml('wire:confirm="Are you sure? 2"')
->set('search', 'Peixada da chef Nábia')
->assertSeeHtml('wire:confirm="Are you sure? 2"')
->assertDontSeeHtml('wire:confirm="Are you sure? 1"');
})
->with('action:confirm')
->group('action');