Skip to content

Commit

Permalink
item actions with icons, addLink with closure param
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Dec 9, 2022
1 parent ee4f40f commit 0a512b8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
7 changes: 5 additions & 2 deletions resources/views/base/index/shared/item_actions.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@foreach($resource->itemActions() as $index => $action)
<a href="{{ $resource->route("action", $item->getKey(), ['index' => $index]) }}" class="text-purple inline-block">
{{ $action->label() }}
<a href="{{ $resource->route("action", $item->getKey(), ['index' => $index]) }}"
class="text-purple inline-block"
title="{{ $action->label() }}"
>
{{ $action->getIcon(6, 'purple', 'mr-2') }}
</a>
@endforeach

Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Resources/ResourceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/ItemActions/ItemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 3 additions & 19 deletions src/Menu/MenuSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,7 +154,7 @@ public function setTitleField(string $titleField): void
$this->titleField = $titleField;
}

public function getItem(): Model
public function getItem(): ?Model
{
return $this->item;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Traits/Fields/LinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Leeto\MoonShine\Traits\Fields;

use Closure;

trait LinkTrait
{
protected string $linkValue = '';
Expand All @@ -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;

Expand Down
29 changes: 29 additions & 0 deletions src/Traits/WithIcon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Leeto\MoonShine\Traits;

use Illuminate\Contracts\View\View;

trait WithIcon
{
protected string|null $icon = null;

public function icon(string $icon): static
{
$this->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'));
}
}

0 comments on commit 0a512b8

Please sign in to comment.