-
Notifications
You must be signed in to change notification settings - Fork 2
/
ButtonDropdown.php
104 lines (97 loc) · 2.3 KB
/
ButtonDropdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace artkost\uikit;
use yii\helpers\Html;
/**
* ButtonDropdown renders a group or split button dropdown component.
*
* For example,
*
* ```php
* // a button group using Dropdown widget
* echo ButtonDropdown::widget([
* 'label' => 'Action',
* 'dropdown' => [
* 'items' => [
* ['label' => 'DropdownA', 'url' => '/'],
* ['label' => 'DropdownB', 'url' => '#'],
* ],
* ],
* ]);
* ```
* @see http://www.getuikit.com/docs/button.html
* @see http://www.getuikit.com/docs/dropdown.html
* @author Nikolay Kostyurin <[email protected]>
* @since 2.0
*/
class ButtonDropdown extends Widget
{
/**
* @var string the button label
*/
public $label = 'Button';
/**
* @var array the HTML attributes of the button.
*/
public $options = [];
/**
* @var array the configuration array for [[Dropdown]].
*/
public $dropdown = [];
/**
* @var boolean whether to display a group of split-styled button group.
*/
public $split = false;
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderButton() . "\n" . $this->renderDropdown();
$this->registerPlugin('button');
}
/**
* Generates the button dropdown.
* @return string the rendering result.
*/
protected function renderButton()
{
Html::addCssClass($this->options, 'btn');
if ($this->split) {
$tag = 'button';
$options = $this->options;
$this->options['data-toggle'] = 'dropdown';
Html::addCssClass($this->options, 'dropdown-toggle');
$splitButton = Button::widget([
'label' => '<span class="caret"></span>',
'encodeLabel' => false,
'options' => $this->options,
]);
} else {
$tag = 'a';
$this->label .= ' <span class="caret"></span>';
$options = $this->options;
if (!isset($options['href'])) {
$options['href'] = '#';
}
Html::addCssClass($options, 'dropdown-toggle');
$options['data-toggle'] = 'dropdown';
$splitButton = '';
}
return Button::widget([
'tagName' => $tag,
'label' => $this->label,
'options' => $options,
'encodeLabel' => false,
]) . "\n" . $splitButton;
}
/**
* Generates the dropdown menu.
* @return string the rendering result.
*/
protected function renderDropdown()
{
$config = $this->dropdown;
$config['clientOptions'] = false;
return Dropdown::widget($config);
}
}