Skip to content

Commit

Permalink
Merge pull request #1387 from hydephp/navigation-dropdown-priorities
Browse files Browse the repository at this point in the history
Update dropdown navigation menus to support setting priority in config hydephp/develop@284d56a
  • Loading branch information
github-actions committed Oct 19, 2023
1 parent 964de23 commit f20115f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Framework/Features/Navigation/DropdownNavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Features\Navigation;

use Hyde\Facades\Config;
use Illuminate\Support\Collection;

use function collect;
Expand All @@ -19,9 +20,9 @@ class DropdownNavItem extends NavItem
public array $items;

/** @param array<NavItem> $items */
public function __construct(string $label, array $items)
public function __construct(string $label, array $items, ?int $priority = null)
{
parent::__construct('', $label, 999);
parent::__construct('', $label, $priority ?? $this->searchForDropdownPriorityInNavigationConfig($label) ?? 999);
$this->items = $items;
}

Expand All @@ -34,4 +35,13 @@ public function getItems(): Collection
{
return collect($this->items);
}

private function searchForDropdownPriorityInNavigationConfig(string $groupKey): ?int
{
return Config::getArray('hyde.navigation.order', [
'index' => 0,
'posts' => 10,
'docs/index' => 100,
])[$groupKey] ?? null;
}
}
30 changes: 30 additions & 0 deletions tests/Unit/DropdownNavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Unit;

use Hyde\Facades\Config;
use Hyde\Framework\Features\Navigation\DropdownNavItem;
use Hyde\Framework\Features\Navigation\NavItem;
use Hyde\Pages\MarkdownPage;
Expand Down Expand Up @@ -37,6 +38,21 @@ public function testConstruct()

$this->assertSame('foo', $item->label);
$this->assertSame([], $item->items);
$this->assertSame(999, $item->priority);
}

public function testConstructWithCustomPriority()
{
$item = new DropdownNavItem('foo', [], 500);

$this->assertSame(500, $item->priority);
}

public function testConstructWithNullPriority()
{
$item = new DropdownNavItem('foo', [], null);

$this->assertSame(999, $item->priority);
}

public function testFromArray()
Expand Down Expand Up @@ -72,4 +88,18 @@ public function testGetItems()
$item = DropdownNavItem::fromArray('foo', $children);
$this->assertSame($children, $item->getItems()->all());
}

public function testCanSetPriorityInConfig()
{
$root = Config::getFacadeRoot();
$mock = clone $root;
Config::swap($mock);

Config::set('hyde.navigation.order.foo', 500);
$item = new DropdownNavItem('foo', []);

$this->assertSame(500, $item->priority);

Config::swap($root);
}
}

0 comments on commit f20115f

Please sign in to comment.