Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable icons per widget #35

Merged
merged 12 commits into from
Feb 17, 2024
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ public static function getOverlookWidgetTitle(): string
}
```

### Customize Widget Icon

By default, the icon will be loaded from the resource but you can override it by passing using the `icons` modifier on the plugin and passing it an array of icon names and resource names.

```php
use Awcodes\Overlook\OverlookPlugin;

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
OverlookPlugin::make()
->icons([
'heroicon-o-heart' => \App\Filament\Resources\Shop\ProductResource::class,
'heroicon-o-newspaper' => \App\Filament\Resources\Shop\OrderResource::class,
]),
]);
}
```


<!-- docs_end -->

Expand Down
14 changes: 14 additions & 0 deletions src/OverlookPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class OverlookPlugin implements Plugin

protected int | Closure | null $sort = null;

protected array | Closure | null $icons = null;

public function getId(): string
{
return 'awcodes/overlook';
Expand Down Expand Up @@ -136,4 +138,16 @@ public function tooltips(bool | Closure | null $condition = true): static

return $this;
}

public function icons(array | Closure | null $icons): static
{
$this->icons = $icons;

return $this;
}

public function getIcons(): array
{
return $this->evaluate($this->icons) ?? [];
}
}
12 changes: 9 additions & 3 deletions src/Widgets/OverlookWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class OverlookWidget extends Widget

public array $grid = [];

public array $icons = [];

/**
* @throws Exception
*/
Expand Down Expand Up @@ -61,16 +63,20 @@ public function getData(): array
$plugin = OverlookPlugin::get();
$includes = filled($this->includes) ? $this->includes : $plugin->getIncludes();
$excludes = filled($this->excludes) ? $this->excludes : $plugin->getExcludes();
$icons = filled($this->icons) ? $this->icons : $plugin->getIcons();

$rawResources = filled($includes)
? $includes
: filament()->getCurrentPanel()->getResources();

return collect($rawResources)->filter(function ($resource) use ($excludes) {
return ! in_array($resource, $excludes);
})->transform(function ($resource) {
})->transform(function ($resource) use ($icons) {

$customIcon = array_search($resource, $icons);

$res = app($resource);

$widgetQuery = $res->getEloquentQuery();

if ($res instanceof CustomizeOverlookWidget) {
Expand All @@ -86,7 +92,7 @@ public function getData(): array
'name' => $title,
'raw_count' => $this->formatRawcount($rawCount),
'count' => $this->convertCount($rawCount),
'icon' => $res->getNavigationIcon(),
'icon' => $customIcon ?: $res->getNavigationIcon(),
'url' => $res->getUrl('index'),
];
}
Expand Down