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
|
|
|
* ->withLabel('Action') |
17
|
|
|
* ->withOptions(['class' => 'btn-lg']); |
18
|
|
|
* ``` |
19
|
|
|
*/ |
20
|
|
|
final class Button extends Widget |
21
|
|
|
{ |
22
|
|
|
private string $tagName = 'button'; |
23
|
|
|
private string $label = 'Button'; |
24
|
|
|
private bool $encodeLabels = true; |
25
|
|
|
private bool $encodeTags = false; |
26
|
|
|
private array $options = []; |
27
|
8 |
|
|
28
|
|
|
public function run(): string |
29
|
8 |
|
{ |
30
|
6 |
|
if (!isset($this->options['id'])) { |
31
|
|
|
$this->options['id'] = "{$this->getId()}-button"; |
32
|
|
|
} |
33
|
|
|
|
34
|
8 |
|
/** @psalm-suppress InvalidArgument */ |
35
|
|
|
Html::addCssClass($this->options, ['widget' => 'btn']); |
36
|
8 |
|
|
37
|
|
|
$this->registerPlugin('button', $this->options); |
38
|
8 |
|
|
39
|
8 |
|
if ($this->encodeTags === false) { |
40
|
8 |
|
$this->options = array_merge($this->options, ['encode' => false]); |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
return Html::tag( |
44
|
|
|
$this->tagName, |
45
|
|
|
$this->encodeLabels ? Html::encode($this->label) : $this->label, |
46
|
|
|
$this->options |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Whether the label should be HTML-encoded. |
52
|
8 |
|
* |
53
|
|
|
* @param bool $value |
54
|
8 |
|
* |
55
|
|
|
* @return $this |
56
|
8 |
|
*/ |
57
|
|
|
public function withEncodeLabels(bool $value): self |
58
|
|
|
{ |
59
|
|
|
$new = clone $this; |
60
|
|
|
$new->encodeLabels = $value; |
61
|
|
|
|
62
|
|
|
return $new; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
8 |
|
* The button label |
67
|
|
|
* |
68
|
8 |
|
* @param string $value |
69
|
|
|
* |
70
|
8 |
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function withLabel(string $value): self |
73
|
|
|
{ |
74
|
|
|
$new = clone $this; |
75
|
|
|
$new->label = $value; |
76
|
|
|
|
77
|
|
|
return $new; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
82
|
8 |
|
* |
83
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
84
|
8 |
|
* |
85
|
|
|
* @param array $value |
86
|
8 |
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function withOptions(array $value): self |
90
|
|
|
{ |
91
|
|
|
$new = clone $this; |
92
|
|
|
$new->options = $value; |
93
|
|
|
|
94
|
|
|
return $new; |
95
|
|
|
} |
96
|
3 |
|
|
97
|
|
|
/** |
98
|
3 |
|
* The tag to use to render the button. |
99
|
|
|
* |
100
|
3 |
|
* @param string $value |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function withTagName(string $value): self |
105
|
|
|
{ |
106
|
|
|
$new = clone $this; |
107
|
|
|
$new->tagName = $value; |
108
|
|
|
|
109
|
|
|
return $new; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Allows you to enable or disable the encoding tags html. |
114
|
|
|
* |
115
|
|
|
* @param bool $value |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
|
|
public function withEncodeTags(bool $value): self |
120
|
|
|
{ |
121
|
|
|
$new = clone $this; |
122
|
|
|
$new->encodeTags = $value; |
123
|
|
|
|
124
|
|
|
return $new; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|