77
88namespace macgyer \yii2materializecss \widgets \navigation ;
99
10- use macgyer \yii2materializecss \assets \MaterializePluginAsset ;
1110use macgyer \yii2materializecss \lib \BaseWidget ;
1211use macgyer \yii2materializecss \lib \Html ;
1312use macgyer \yii2materializecss \widgets \Button ;
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 }
0 commit comments