Skip to content

Commit c0d2be2

Browse files
feat: confirmation before any action. (#1599)
* adding macros functions and methods for Button class * adding English translations for default message * writing tests for confirm and confirmPrompt method * fix: test ConfirmPromptTest.php * style: adding line break in both macro functions * use camel case for $confirm_value to $confirmValue * adding missing multi_sort translations * adding english lang messages in all language * wip we change lang translation separator(-) to underscore(-) to make it similar to all other translations. * pint --------- Co-authored-by: luanfreitasdev <[email protected]>
1 parent 1899b41 commit c0d2be2

22 files changed

+252
-0
lines changed

resources/lang/ar/datatable.php

+11
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@
6262
'with_trashed' => 'مع المحذوف',
6363
'only_trashed' => 'فقط محذوف',
6464
],
65+
'multi_sort' => [
66+
'message' => 'Multiple sort is active',
67+
],
68+
'buttons_macros' => [
69+
'confirm' => [
70+
'message' => 'Are you sure you want to perform this action?',
71+
],
72+
'confirm_prompt' => [
73+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
74+
],
75+
],
6576
];

resources/lang/ca/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/cs/datatable.php

+11
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@
6262
'with_trashed' => 'Se smazanými',
6363
'only_trashed' => 'Pouze smazané',
6464
],
65+
'multi_sort' => [
66+
'message' => 'Multiple sort is active',
67+
],
68+
'buttons_macros' => [
69+
'confirm' => [
70+
'message' => 'Are you sure you want to perform this action?',
71+
],
72+
'confirm_prompt' => [
73+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
74+
],
75+
],
6576
];

resources/lang/de/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple Suche ist aktiv',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/en/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/es/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/fa/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/fr/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@
6565
'multi_sort' => [
6666
'message' => 'Multiple sort is active',
6767
],
68+
'buttons_macros' => [
69+
'confirm' => [
70+
'message' => 'Are you sure you want to perform this action?',
71+
],
72+
'confirm_prompt' => [
73+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
74+
],
75+
],
6876
];

resources/lang/id/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Pengurutan ganda diaktifkan',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/it/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/ms_MY/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/nl/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Meervoudige sortering is actief',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/pl/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Wielokolumnowe sortowanie jest aktywne',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/pt_BR/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'A classificação múltipla está ativa',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/ru/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@
6565
'multi_sort' => [
6666
'message' => 'Multiple sort is active',
6767
],
68+
'buttons_macros' => [
69+
'confirm' => [
70+
'message' => 'Are you sure you want to perform this action?',
71+
],
72+
'confirm_prompt' => [
73+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
74+
],
75+
],
6876
];

resources/lang/tr/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Çoklu sıralama etkin',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

resources/lang/uk/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@
6565
'multi_sort' => [
6666
'message' => 'Сортувати за',
6767
],
68+
'buttons_macros' => [
69+
'confirm' => [
70+
'message' => 'Are you sure you want to perform this action?',
71+
],
72+
'confirm_prompt' => [
73+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
74+
],
75+
],
6876
];

resources/lang/yr/datatable.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
'multi_sort' => [
6565
'message' => 'Multiple sort is active',
6666
],
67+
'buttons_macros' => [
68+
'confirm' => [
69+
'message' => 'Are you sure you want to perform this action?',
70+
],
71+
'confirm_prompt' => [
72+
'message' => "Are you sure you want to perform this action? \n\n Enter :confirmValue to confirm.",
73+
],
74+
],
6775
];

src/Button.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @method static bladeComponent(string $component, array $params)
2222
* @method static can(bool|\Closure $allowed = true)
2323
* @method static id(string $id = null)
24+
* @method static confirm(string $message = 'Are you sure you want to perform this action?')
25+
* @method static confirmPrompt(string $message = 'Are you sure you want to perform this action?', string $confirmValue = 'Confirm')
2426
*
2527
*/
2628
final class Button implements Wireable

src/Components/Actions/Macros.php

