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
|
20 |
|
public function render(): string |
30
|
|
|
{ |
31
|
20 |
|
if (!isset($this->options['id'])) { |
32
|
12 |
|
$this->options['id'] = "{$this->getId()}-button"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** @psalm-suppress InvalidArgument */ |
36
|
20 |
|
Html::addCssClass($this->options, ['widget' => 'btn']); |
37
|
|
|
|
38
|
20 |
|
return Html::tag( |
39
|
20 |
|
$this->tagName, |
40
|
20 |
|
$this->encodeLabels ? Html::encode($this->label) : $this->label, |
41
|
20 |
|
$this->options |
42
|
20 |
|
) |
43
|
20 |
|
->encode($this->encodeTags) |
44
|
20 |
|
->render(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* When tags Labels HTML should not be encoded. |
49
|
|
|
*/ |
50
|
4 |
|
public function withoutEncodeLabels(): self |
51
|
|
|
{ |
52
|
4 |
|
$new = clone $this; |
53
|
4 |
|
$new->encodeLabels = false; |
54
|
|
|
|
55
|
4 |
|
return $new; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The button label |
60
|
|
|
*/ |
61
|
20 |
|
public function label(string $value): self |
62
|
|
|
{ |
63
|
20 |
|
$new = clone $this; |
64
|
20 |
|
$new->label = $value; |
65
|
|
|
|
66
|
20 |
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
71
|
|
|
* |
72
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
73
|
|
|
* |
74
|
|
|
* @param array $value |
75
|
|
|
*/ |
76
|
17 |
|
public function options(array $value): self |
77
|
|
|
{ |
78
|
17 |
|
$new = clone $this; |
79
|
17 |
|
$new->options = $value; |
80
|
|
|
|
81
|
17 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The tag to use to render the button. |
86
|
|
|
* |
87
|
|
|
* @psalm-param non-empty-string $value |
88
|
|
|
*/ |
89
|
10 |
|
public function tagName(string $value): self |
90
|
|
|
{ |
91
|
10 |
|
$new = clone $this; |
92
|
10 |
|
$new->tagName = $value; |
93
|
|
|
|
94
|
10 |
|
return $new; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|