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

11 changes: 11 additions & 0 deletions resources/lang/ar/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,15 @@
'with_trashed' => 'مع المحذوف',
'only_trashed' => 'فقط محذوف',
],
'multi_sort' => [
'message' => 'Multiple sort is active',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/ca/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' => [
'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 :confirmValue to confirm.",
],
],
];
11 changes: 11 additions & 0 deletions resources/lang/cs/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,15 @@
'with_trashed' => 'Se smazanými',
'only_trashed' => 'Pouze smazané',
],
'multi_sort' => [
'message' => 'Multiple sort is active',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/de/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'Multiple Suche ist aktiv',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
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' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/es/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' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/fa/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' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/fr/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@
'multi_sort' => [
'message' => 'Multiple sort is active',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/id/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'Pengurutan ganda diaktifkan',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/it/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' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/ms_MY/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' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/nl/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'Meervoudige sortering is actief',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/pl/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'Wielokolumnowe sortowanie jest aktywne',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/pt_BR/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'A classificação múltipla está ativa',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/ru/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@
'multi_sort' => [
'message' => 'Multiple sort is active',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/tr/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
'multi_sort' => [
'message' => 'Çoklu sıralama etkin',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/uk/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@
'multi_sort' => [
'message' => 'Сортувати за',
],
'buttons_macros' => [
'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 :confirmValue to confirm.",
],
],
];
8 changes: 8 additions & 0 deletions resources/lang/yr/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' => [
'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 :confirmValue 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 $confirmValue = '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 $confirmValue = 'Confirm') {
$message = $message ?? trans('livewire-powergrid::datatable.buttons_macros.confirm_prompt.message', ['confirm_value' => $confirmValue]);
$confirm_value = trim($confirmValue);
$this->dynamicProperties['confirmPrompt'] = [
'component' => 'button',
'attribute' => 'wire:confirm.prompt',
'value' => "$message |$confirmValue",
];

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');