+22
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,27 @@ public static function boot(): void
194194

195195
return $this;
196196
});
197+
198+
Button::macro('confirm', function (?string $message = null) {
199+
$this->dynamicProperties['confirm'] = [
200+
'component' => 'button',
201+
'attribute' => 'wire:confirm',
202+
'value' => $message ?? trans('livewire-powergrid::datatable.buttons_macros.confirm.message'),
203+
];
204+
205+
return $this;
206+
});
207+
208+
Button::macro('confirmPrompt', function (?string $message = null, string $confirmValue = 'Confirm') {
209+
$message = $message ?? trans('livewire-powergrid::datatable.buttons_macros.confirm_prompt.message', ['confirm_value' => $confirmValue]);
210+
$confirm_value = trim($confirmValue);
211+
$this->dynamicProperties['confirmPrompt'] = [
212+
'component' => 'button',
213+
'attribute' => 'wire:confirm.prompt',
214+
'value' => "$message |$confirmValue",
215+
];
216+
217+
return $this;
218+
});
197219
}
198220
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use PowerComponents\LivewirePowerGrid\Button;
4+
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishTableBase;
5+
6+
use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;
7+
8+
$component = new class () extends DishTableBase {
9+
public function actions($row): array
10+
{
11+
return [
12+
Button::make('confirm-prompt')
13+
->slot('confirm-prompt: ' . $row->id)
14+
->confirmPrompt("$row->id Are you sure? Enter CONFIRM to confirm", "CONFIRM"),
15+
];
16+
}
17+
};
18+
19+
dataset('action:confirm-prompt', [
20+
'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]],
21+
'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]],
22+
'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]],
23+
'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]],
24+
]);
25+
26+
it('properly displays "confirm" on button click', function (string $component, object $params) {
27+
livewire($component, [
28+
'join' => $params->join,
29+
])
30+
->call($params->theme)
31+
->set('search', 'Pastel de Nata')
32+
->assertSeeHtml('wire:confirm.prompt="1 Are you sure? Enter CONFIRM to confirm |CONFIRM"')
33+
->assertDontSeeHtml('wire:confirm.prompt="2 Are you sure? Enter CONFIRM to confirm |CONFIRM"')
34+
->set('search', 'Peixada da chef Nábia')
35+
->assertSeeHtml('wire:confirm.prompt="2 Are you sure? Enter CONFIRM to confirm |CONFIRM"')
36+
->assertDontSeeHtml('wire:confirm.prompt="1 Are you sure? Enter CONFIRM to confirm |CONFIRM"');
37+
})
38+
->with('action:confirm-prompt')
39+
->group('action');

tests/Feature/Buttons/ConfirmTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use PowerComponents\LivewirePowerGrid\Button;
4+
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishTableBase;
5+
6+
use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;
7+
8+
$component = new class () extends DishTableBase {
9+
public function actions($row): array
10+
{
11+
return [
12+
Button::make('confirm')
13+
->slot('confirm: ' . $row->id)
14+
->confirm('Are you sure? ' . $row->id),
15+
];
16+
}
17+
};
18+
19+
dataset('action:confirm', [
20+
'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]],
21+
'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]],
22+
'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]],
23+
'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]],
24+
]);
25+
26+
it('properly displays "confirm" on button click', function (string $component, object $params) {
27+
livewire($component, [
28+
'join' => $params->join,
29+
])
30+
->call($params->theme)
31+
->set('search', 'Pastel de Nata')
32+
->assertSeeHtml('wire:confirm="Are you sure? 1"')
33+
->assertDontSeeHtml('wire:confirm="Are you sure? 2"')
34+
->set('search', 'Peixada da chef Nábia')
35+
->assertSeeHtml('wire:confirm="Are you sure? 2"')
36+
->assertDontSeeHtml('wire:confirm="Are you sure? 1"');
37+
})
38+
->with('action:confirm')
39+
->group('action');

0 commit comments

Comments
 (0)