Repository navigation
Expand file tree
/
Copy pathConfirmationDialog.php
More file actions
221 lines (187 loc) · 6.73 KB
/
Copy pathConfirmationDialog.php
File metadata and controls
221 lines (187 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare(strict_types=1);
namespace NyonCode\WireCore\Modals;
use NyonCode\WireCore\Core\Support\Trans;
use NyonCode\WireCore\Foundation\Colors\Color;
use NyonCode\WireCore\Foundation\Concerns\HasModalProperties;
use NyonCode\WireCore\Foundation\Concerns\InteractsWithColor;
use NyonCode\WireCore\Foundation\Icons\Icon;
use NyonCode\WireCore\Modals\Concerns\HasFooterActions;
use NyonCode\WireCore\Modals\Contracts\ModalContract;
/**
* Confirmation dialog configuration.
*
* A specialized modal for yes/no confirmation prompts.
* Provides presets for common confirmation patterns (delete, danger, warning).
*
* Usage:
* ConfirmationDialog::make()
* ->heading('Delete record?')
* ->description('This action cannot be undone.')
* ->icon('trash', 'danger')
* ->danger();
*
* Presets:
* ConfirmationDialog::delete('User');
* ConfirmationDialog::danger('Are you sure?', 'This is dangerous.');
*
* @phpstan-consistent-constructor
*/
class ConfirmationDialog implements ModalContract
{
use HasFooterActions;
use HasModalProperties;
use InteractsWithColor;
protected ?string $icon = null;
protected ?string $iconColor = Color::Warning->value;
protected bool $isDanger = false;
protected bool $isInformative = false;
public static function make(): static
{
return new static;
}
// ─── Presets ─────────────────────────────────────────────────
/**
* Preset: Delete confirmation.
*/
public static function delete(?string $recordName = null): static
{
$description = $recordName
? Trans::get('wire-core::actions.delete_description_named', ['name' => $recordName])
: Trans::get('wire-core::actions.delete_description');
return static::make()
->heading(Trans::get('wire-core::actions.delete_heading'))
->description($description)
->icon('trash', Color::Danger)
->submitLabel(Trans::get('wire-core::actions.delete_submit'))
->danger();
}
/**
* Preset: Generic danger confirmation.
*/
public static function makeDanger(string $heading, ?string $description = null): static
{
return static::make()
->heading($heading)
->description($description)
->icon('warning', Color::Danger)
->danger();
}
/**
* Preset: Warning confirmation.
*/
public static function makeWarning(string $heading, ?string $description = null): static
{
return static::make()
->heading($heading)
->description($description)
->icon('warning', Color::Warning);
}
/**
* Preset: Informative (no submit button, just info with a close button).
*/
public static function makeInfo(string $heading, ?string $description = null): static
{
return static::make()
->heading($heading)
->description($description)
->icon('info', Color::Info)
->informative();
}
/**
* Preset: Success info.
*/
public static function makeSuccess(string $heading, ?string $description = null): static
{
return static::make()
->heading($heading)
->description($description)
->icon('check-circle', Color::Success)
->informative();
}
// ─── Fluent setters ─────────────────────────────────────────
public function icon(string|Icon|null $icon, string|Color|null $color = null): static
{
$this->icon = $icon instanceof Icon ? $icon->value() : $icon;
if ($color !== null) {
$this->iconColor = $color instanceof Color ? $color->value : $color;
}
return $this;
}
public function danger(bool $danger = true): static
{
$this->isDanger = $danger;
// `danger()` names the intent, not the hue: it fills the colour slot only
// when nothing has chosen one. The same rule already guards the two
// confirmation constructors (`Modals\Html\Confirmation`,
// `Modals\View\ConfirmationComponent`), and they are the newer copies —
// without the guard, `->color('primary')->danger()` and
// `->danger()->color('primary')` disagree, and so does the Blade tag.
if ($danger && $this->color === null) {
$this->color = Color::Danger->value;
}
return $this;
}
public function informative(bool $informative = true): static
{
$this->isInformative = $informative;
if ($informative) {
$this->cancelLabel = $this->cancelLabel ?? Trans::get('wire-core::actions.confirm_close');
}
return $this;
}
// ─── Getters ────────────────────────────────────────────────
public function getIcon(): ?string
{
return $this->icon;
}
public function getIconColor(): string
{
return $this->iconColor ?? Color::Warning->value;
}
public function isDanger(): bool
{
return $this->isDanger;
}
public function isInformative(): bool
{
return $this->isInformative;
}
public function getSubmitLabel(): ?string
{
if ($this->isInformative) {
return null;
}
return $this->submitLabel ?? Trans::get('wire-core::actions.confirm_submit');
}
public function getCancelLabel(): string
{
if ($this->isInformative) {
return $this->cancelLabel ?? Trans::get('wire-core::actions.confirm_close');
}
return $this->cancelLabel ?? Trans::get('wire-core::actions.confirm_cancel');
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'id' => $this->getId(),
'heading' => $this->getHeading(),
'description' => $this->getDescription(),
'icon' => $this->getIcon(),
'iconColor' => $this->getIconColor(),
'color' => $this->getColor(),
'width' => $this->getWidth(),
'closeOnClickAway' => $this->shouldCloseOnClickAway(),
'closeOnEscape' => $this->shouldCloseOnEscape(),
'submitLabel' => $this->getSubmitLabel(),
'cancelLabel' => $this->getCancelLabel(),
'isDanger' => $this->isDanger(),
'isInformative' => $this->isInformative(),
'stickyFooter' => $this->hasStickyFooter(),
'stickyHeader' => $this->hasStickyHeader(),
];
}
}