-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added blade component
- Loading branch information
Showing
10 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tabuna\Breadcrumbs; | ||
|
||
use Illuminate\View\Component; | ||
use Illuminate\Support\Collection; | ||
|
||
class BreadcrumbsComponent extends Component | ||
{ | ||
/** | ||
* @var Manager | ||
*/ | ||
public $breadcrumbs; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $route; | ||
|
||
/** | ||
* @var mixed|null | ||
*/ | ||
public $parameters; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $class; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $active; | ||
|
||
/** | ||
* Create a new component instance. | ||
* | ||
* @param Manager $manager | ||
* @param string|null $route | ||
* @param mixed|null $parameters | ||
* @param string|null $class | ||
* @param string|null $active | ||
*/ | ||
public function __construct( | ||
Manager $manager, | ||
string $route = null, | ||
$parameters = null, | ||
string $class = null, | ||
string $active = null | ||
) | ||
{ | ||
$this->breadcrumbs = $manager; | ||
$this->route = $route; | ||
$this->parameters = $parameters; | ||
$this->class = $class; | ||
$this->active = $active; | ||
} | ||
|
||
/** | ||
* @return Collection | ||
* @throws \Throwable | ||
*/ | ||
public function generate(): Collection | ||
{ | ||
if ($this->route !== null) { | ||
return $this->breadcrumbs->generate($this->route, $this->parameters); | ||
} | ||
|
||
return $this->breadcrumbs->current($this->parameters); | ||
} | ||
|
||
/** | ||
* Get the view / contents that represent the component. | ||
* | ||
* @return \Illuminate\View\View|string | ||
*/ | ||
public function render() | ||
{ | ||
return $this->breadcrumbs->has($this->route) | ||
? view('breadcrumbs::breadcrumbs') | ||
: ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
|
||
namespace Tabuna\Breadcrumbs\Tests; | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Testing\TestResponse; | ||
use Tabuna\Breadcrumbs\Trail; | ||
|
||
class ComponentTest extends TestCase | ||
{ | ||
|
||
public function testSimple(): void | ||
{ | ||
$this->getComponent('simple')->assertSee('Home'); | ||
} | ||
|
||
public function testClass(): void | ||
{ | ||
$this->getComponent('class') | ||
->assertSee('Home') | ||
->assertSee('<li class="item">', false) | ||
->assertSee('<li class="item active">', false); | ||
} | ||
|
||
public function testRoute(): void | ||
{ | ||
$this->getComponent('route') | ||
->assertSee('Static Page'); | ||
} | ||
|
||
public function testParameters(): void | ||
{ | ||
$this->getComponent('parameters') | ||
->assertSee('value 1') | ||
->assertSee('value 2') | ||
->assertSee('value 3'); | ||
} | ||
|
||
/** | ||
* @param string $template | ||
* | ||
* @return TestResponse | ||
*/ | ||
protected function getComponent(string $template): TestResponse | ||
{ | ||
Route::get('static', function (string $view) { | ||
}) | ||
->name('static') | ||
->breadcrumbs(function (Trail $trail) { | ||
return $trail->push('Static Page'); | ||
}); | ||
|
||
Route::get('component/{view}', function (string $view) { | ||
return view("test_breadcrumbs::$view"); | ||
}) | ||
->name('breadcrumbs.components') | ||
->breadcrumbs(function (Trail $trail, $value = null, $value2 = null) { | ||
return $trail | ||
->push('Home') | ||
->push('Arguments', $value) | ||
->push('Arguments2', $value2); | ||
}); | ||
|
||
return $this->get("component/$template"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tabuna\Breadcrumbs\Tests; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class TestServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->loadViewsFrom(__DIR__ . '/views', 'test_breadcrumbs'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<x-tabuna-breadcrumbs | ||
class="item" | ||
active="active" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<x-tabuna-breadcrumbs | ||
parameters="['value 1', 'value 2', 'value 3']" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<x-tabuna-breadcrumbs | ||
route="static" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<x-tabuna-breadcrumbs/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@foreach ($generate() as $crumbs) | ||
@if ($crumbs->url() && !$loop->last) | ||
<li class="{{$class}}"> | ||
<a href="{{ $crumbs->url() }}"> | ||
{{ $crumbs->title() }} | ||
</a> | ||
</li> | ||
@else | ||
<li class="{{$class}} {{$active}}"> | ||
{{ $crumbs->title() }} | ||
</li> | ||
@endif | ||
@endforeach |