diff --git a/resources/lang/ar/datatable.php b/resources/lang/ar/datatable.php index acb70d0a3..469f669ee 100644 --- a/resources/lang/ar/datatable.php +++ b/resources/lang/ar/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/ca/datatable.php b/resources/lang/ca/datatable.php index 9e1574fb6..b388f16bd 100644 --- a/resources/lang/ca/datatable.php +++ b/resources/lang/ca/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/cs/datatable.php b/resources/lang/cs/datatable.php index fd05c4e89..68d802119 100644 --- a/resources/lang/cs/datatable.php +++ b/resources/lang/cs/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/de/datatable.php b/resources/lang/de/datatable.php index bc0886d50..c98a5e694 100644 --- a/resources/lang/de/datatable.php +++ b/resources/lang/de/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/en/datatable.php b/resources/lang/en/datatable.php index 14804bd52..5308f7f1c 100644 --- a/resources/lang/en/datatable.php +++ b/resources/lang/en/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/es/datatable.php b/resources/lang/es/datatable.php index fb220730e..de4089879 100644 --- a/resources/lang/es/datatable.php +++ b/resources/lang/es/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/fa/datatable.php b/resources/lang/fa/datatable.php index b91e18978..71438f22b 100644 --- a/resources/lang/fa/datatable.php +++ b/resources/lang/fa/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/fr/datatable.php b/resources/lang/fr/datatable.php index 5a3b01265..45d77655b 100644 --- a/resources/lang/fr/datatable.php +++ b/resources/lang/fr/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/id/datatable.php b/resources/lang/id/datatable.php index 7753ce28e..53b235030 100644 --- a/resources/lang/id/datatable.php +++ b/resources/lang/id/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/it/datatable.php b/resources/lang/it/datatable.php index 576a5618e..61ff5169d 100644 --- a/resources/lang/it/datatable.php +++ b/resources/lang/it/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/ms_MY/datatable.php b/resources/lang/ms_MY/datatable.php index 3c7dd2c8f..8d31166d8 100644 --- a/resources/lang/ms_MY/datatable.php +++ b/resources/lang/ms_MY/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/nl/datatable.php b/resources/lang/nl/datatable.php index 6cb8de6df..e686bfb80 100644 --- a/resources/lang/nl/datatable.php +++ b/resources/lang/nl/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/pl/datatable.php b/resources/lang/pl/datatable.php index 059e4642f..9c75b1449 100644 --- a/resources/lang/pl/datatable.php +++ b/resources/lang/pl/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/pt_BR/datatable.php b/resources/lang/pt_BR/datatable.php index 6a84be76e..61e4e943c 100644 --- a/resources/lang/pt_BR/datatable.php +++ b/resources/lang/pt_BR/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/ru/datatable.php b/resources/lang/ru/datatable.php index 48e07fab9..4833342a9 100644 --- a/resources/lang/ru/datatable.php +++ b/resources/lang/ru/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/tr/datatable.php b/resources/lang/tr/datatable.php index 4f29e4b15..05ddd6a69 100644 --- a/resources/lang/tr/datatable.php +++ b/resources/lang/tr/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/uk/datatable.php b/resources/lang/uk/datatable.php index 0e2924bd0..6170b6911 100644 --- a/resources/lang/uk/datatable.php +++ b/resources/lang/uk/datatable.php @@ -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.", + ], + ], ]; diff --git a/resources/lang/yr/datatable.php b/resources/lang/yr/datatable.php index 6e74bf4bb..b6256b430 100644 --- a/resources/lang/yr/datatable.php +++ b/resources/lang/yr/datatable.php @@ -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.", + ], + ], ]; diff --git a/src/Button.php b/src/Button.php index 87798ad6d..86ca7663b 100644 --- a/src/Button.php +++ b/src/Button.php @@ -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 diff --git a/src/Components/Actions/Macros.php b/src/Components/Actions/Macros.php index 75d50ff2c..35c343c95 100644 --- a/src/Components/Actions/Macros.php +++ b/src/Components/Actions/Macros.php @@ -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; + }); } } diff --git a/tests/Feature/Buttons/ConfirmPromptTest.php b/tests/Feature/Buttons/ConfirmPromptTest.php new file mode 100644 index 000000000..6e8af5238 --- /dev/null +++ b/tests/Feature/Buttons/ConfirmPromptTest.php @@ -0,0 +1,39 @@ +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'); diff --git a/tests/Feature/Buttons/ConfirmTest.php b/tests/Feature/Buttons/ConfirmTest.php new file mode 100644 index 000000000..607c8a403 --- /dev/null +++ b/tests/Feature/Buttons/ConfirmTest.php @@ -0,0 +1,39 @@ +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');