1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
|
11
|
|
|
use function implode; |
12
|
|
|
use function is_array; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ButtonGroup renders a button group bootstrap component. |
16
|
|
|
* |
17
|
|
|
* For example, |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* // a button group with items configuration |
21
|
|
|
* echo ButtonGroup::widget() |
22
|
|
|
* ->buttons([ |
23
|
|
|
* ['label' => 'A'], |
24
|
|
|
* ['label' => 'B'], |
25
|
|
|
* ['label' => 'C', 'visible' => false], |
26
|
|
|
* ]); |
27
|
|
|
* |
28
|
|
|
* // button group with an item as a string |
29
|
|
|
* echo ButtonGroup::widget() |
30
|
|
|
* ->buttons([ |
31
|
|
|
* Button::widget()->label('A'), |
32
|
|
|
* ['label' => 'B'], |
33
|
|
|
* ]); |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* Pressing on the button should be handled via JavaScript. See the following for details: |
37
|
|
|
*/ |
38
|
|
|
final class ButtonGroup extends Widget |
39
|
|
|
{ |
40
|
|
|
private array $buttons = []; |
41
|
|
|
private bool $encodeLabels = true; |
42
|
|
|
private bool $encodeTags = false; |
43
|
|
|
private array $options = []; |
44
|
|
|
|
45
|
7 |
|
public function render(): string |
46
|
|
|
{ |
47
|
7 |
|
if (!isset($this->options['id'])) { |
48
|
7 |
|
$this->options['id'] = "{$this->getId()}-button-group"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @psalm-suppress InvalidArgument */ |
52
|
7 |
|
Html::addCssClass($this->options, ['widget' => 'btn-group']); |
53
|
|
|
|
54
|
7 |
|
if (!isset($this->options['role'])) { |
55
|
7 |
|
$this->options['role'] = 'group'; |
56
|
|
|
} |
57
|
|
|
|
58
|
7 |
|
return Html::div($this->renderButtons(), $this->options) |
59
|
7 |
|
->encode($this->encodeTags) |
60
|
7 |
|
->render(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* List of buttons. Each array element represents a single button which can be specified as a string or an array of |
65
|
|
|
* the following structure: |
66
|
|
|
* |
67
|
|
|
* - label: string, required, the button label. |
68
|
|
|
* - options: array, optional, the HTML attributes of the button. |
69
|
|
|
* - visible: bool, optional, whether this button is visible. Defaults to true. |
70
|
|
|
* |
71
|
|
|
* @param array $value |
72
|
|
|
*/ |
73
|
7 |
|
public function buttons(array $value): self |
74
|
|
|
{ |
75
|
7 |
|
$new = clone $this; |
76
|
7 |
|
$new->buttons = $value; |
77
|
|
|
|
78
|
7 |
|
return $new; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* When tags Labels HTML should not be encoded. |
83
|
|
|
*/ |
84
|
1 |
|
public function withoutEncodeLabels(): self |
85
|
|
|
{ |
86
|
1 |
|
$new = clone $this; |
87
|
1 |
|
$new->encodeLabels = false; |
88
|
|
|
|
89
|
1 |
|
return $new; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
94
|
|
|
* |
95
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
96
|
|
|
* |
97
|
|
|
* @param array $value |
98
|
|
|
*/ |
99
|
4 |
|
public function options(array $value): self |
100
|
|
|
{ |
101
|
4 |
|
$new = clone $this; |
102
|
4 |
|
$new->options = $value; |
103
|
|
|
|
104
|
4 |
|
return $new; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generates the buttons that compound the group as specified on {@see buttons}. |
109
|
|
|
* |
110
|
|
|
* @throws InvalidConfigException |
111
|
|
|
* |
112
|
|
|
* @return string the rendering result. |
113
|
|
|
*/ |
114
|
7 |
|
private function renderButtons(): string |
115
|
|
|
{ |
116
|
7 |
|
$buttons = []; |
117
|
|
|
|
118
|
7 |
|
foreach ($this->buttons as $button) { |
119
|
7 |
|
if (is_array($button)) { |
120
|
7 |
|
$visible = ArrayHelper::remove($button, 'visible', true); |
121
|
|
|
|
122
|
7 |
|
if ($visible === false) { |
123
|
1 |
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
7 |
|
if (!isset($button['encodeLabel'])) { |
127
|
7 |
|
$button['encodeLabel'] = $this->encodeLabels; |
128
|
|
|
} |
129
|
|
|
|
130
|
7 |
|
if (!isset($button['options']['type'])) { |
131
|
6 |
|
ArrayHelper::setValueByPath($button, 'options.type', 'button'); |
132
|
|
|
} |
133
|
|
|
|
134
|
7 |
|
$buttonWidget = Button::widget() |
135
|
7 |
|
->label($button['label']) |
|
|
|
|
136
|
7 |
|
->options($button['options']); |
137
|
|
|
|
138
|
7 |
|
if ($button['encodeLabel'] === false) { |
139
|
1 |
|
$buttonWidget = $buttonWidget->withoutEncodeLabels(); |
140
|
|
|
} |
141
|
|
|
|
142
|
7 |
|
$buttons[] = $buttonWidget->render(); |
143
|
|
|
} else { |
144
|
1 |
|
$buttons[] = $button; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
7 |
|
return implode("\n", $buttons); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|