Skip to content

Commit

Permalink
Merge pull request #8 from innocenzi/feat/better-attribute-support
Browse files Browse the repository at this point in the history
feat: support `attributes` parameter
  • Loading branch information
freekmurze authored Jun 10, 2022
2 parents 4c786a6 + 03fc20c commit 954baa7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function __construct(ActiveUrlChecker $activeUrlChecker)
$this->children = [];
}

public static function make(): static
{
return app(static::class);
}

public function add(string $title = '', string $url = '', ?callable $configure = null): self
{
$section = new Section($this, $title, $url);
Expand Down
10 changes: 7 additions & 3 deletions src/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@ public function __construct(Node $parent, string $title = '', string $url = '')
$this->children = [];
}

public function add(string $title = '', string $url = '', ?callable $configure = null): self
public function add(string $title = '', string $url = '', ?callable $configure = null, ?array $attributes = null): self
{
$section = new Section($this, $title, $url);

if ($configure) {
$configure($section);
}

if ($attributes) {
$section->attributes($attributes);
}

$this->children[] = $section;

return $this;
}

public function addIf($condition, string $title = '', string $url = '', ?callable $configure = null): self
public function addIf($condition, string $title = '', string $url = '', ?callable $configure = null, ?array $attributes = null): self
{
if ($this->resolveCondition($condition)) {
$this->add($title, $url, $configure);
$this->add($title, $url, $configure, $attributes);
}

return $this;
Expand Down
9 changes: 9 additions & 0 deletions tests/SectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public function test_it_can_configure_children()
$this->assertEquals('qux', $section->children[0]->attributes['baz']);
}

public function test_attributes_can_be_configured_inline()
{
$section = (new Section($this->navigation, 'Top level', '/'))
->add('First link', '/link', attributes: ['icon' => 'mdi:link']);

$this->assertCount(1, $section->children);
$this->assertArrayHasKey('icon', $section->children[0]->attributes);
}

public function test_it_has_depth()
{
$section = (new Section($this->navigation, 'Hello, world!', '/'))->add('Blog', '/posts');
Expand Down

0 comments on commit 954baa7

Please sign in to comment.