|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap4; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Factory\Exceptions\InvalidConfigException; |
|
8
|
|
|
use Yiisoft\Html\Html; |
|
9
|
|
|
|
|
10
|
|
|
use function implode; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* ButtonToolbar Combines sets of button groups into button toolbars for more complex components. |
|
15
|
|
|
* Use utility classes as needed to space out groups, buttons, and more. |
|
16
|
|
|
* |
|
17
|
|
|
* For example, |
|
18
|
|
|
* |
|
19
|
|
|
* ```php |
|
20
|
|
|
* // a button toolbar with items configuration |
|
21
|
|
|
* echo ButtonToolbar::widget() |
|
22
|
|
|
* ->buttonGroups([ |
|
23
|
|
|
* [ |
|
24
|
|
|
* 'buttons' => [ |
|
25
|
|
|
* ['label' => '1', 'class' => ['btn-secondary']], |
|
26
|
|
|
* ['label' => '2', 'class' => ['btn-secondary']], |
|
27
|
|
|
* ['label' => '3', 'class' => ['btn-secondary']], |
|
28
|
|
|
* ['label' => '4', 'class' => ['btn-secondary']] |
|
29
|
|
|
* ], |
|
30
|
|
|
* 'class' => ['mr-2'] |
|
31
|
|
|
* ], |
|
32
|
|
|
* [ |
|
33
|
|
|
* 'buttons' => [ |
|
34
|
|
|
* ['label' => '5', 'class' => ['btn-secondary']], |
|
35
|
|
|
* ['label' => '6', 'class' => ['btn-secondary']], |
|
36
|
|
|
* ['label' => '7', 'class' => ['btn-secondary']] |
|
37
|
|
|
* ], |
|
38
|
|
|
* 'class' => ['mr-2'] |
|
39
|
|
|
* ], |
|
40
|
|
|
* [ |
|
41
|
|
|
* 'buttons' => [ |
|
42
|
|
|
* ['label' => '8', 'class' => ['btn-secondary']] |
|
43
|
|
|
* ] |
|
44
|
|
|
* ] |
|
45
|
|
|
* ]); |
|
46
|
|
|
* ``` |
|
47
|
|
|
* |
|
48
|
|
|
* Pressing on the button should be handled via JavaScript. See the following for details: |
|
49
|
|
|
*/ |
|
50
|
|
|
class ButtonToolbar extends Widget |
|
51
|
|
|
{ |
|
52
|
|
|
private array $buttonGroups = []; |
|
53
|
|
|
private array $options = []; |
|
54
|
|
|
|
|
55
|
2 |
|
protected function run(): string |
|
56
|
|
|
{ |
|
57
|
2 |
|
if (!isset($this->options['id'])) { |
|
58
|
2 |
|
$this->options['id'] = "{$this->getId()}-button-toolbar"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
Html::addCssClass($this->options, ['widget' => 'btn-toolbar']); |
|
62
|
|
|
|
|
63
|
2 |
|
if (!isset($this->options['role'])) { |
|
64
|
2 |
|
$this->options['role'] = 'toolbar'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return Html::tag('div', $this->renderButtonGroups(), $this->options); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Generates the button groups that compound the toolbar as specified on {@see buttonGroups}. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidConfigException |
|
74
|
|
|
* |
|
75
|
|
|
* @return string the rendering result. |
|
76
|
|
|
*/ |
|
77
|
2 |
|
protected function renderButtonGroups(): string |
|
78
|
|
|
{ |
|
79
|
2 |
|
$buttonGroups = []; |
|
80
|
|
|
|
|
81
|
2 |
|
foreach ($this->buttonGroups as $group) { |
|
82
|
2 |
|
if (is_array($group)) { |
|
83
|
2 |
|
if (!isset($group['buttons'])) { |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
$buttonGroups[] = ButtonGroup::widget() |
|
88
|
2 |
|
->buttons($group['buttons']) |
|
|
|
|
|
|
89
|
2 |
|
->options($group['options']) |
|
90
|
2 |
|
->render(); |
|
91
|
|
|
} else { |
|
92
|
2 |
|
$buttonGroups[] = $group; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
return implode("\n", $buttonGroups); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* List of buttons groups. Each array element represents a single group which can be specified as a string or an |
|
101
|
|
|
* array of the following structure: |
|
102
|
|
|
* |
|
103
|
|
|
* - buttons: array list of buttons. Either as array or string representation |
|
104
|
|
|
* - options: array optional, the HTML attributes of the button group. |
|
105
|
|
|
* - encodeLabels: bool whether to HTML-encode the button labels. |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $value |
|
108
|
|
|
* |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function buttonGroups(array $value): self |
|
112
|
|
|
{ |
|
113
|
2 |
|
$this->buttonGroups = $value; |
|
114
|
|
|
|
|
115
|
2 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* The HTML attributes for the container tag. The following special options are recognized. |
|
120
|
|
|
* |
|
121
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $value |
|
124
|
|
|
* |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function options(array $value): self |
|
128
|
|
|
{ |
|
129
|
2 |
|
$this->options = $value; |
|
130
|
|
|
|
|
131
|
2 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|