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
|
|
|
if ($this->encodeTags === false) { |
38
|
8 |
|
$this->options = array_merge($this->options, ['encode' => false]); |
39
|
8 |
|
} |
40
|
8 |
|
|
41
|
8 |
|
return Html::tag( |
42
|
|
|
$this->tagName, |
43
|
|
|
$this->encodeLabels ? Html::encode($this->label) : $this->label, |
44
|
|
|
$this->options |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Whether the label should be HTML-encoded. |
50
|
|
|
* |
51
|
|
|
* @param bool $value |
52
|
8 |
|
* |
53
|
|
|
* @return $this |
54
|
8 |
|
*/ |
55
|
|
|
public function withoutEncodeLabels(bool $value = false): self |
56
|
8 |
|
{ |
57
|
|
|
$new = clone $this; |
58
|
|
|
$new->encodeLabels = $value; |
59
|
|
|
|
60
|
|
|
return $new; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The button label |
65
|
|
|
* |
66
|
8 |
|
* @param string $value |
67
|
|
|
* |
68
|
8 |
|
* @return $this |
69
|
|
|
*/ |
70
|
8 |
|
public function withLabel(string $value): self |
71
|
|
|
{ |
72
|
|
|
$new = clone $this; |
73
|
|
|
$new->label = $value; |
74
|
|
|
|
75
|
|
|
return $new; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
80
|
|
|
* |
81
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
82
|
8 |
|
* |
83
|
|
|
* @param array $value |
84
|
8 |
|
* |
85
|
|
|
* @return $this |
86
|
8 |
|
*/ |
87
|
|
|
public function withOptions(array $value): self |
88
|
|
|
{ |
89
|
|
|
$new = clone $this; |
90
|
|
|
$new->options = $value; |
91
|
|
|
|
92
|
|
|
return $new; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
3 |
|
* The tag to use to render the button. |
97
|
|
|
* |
98
|
3 |
|
* @param string $value |
99
|
|
|
* |
100
|
3 |
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function withTagName(string $value): self |
103
|
|
|
{ |
104
|
|
|
$new = clone $this; |
105
|
|
|
$new->tagName = $value; |
106
|
|
|
|
107
|
|
|
return $new; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Allows you to enable or disable the encoding tags html. |
112
|
|
|
* |
113
|
|
|
* @param bool $value |
114
|
|
|
* |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
|
|
public function withEncodeTags(bool $value = true): self |
118
|
|
|
{ |
119
|
|
|
$new = clone $this; |
120
|
|
|
$new->encodeTags = $value; |
121
|
|
|
|
122
|
|
|
return $new; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|