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