Skip to content

Commit a00b943

Browse files
author
Nur Alam
committed
nav render without header bug fixed
1 parent 654cd05 commit a00b943

File tree

3 files changed

+67
-43
lines changed

3 files changed

+67
-43
lines changed

README.md

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ This package generates navigation/navbar for laravel application. The package al
66
# Sample & Usages
77
```php
88
$navitems = Nav::make()
9+
->add('Home', route('home'), ['icon' => 'fa fa-home'])
910
->header('Adminland', function (Nav $nav) {
1011
$nav
11-
->addIf(hasPermission('role-list'), 'Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
12-
->addIf(hasPermission('system-user-list'), 'Users', route('system-user-list'), ['icon' => 'fa fa-users']);
12+
->add('Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
13+
->add('Users', route('system-user-list'), ['icon' => 'fa fa-users']);
1314
})
1415
->header('Employee Management', function (Nav $nav) {
1516
$nav
@@ -43,9 +44,9 @@ class ViewServiceProvider extends ServiceProvider
4344
{
4445
View::composer('layouts.partials._left_nav',function(View $view){
4546
$navitems = Nav::make()
46-
->add('Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
47+
->addIf(condition: true, 'Roles', route('role-list'), ['icon' => 'fa fa-user-tag'])
4748
->add('Users', route('system-user-list'), ['icon' => 'fa fa-users'])
48-
->add('Employee', '#', ['icon' => 'fa fa-user'], function (Children $children) {
49+
->add('Employee', '#', ['icon' => 'fa fa-user'], function (Children $children) {
4950
$children
5051
->addif(condition: true, 'List', route('employee-list'), ['icon' => 'fa fa-list'])
5152
->addif(condition: false, 'Create', route('create-employee'), ['icon' => 'fa fa-plus-circle']);
@@ -182,12 +183,22 @@ $navitems = Nav::make()
182183
```php
183184
// render() result sample
184185
[
186+
"home" => [
187+
"title" => "Home",
188+
"url" => "http://hrp.test/",
189+
"attributes" => [
190+
"icon" => 'fa fa-home'
191+
],
192+
"is_active" => false,
193+
"type" => "menu",
194+
"children" => [] // no children
195+
],
185196
"adminland" => [ // header
186197
"title" => "Adminland",
187198
"attributes" => [],
188199
"type" => "header",
189200
"nav-items" => [ // nav items under the adminland header
190-
[
201+
'roles' => [
191202
"title" => "Roles",
192203
"url" => "http://hrp.test/role-list",
193204
"attributes" => [
@@ -197,7 +208,7 @@ $navitems = Nav::make()
197208
"type" => "menu",
198209
"children" => [] // no children
199210
],
200-
[
211+
'user' => [
201212
"title" => "User",
202213
"url" => "http://hrp.test/system-user-list",
203214
"attributes" => [
@@ -214,7 +225,7 @@ $navitems = Nav::make()
214225
"attributes" => [],
215226
"type" => "header",
216227
"nav-items" => [ // nav items under the employee managment
217-
[
228+
'employee' => [
218229
"title" => "Employee", // parent nav
219230
"url" => "#",
220231
"attributes" => [
@@ -223,33 +234,30 @@ $navitems = Nav::make()
223234
"is_active" => false,
224235
"type" => "menu",
225236
"children" => [ // children nav items of employee nav
226-
"nav-items" => [
227-
[
228-
"title" => "List",
229-
"url" => "http://hrp.test/employee-list",
230-
"attributes" => [
231-
"icon" => 'fa fa-list'
232-
],
233-
"is_active" => false,
234-
"type" => "menu",
235-
"children" => []
237+
'list' => [
238+
"title" => "List",
239+
"url" => "http://hrp.test/employee-list",
240+
"attributes" => [
241+
"icon" => 'fa fa-list'
242+
],
243+
"is_active" => false,
244+
"type" => "menu",
245+
"children" => []
246+
],
247+
'create' => [
248+
"title" => "Create",
249+
"url" => "http://hrp.test/create-employee",
250+
"attributes" => [
251+
"icon" => 'fa fa-plus-circle'
236252
],
237-
[
238-
"title" => "Create",
239-
"url" => "http://hrp.test/create-employee",
240-
"attributes" => [
241-
"icon" => 'fa fa-plus-circle'
242-
],
243-
"is_active" => false,
244-
"type" => "menu",
245-
"children" => []
246-
]
253+
"is_active" => false,
254+
"type" => "menu",
255+
"children" => []
247256
]
248257
]
249258
]
250259
]
251260
],
252-
"nav-items" => [] // nav items without header will be append here
253261
]
254262
```
255263
## Navbar UI Builder

src/Nav.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ public function header(string $name, Closure $closure, array $attributes = []):
4444
$navItems = $nav->render();
4545

4646
// if no nav-items then why bother to create header in first place
47-
if (empty($navItems['nav-items'])) {
47+
if (empty($navItems)) {
4848
return $this;
4949
}
5050

51-
$this->navWithHeader[Str::slug($name)] = array_merge([
51+
$this->nav[Str::slug($name)] = [
5252
'title' => $name,
5353
'attributes' => $attributes,
54-
'type' => 'header'
55-
], $navItems);
54+
'type' => 'header',
55+
'nav-items' => $navItems
56+
];
5657

5758
return $this;
5859
}
@@ -70,6 +71,8 @@ public function add(string $title, string $url, ?array $attributes = null, ?call
7071
{
7172
$childrenItems = [];
7273

74+
$hasNoChildren = false;
75+
7376
$isActive = false;
7477

7578
// active a nav
@@ -84,16 +87,20 @@ public function add(string $title, string $url, ?array $attributes = null, ?call
8487

8588
$childrenItems = $childrenNav->render();
8689

90+
$hasNoChildren = empty($childrenItems);
91+
8792
// active parent nav if any child nav is active
88-
$isActive = $this->isChildrenActive($childrenItems['nav-items']);
93+
$isActive = $this->isChildrenActive($childrenItems);
8994
}
9095

9196
// if no children then why bother to create the nav
92-
if (array_key_exists('nav-items', $childrenItems) && empty($childrenItems['nav-items'])) {
97+
if ($hasNoChildren) {
9398
return $this;
9499
}
95100

96-
$this->nav[] = [
101+
$navKey = Str::slug(strtolower($title));
102+
103+
$this->nav[$navKey] = [
97104
'title' => $title,
98105
'url' => $url,
99106
'attributes' => $attributes,
@@ -128,7 +135,7 @@ public function addIf($condition, string $title, string $url, array $attributes
128135

129136
public function render()
130137
{
131-
return array_merge($this->navWithHeader, ['nav-items' => $this->nav]);
138+
return array_merge($this->navWithHeader, $this->nav);
132139
}
133140

134141
protected function isChildrenActive($items)
@@ -137,7 +144,7 @@ protected function isChildrenActive($items)
137144
return false;
138145
}
139146

140-
foreach ($items as $item) {
147+
foreach ($items as $key => $item) {
141148
if ($item['is_active']) {
142149
return true;
143150
}

src/Presenter/NavbarPresenter.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ protected function headerTag(string $name, array $attributes = [])
4242
? '<i class="nav-header-icon ' . $attributes['icon'] . '"></i>'
4343
: '';
4444

45-
return '<li class="nav-header">'
46-
. ($icon) . $name
47-
. '</li>';
45+
return '<li class="nav-header">'
46+
. ($icon) . $name
47+
. '</li>';
4848
}
4949

5050
/**
@@ -57,8 +57,11 @@ protected function headerTag(string $name, array $attributes = [])
5757
protected function treeTag(array $navItem)
5858
{
5959
$treeNav = '';
60+
6061
$navIcon = $this->navIcon($navItem);
62+
6163
$active = $navItem['is_active'] ? 'active' : '';
64+
6265
$menuOpen = $navItem['is_active'] ? 'menu-open' : 'has-treeview';
6366

6467
$treeNav = PHP_EOL . '<li class="nav-item ' . $menuOpen . '">
@@ -71,7 +74,7 @@ protected function treeTag(array $navItem)
7174
</a>
7275
<ul class="nav nav-treeview">';
7376

74-
$treeNav .= $this->nested($navItem['children']['nav-items']);
77+
$treeNav .= $this->nested($navItem['children']);
7578

7679
$treeNav .= '</ul></li>' . PHP_EOL;
7780

@@ -88,6 +91,7 @@ protected function treeTag(array $navItem)
8891
protected function soloTag(array $navItem)
8992
{
9093
$navIcon = $this->navIcon($navItem);
94+
9195
$active = $navItem['is_active'] ? 'active' : '';
9296

9397
return PHP_EOL . '<li class="nav-item">
@@ -133,11 +137,16 @@ public function navbar(): string
133137
$nav = '';
134138

135139
foreach ($this->navItems as $key => $item) {
136-
if ($key != 'nav-items' && $item['type'] == 'header') {
140+
if ($item['type'] === 'header') {
137141
$nav .= $this->headerTag($item['title'], $item['attributes']);
138142

139143
$nav .= $this->nav($item['nav-items']);
140144
}
145+
146+
// nav items without header
147+
if ($item['type'] === 'menu') {
148+
$nav .= $this->nav([$item]);
149+
}
141150
}
142151

143152
return $this->navTag($nav);
@@ -151,7 +160,7 @@ public function navbar(): string
151160
*/
152161
protected function isTreeNav(array $nav)
153162
{
154-
return !empty($nav['children']) && !empty($nav['children']['nav-items']);
163+
return !empty($nav['children']);
155164
}
156165

157166
/**

0 commit comments

Comments
 (0)