1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Html\Html; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Button renders a bootstrap button. |
11
|
|
|
* |
12
|
|
|
* For example, |
13
|
|
|
* |
14
|
|
|
* ```php |
15
|
|
|
* echo Button::widget() |
16
|
|
|
* ->label('Action') |
17
|
|
|
* ->options(['class' => 'btn-lg']); |
18
|
|
|
* ``` |
19
|
|
|
*/ |
20
|
|
|
final class Button extends Widget |
21
|
|
|
{ |
22
|
|
|
/** @psalm-var non-empty-string */ |
23
|
|
|
private string $tagName = 'button'; |
24
|
|
|
private string $label = 'Button'; |
25
|
|
|
private bool $encodeLabels = true; |
26
|
|
|
private bool $encodeTags = false; |
27
|
|
|
private array $options = []; |
28
|
|
|
|
29
|
23 |
|
public function render(): string |
30
|
|
|
{ |
31
|
23 |
|
if (!isset($this->options['id'])) { |
32
|
23 |
|
$this->options['id'] = "{$this->getId()}-button"; |
33
|
|
|
} |
34
|
|
|
|
35
|
23 |
|
if ($this->theme) { |
36
|
1 |
|
$this->options['data-bs-theme'] = $this->theme; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @psalm-suppress InvalidArgument */ |
40
|
23 |
|
Html::addCssClass($this->options, ['widget' => 'btn']); |
41
|
|
|
|
42
|
23 |
|
return Html::tag( |
43
|
23 |
|
$this->tagName, |
44
|
23 |
|
$this->encodeLabels ? Html::encode($this->label) : $this->label, |
45
|
23 |
|
$this->options |
46
|
23 |
|
) |
47
|
23 |
|
->encode($this->encodeTags) |
48
|
23 |
|
->render(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* When tags Labels HTML should not be encoded. |
53
|
|
|
*/ |
54
|
5 |
|
public function withoutEncodeLabels(): self |
55
|
|
|
{ |
56
|
5 |
|
$new = clone $this; |
57
|
5 |
|
$new->encodeLabels = false; |
58
|
|
|
|
59
|
5 |
|
return $new; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The button label |
64
|
|
|
*/ |
65
|
23 |
|
public function label(string $value): self |
66
|
|
|
{ |
67
|
23 |
|
$new = clone $this; |
68
|
23 |
|
$new->label = $value; |
69
|
|
|
|
70
|
23 |
|
return $new; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
75
|
|
|
* |
76
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
77
|
|
|
*/ |
78
|
20 |
|
public function options(array $value): self |
79
|
|
|
{ |
80
|
20 |
|
$new = clone $this; |
81
|
20 |
|
$new->options = $value; |
82
|
|
|
|
83
|
20 |
|
return $new; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The tag to use to render the button. |
88
|
|
|
* |
89
|
|
|
* @psalm-param non-empty-string $value |
90
|
|
|
*/ |
91
|
13 |
|
public function tagName(string $value): self |
92
|
|
|
{ |
93
|
13 |
|
$new = clone $this; |
94
|
13 |
|
$new->tagName = $value; |
95
|
|
|
|
96
|
13 |
|
return $new; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|