File tree Expand file tree Collapse file tree 4 files changed +92
-40
lines changed Expand file tree Collapse file tree 4 files changed +92
-40
lines changed Original file line number Diff line number Diff line change @@ -133,4 +133,47 @@ public function it_can_destroy_all_menus()
133133 $ this ->menu ->destroy ();
134134 $ this ->assertCount (0 , $ this ->menu ->all ());
135135 }
136+
137+ /** @test */
138+ public function it_still_generates_empty_menu_after_adding_dropdown ()
139+ {
140+ $ this ->menu ->create ('test ' , function (MenuBuilder $ menu ) {
141+ $ menu ->dropdown ('Test ' , function ($ sub ) {
142+
143+ })->hideWhen (function () {
144+ return true ;
145+ });
146+ });
147+
148+ $ expected = <<<TEXT
149+
150+ <ul class="nav navbar-nav">
151+
152+ </ul>
153+
154+ TEXT ;
155+
156+ self ::assertEquals ($ expected , $ this ->menu ->get ('test ' ));
157+ }
158+
159+ /** @test */
160+ public function it_still_generates_empty_menu_after_adding_item ()
161+ {
162+ $ this ->menu ->create ('test ' , function (MenuBuilder $ menu ) {
163+ $ menu ->url ('/ ' , 'Test ' )
164+ ->hideWhen (function () {
165+ return true ;
166+ });
167+ });
168+
169+ $ expected = <<<TEXT
170+
171+ <ul class="nav navbar-nav">
172+
173+ </ul>
174+
175+ TEXT ;
176+
177+ self ::assertEquals ($ expected , $ this ->menu ->get ('test ' ));
178+ }
136179}
Original file line number Diff line number Diff line change 33namespace Nwidart \Menus ;
44
55use Countable ;
6- use Illuminate \Contracts \Config \Repository ;
76use Illuminate \Support \Arr ;
7+ use Nwidart \Menus \Traits \CanHide ;
8+ use Illuminate \Contracts \Config \Repository ;
89use Illuminate \View \Factory as ViewFactory ;
910
1011class MenuBuilder implements Countable
1112{
13+ use CanHide;
1214 /**
1315 * Menu name.
1416 *
@@ -634,7 +636,7 @@ protected function renderMenu()
634636 $ menu = $ presenter ->getOpenTagWrapper ();
635637
636638 foreach ($ this ->getOrderedItems () as $ item ) {
637- if ($ item ->hidden ()) {
639+ if ($ item ->hidden () || $ this -> hidden () ) {
638640 continue ;
639641 }
640642
Original file line number Diff line number Diff line change 22
33namespace Nwidart \Menus ;
44
5- use Closure ;
6- use Collective \Html \HtmlFacade as HTML ;
7- use Illuminate \Contracts \Support \Arrayable as ArrayableContract ;
85use Illuminate \Support \Arr ;
9- use Illuminate \Support \Facades \Request ;
106use Illuminate \Support \Str ;
7+ use Nwidart \Menus \Traits \CanHide ;
8+ use Collective \Html \HtmlFacade as HTML ;
9+ use Illuminate \Support \Facades \Request ;
10+ use Illuminate \Contracts \Support \Arrayable as ArrayableContract ;
1111
1212/**
1313 * @property string url
2222 */
2323class MenuItem implements ArrayableContract
2424{
25+ use CanHide;
2526 /**
2627 * Array properties.
2728 *
@@ -54,13 +55,6 @@ class MenuItem implements ArrayableContract
5455 'hideWhen ' ,
5556 );
5657
57- /**
58- * The hideWhen callback.
59- *
60- * @var Closure
61- */
62- protected $ hideWhen ;
63-
6458 /**
6559 * Constructor.
6660 *
@@ -592,33 +586,6 @@ public function order($order)
592586 return $ this ;
593587 }
594588
595- /**
596- * Set hide condition for current menu item.
597- *
598- * @param Closure
599- * @return boolean
600- */
601- public function hideWhen (Closure $ callback )
602- {
603- $ this ->hideWhen = $ callback ;
604-
605- return $ this ;
606- }
607-
608- /**
609- * Determine whether the menu item is hidden.
610- *
611- * @return boolean
612- */
613- public function hidden ()
614- {
615- if (is_null ($ this ->hideWhen )) {
616- return false ;
617- }
618-
619- return call_user_func ($ this ->hideWhen ) == true ;
620- }
621-
622589 /**
623590 * Get the instance as an array.
624591 *
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Nwidart \Menus \Traits ;
4+
5+ use Closure ;
6+
7+ trait CanHide
8+ {
9+ /**
10+ * @var Closure
11+ */
12+ protected $ hideWhen ;
13+
14+ /**
15+ * Set hide condition for current menu item.
16+ *
17+ * @param Closure
18+ * @return boolean
19+ */
20+ public function hideWhen (Closure $ callback )
21+ {
22+ $ this ->hideWhen = $ callback ;
23+
24+ return $ this ;
25+ }
26+
27+ /**
28+ * Determine whether the menu item is hidden.
29+ *
30+ * @return boolean
31+ */
32+ public function hidden ()
33+ {
34+ if (is_null ($ this ->hideWhen )) {
35+ return false ;
36+ }
37+
38+ return call_user_func ($ this ->hideWhen ) == true ;
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments