-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropdown.php
153 lines (133 loc) · 4.12 KB
/
Dropdown.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace pjkui\uikit;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Dropdown renders a UIkit dropdown menu component.
*
* ```php
* use pjkui\uikit\Dropdown;
* use pjkui\uikit\Nav;
*
* Dropdown::begin([
* 'tagOptions' => ['class' => 'uk-button-dropdown'],
* 'toggleButton' => ['label' => 'Action'],
* 'items' => []
* ]);
* echo Nav::widget([
* 'options' => ['class' => 'uk-nav-dropdown'],
* 'items' => [
* [
* 'label' => 'Home',
* 'url' => ['site/index'],
* 'linkOptions' => [...],
* ],
* [
* 'label' => 'Dropdown',
* 'items' => [
* ['label' => 'Level 1 - Dropdown A', 'url' => '#'],
* '<li class="divider"></li>',
* '<li class="dropdown-header">Dropdown Header</li>',
* ['label' => 'Level 1 - Dropdown B', 'url' => '#'],
* ],
* ],
* ],
* ]);
* Dropdown::end();
* ```
*
* @see https://getuikit.com/docs/dropdown
* @author Quinn Pan <pjkui@qq.com>
* @since 3.0
*/
class Dropdown extends Widget
{
/**
* @var array list of menu items in the dropdown. Each array element can be either an HTML string,
* or an array representing a single menu with the following structure:
*
* - label: string, required, the label of the item link
* - url: string, optional, the url of the item link. Defaults to "#".
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
* - linkOptions: array, optional, the HTML attributes of the item link.
* - options: array, optional, the HTML attributes of the item.
*
* To insert divider use `<li role="presentation" class="divider"></li>`.
*/
public $items = [];
public $itemsOptions = [];
public $toggleButton = [];
/**
* Container tag name, by default it can be div
* @var string
*/
public $tag = 'div';
public $tagOptions = [];
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, 'uk-dropdown');
$this->tagOptions['data-uk-dropdown'] = $this->jsonClientOptions();
if ($this->toggleButton !== null) {
Html::addCssClass($this->tagOptions, 'uk-button-dropdown');
}
if ($this->tag) {
echo Html::beginTag($this->tag, $this->tagOptions) . "\n";
}
echo $this->renderToggleButton() . "\n";
echo Html::beginTag('div', $this->options);
}
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderItems($this->items);
echo Html::endTag('div');
if ($this->tag) {
echo Html::endTag($this->tag);
}
$this->registerAsset();
}
/**
* Renders the toggle button.
* @return string the rendering result
*/
protected function renderToggleButton()
{
if ($this->toggleButton !== null) {
$tag = ArrayHelper::remove($this->toggleButton, 'tag', 'a');
$label = ArrayHelper::remove($this->toggleButton, 'label', 'Show');
if ($tag === 'button' && !isset($this->toggleButton['type'])) {
$this->toggleButton['type'] = 'button';
}
if ($tag === 'a' && !isset($this->toggleButton['href'])) {
$this->toggleButton['href'] = '#' . $this->options['id'];
}
return Html::tag($tag, $label, $this->toggleButton);
} else {
return null;
}
}
/**
* Renders menu items.
* @param array $items the menu items to be rendered
* @return string the rendering result.
* @throws InvalidConfigException if the label option is not specified in one of the items.
*/
protected function renderItems($items)
{
if ($this->tag) {
Html::addCssClass($this->itemsOptions, 'uk-nav-dropdown');
}
if (is_array($items)) {
return Nav::widget(['options' => $this->itemsOptions, 'items' => $items]);
}
return $items;
}
}