From 0a512b8c620e4d24531a4c808e0475b91af6fea1 Mon Sep 17 00:00:00 2001 From: LT Date: Fri, 9 Dec 2022 14:12:44 +0300 Subject: [PATCH] item actions with icons, addLink with closure param --- .../base/index/shared/item_actions.blade.php | 7 +++-- src/Contracts/Resources/ResourceContract.php | 4 +-- src/ItemActions/ItemAction.php | 2 ++ src/Menu/MenuSection.php | 22 ++------------ src/Resources/Resource.php | 4 +-- src/Traits/Fields/LinkTrait.php | 8 ++++- src/Traits/WithIcon.php | 29 +++++++++++++++++++ 7 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 src/Traits/WithIcon.php diff --git a/resources/views/base/index/shared/item_actions.blade.php b/resources/views/base/index/shared/item_actions.blade.php index a288c1fa2..b58eee8a6 100644 --- a/resources/views/base/index/shared/item_actions.blade.php +++ b/resources/views/base/index/shared/item_actions.blade.php @@ -1,6 +1,9 @@ @foreach($resource->itemActions() as $index => $action) - getKey(), ['index' => $index]) }}" class="text-purple inline-block"> - {{ $action->label() }} + getKey(), ['index' => $index]) }}" + class="text-purple inline-block" + title="{{ $action->label() }}" + > + {{ $action->getIcon(6, 'purple', 'mr-2') }} @endforeach diff --git a/src/Contracts/Resources/ResourceContract.php b/src/Contracts/Resources/ResourceContract.php index 30951e004..26022a8c1 100644 --- a/src/Contracts/Resources/ResourceContract.php +++ b/src/Contracts/Resources/ResourceContract.php @@ -37,9 +37,9 @@ public function getModel(): Model; /** * Get current eloquent instance * - * @return Model + * @return ?Model */ - public function getItem(): Model; + public function getItem(): ?Model; /** * Get a collection of additional actions performed on resource page diff --git a/src/ItemActions/ItemAction.php b/src/ItemActions/ItemAction.php index 8da2f0311..41a255216 100644 --- a/src/ItemActions/ItemAction.php +++ b/src/ItemActions/ItemAction.php @@ -7,10 +7,12 @@ use Illuminate\Database\Eloquent\Model; use Leeto\MoonShine\Traits\Makeable; use Closure; +use Leeto\MoonShine\Traits\WithIcon; final class ItemAction { use Makeable; + use WithIcon; public function __construct( protected string $label, diff --git a/src/Menu/MenuSection.php b/src/Menu/MenuSection.php index 2708affa4..f79802962 100644 --- a/src/Menu/MenuSection.php +++ b/src/Menu/MenuSection.php @@ -8,12 +8,13 @@ use Illuminate\Http\Request; use Illuminate\Support\Collection; use Leeto\MoonShine\Resources\Resource; +use Leeto\MoonShine\Traits\WithIcon; abstract class MenuSection { - protected string $title; + use WithIcon; - protected string|null $icon = null; + protected string $title; protected Collection $items; @@ -33,13 +34,6 @@ public function items(): Collection return $this->items; } - public function icon(string $icon): static - { - $this->icon = $icon; - - return $this; - } - public function badge(Closure $callback): static { $this->badge = $callback; @@ -83,16 +77,6 @@ public function isSee(Request $request) : true; } - public function getIcon( - string $size = '8', - string $color = '', - string $class = '' - ): \Illuminate\Contracts\View\View { - $icon = $this->icon ?? 'app'; - - return view("moonshine::shared.icons.$icon", compact('size', 'color', 'class')); - } - public function isGroup(): bool { return $this instanceof MenuGroup; diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index b8a3ef101..37bf7fe45 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -57,7 +57,7 @@ abstract class Resource implements ResourceContract protected static bool $system = false; - protected Model $item; + protected ?Model $item = null; /** * Get an array of validation rules for resource related model @@ -154,7 +154,7 @@ public function setTitleField(string $titleField): void $this->titleField = $titleField; } - public function getItem(): Model + public function getItem(): ?Model { return $this->item; } diff --git a/src/Traits/Fields/LinkTrait.php b/src/Traits/Fields/LinkTrait.php index 32b73c9df..23fe2659f 100644 --- a/src/Traits/Fields/LinkTrait.php +++ b/src/Traits/Fields/LinkTrait.php @@ -4,6 +4,8 @@ namespace Leeto\MoonShine\Traits\Fields; +use Closure; + trait LinkTrait { protected string $linkValue = ''; @@ -25,8 +27,12 @@ public function getLinkValue(): string return $this->linkValue; } - public function addLink(string $name, string $link): static + public function addLink(string $name, string|Closure $link): static { + if(is_callable($link)) { + $link = call_user_func($link); + } + $this->linkValue = $link; $this->linkName = $name; diff --git a/src/Traits/WithIcon.php b/src/Traits/WithIcon.php new file mode 100644 index 000000000..5ebf9a871 --- /dev/null +++ b/src/Traits/WithIcon.php @@ -0,0 +1,29 @@ +icon = $icon; + + return $this; + } + + public function getIcon( + string $size = '8', + string $color = '', + string $class = '' + ): View { + $icon = $this->icon ?? 'app'; + + return view("moonshine::shared.icons.$icon", compact('size', 'color', 'class')); + } +}