Skip to content

Commit 4b412e7

Browse files
committed
nav, dropdown
1 parent 73ba4e8 commit 4b412e7

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

src/widgets/navigation/Dropdown.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace macgyer\yii2materializecss\widgets\navigation;
99

10-
use macgyer\yii2materializecss\assets\MaterializePluginAsset;
1110
use macgyer\yii2materializecss\lib\BaseWidget;
1211
use macgyer\yii2materializecss\lib\Html;
1312
use macgyer\yii2materializecss\widgets\Button;
@@ -21,23 +20,34 @@
2120
*
2221
* ```php
2322
* <div class="dropdown">
24-
* <a class="dropdown-button" href="#!" data-activates="dropdown1">Dropdown<i class="material-icons right">arrow_drop_down</i></a>
25-
* <?php
26-
* echo Dropdown::widget([
27-
* 'items' => [
28-
* ['label' => 'DropdownA', 'url' => '/'],
29-
* ['label' => 'DropdownB', 'url' => '#'],
30-
* ],
31-
* 'toggleTarget' => 'dropdown1'
32-
* ]);
33-
* ?>
23+
* <?= \macgyer\yii2materializecss\widgets\navigation\Dropdown::widget([
24+
* 'items' => [
25+
* ['label' => 'DropdownA', 'url' => '/'],
26+
* ['label' => 'DropdownB', 'url' => '#'],
27+
* ],
28+
* 'toggleButtonOptions' => [
29+
* 'label' => 'Drop Me!'
30+
* ]
31+
* ]); ?>
3432
* </div>
3533
* ```
3634
*
37-
* Please make sure that you provide the trigger with a `data-activates` attribute and specify the value of this attribute
38-
* in the [[toggleTarget]] property of the widget.
35+
* produces
3936
*
40-
* @see http://materializecss.com/navbar.html#navbar-dropdown
37+
* ```
38+
* <div class="dropdown">
39+
* <button type="button" id="w1" class="dropdown-trigger btn" data-target="w0">Drop Me!</button>
40+
* <ul id="w0" class="dropdown-content" tabindex="0">
41+
* <li tabindex="0"><a href="/" tabindex="-1">DropdownA</a></li>
42+
* <li tabindex="0"><a href="#" tabindex="-1">DropdownB</a></li>
43+
* </ul>
44+
* </div>
45+
* ```
46+
*
47+
* If you are using a custom toggle button, please make sure to correctly set the `data-target` attribute in your toggle button.
48+
* It has to match the `id` attribute of the dropdown.
49+
*
50+
* @see https://materializecss.com/dropdown.html
4151
* @author Christoph Erdmann <[email protected]>
4252
* @package widgets
4353
* @subpackage navigation
@@ -74,6 +84,11 @@ class Dropdown extends BaseWidget
7484
*/
7585
public $submenuOptions;
7686

87+
/**
88+
* @var array the configuration options for the dropdown toggle button. See [[Button|Button]] for detailed information
89+
* on available options.
90+
* To prevent the toggle button from being rendered, set this options to `false`.
91+
*/
7792
public $toggleButtonOptions = [];
7893

7994
/**
@@ -88,13 +103,10 @@ public function init()
88103
}
89104
parent::init();
90105

91-
$this->options['id'] = $this->getUniqueId();
92-
Html::addCssClass($this->options, ['widget' => 'dropdown-content']);
93-
94-
if (!isset($this->toggleButtonOptions['options']['data-target'])) {
95-
$this->toggleButtonOptions['options']['data-target'] = $this->options['id'];
106+
if (!isset($this->options['id'])) {
107+
$this->options['id'] = $this->getUniqueId();
96108
}
97-
Html::addCssClass($this->toggleButtonOptions['options'], ['toggle-button' => 'dropdown-trigger']);
109+
Html::addCssClass($this->options, ['widget' => 'dropdown-content']);
98110

99111
$this->registerPlugin('Dropdown', '.dropdown-trigger');
100112
}
@@ -104,19 +116,29 @@ public function init()
104116
*
105117
* @return string the result of widget execution to be outputted.
106118
* @throws InvalidConfigException
107-
* @see MaterializePluginAsset|MaterializePluginAsset
108119
* @see \macgyer\yii2materializecss\lib\MaterializeWidgetTrait|MaterializeWidgetTrait
109120
*/
110121
public function run()
111122
{
112-
$html[] = $this->renderToggleButton();
123+
if ($this->toggleButtonOptions !== false) {
124+
$html[] = $this->renderToggleButton();
125+
}
113126
$html[] = $this->renderItems($this->items, $this->options);
114127
return implode("\n", $html);
115128
}
116129

130+
/**
131+
* Renders the dropdown toggle button.
132+
*
133+
* @return string the markup of the toggle button.
134+
* @throws \Exception
135+
*/
117136
protected function renderToggleButton()
118137
{
119-
$this->toggleButtonOptions['tagName'] = 'a';
138+
if (!isset($this->toggleButtonOptions['options']['data-target'])) {
139+
$this->toggleButtonOptions['options']['data-target'] = $this->options['id'];
140+
}
141+
Html::addCssClass($this->toggleButtonOptions['options'], ['toggle-button' => 'dropdown-trigger']);
120142

121143
return Button::widget($this->toggleButtonOptions);
122144
}

src/widgets/navigation/Nav.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ protected function renderItem($item)
215215
$items = '';
216216
} else {
217217
$toggleTarget = 'dropdown_' . md5(uniqid());
218-
$linkOptions['data-activates'] = $toggleTarget;
218+
$linkOptions['data-target'] = $toggleTarget;
219219
Html::addCssClass($options, ['widget' => 'dropdown']);
220-
Html::addCssClass($linkOptions, ['widget' => 'dropdown-button']);
220+
Html::addCssClass($linkOptions, ['widget' => 'dropdown-trigger']);
221221
if ($this->dropDownCaret !== '') {
222-
$label .= ' ' . $this->dropDownCaret;
222+
$label .= $this->dropDownCaret;
223223
}
224224
if (is_array($items)) {
225225
if ($this->activateItems) {
@@ -250,19 +250,25 @@ protected function renderItem($item)
250250
*/
251251
protected function renderDropdown($items, $parentItem, $targetId)
252252
{
253+
$dropdownOptions = ArrayHelper::getValue($parentItem, 'dropDownOptions', []);
254+
if (!isset($dropdownOptions['id'])) {
255+
$dropdownOptions['id'] = $targetId;
256+
}
257+
253258
return Dropdown::widget([
254-
'toggleTarget' => $targetId,
255-
'options' => ArrayHelper::getValue($parentItem, 'dropDownOptions', []),
259+
'options' => $dropdownOptions,
256260
'items' => $items,
257261
'encodeLabels' => $this->encodeLabels,
258-
'clientOptions' => false,
259-
'view' => $this->getView(),
262+
'toggleButtonOptions' => false,
263+
// 'clientOptions' => false,
264+
// 'view' => $this->getView(),
260265
]);
261266
}
262267

263268
/**
264269
* Renders the side navigation and corresponding toggle button.
265270
* @return string the rendered side navigation markup.
271+
* @throws \Exception
266272
*/
267273
protected function renderSideNav()
268274
{

0 commit comments

Comments
 (0)