Skip to content

Commit 5712aed

Browse files
committed
Sort navigation items on retrieval
1 parent 8b2daf0 commit 5712aed

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/framework/src/Framework/Features/Navigation/NavigationGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getPriority(): int
5959
/** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem> */
6060
public function getItems(): Collection
6161
{
62-
return $this->items;
62+
return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values();
6363
}
6464

6565
/** @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items */

packages/framework/tests/Unit/NavigationGroupTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ public function testAddChildrenMethodReturnsSelf()
119119
$this->assertSame($group, $group->add([]));
120120
}
121121

122+
public function testItemsOrderingDefaultsToAddOrder()
123+
{
124+
$group = new NavigationGroup('Group', [
125+
new NavigationItem(new Route(new MarkdownPage()), 'Foo'),
126+
new NavigationItem(new Route(new MarkdownPage()), 'Bar'),
127+
new NavigationItem(new Route(new MarkdownPage()), 'Baz'),
128+
]);
129+
130+
$this->assertSame(['Foo', 'Bar', 'Baz'], $group->getItems()->map(fn (NavigationItem $item) => $item->getLabel())->all());
131+
}
132+
133+
public function testItemsAreSortedByPriority()
134+
{
135+
$group = new NavigationGroup('Group', [
136+
new NavigationItem(new Route(new MarkdownPage()), 'Foo', 3),
137+
new NavigationItem(new Route(new MarkdownPage()), 'Bar', 2),
138+
new NavigationItem(new Route(new MarkdownPage()), 'Baz', 1),
139+
]);
140+
141+
$this->assertSame(['Baz', 'Bar', 'Foo'], $group->getItems()->map(fn (NavigationItem $item) => $item->getLabel())->all());
142+
}
143+
122144
public function testGetPriorityUsesDefaultPriority()
123145
{
124146
$this->assertSame(999, (new NavigationGroup('Foo'))->getPriority());

0 commit comments

Comments
 (0)