Skip to content

Commit

Permalink
feat(filament): added coluns to infolists (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored May 15, 2024
1 parent 35d38b5 commit 62b0b51
Show file tree
Hide file tree
Showing 38 changed files with 879 additions and 145 deletions.
66 changes: 66 additions & 0 deletions app/Concerns/Filament/Enums/HasColorOrEmoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace App\Concerns\Filament\Enums;

use Illuminate\Support\Str;

/**
* Trait HasColorOrEmoji.
*/
trait HasColorOrEmoji
{
/**
* Get the enum as an array formatted for a select, but styled.
*
* @param bool|null $hasColor
* @param bool|null $hasEmoji
* @param string|null $locale
* @return array
*/
public static function asSelectArrayStyled(?bool $hasColor = true, ?bool $hasEmoji = true, ?string $locale = null): array
{
$selectArray = [];

/** @var static $case */
foreach (static::cases() as $case) {
$selectArray[$case->value] = $case->localizeStyled($hasColor, $hasEmoji, $locale);
}

return $selectArray;
}

/**
* Localize the enum, but styled.
*
* @param bool|null $hasColor
* @param bool|null $hasEmoji
* @param string|null $locale
* @return string
*/
public function localizeStyled(?bool $hasColor = true, ?bool $hasEmoji = true, ?string $locale = null): string
{
$localizedName = $this->getLocalizedName($locale) ?? $this->getPrettyName();

$name = Str::of('');

if ($hasColor) {
$color = $this->getColor();
$name = $name->append("<p style='color: rgb($color);'>");
}

if ($hasEmoji) {
$emoji = $this->getEmoji();
$name = $name->append("$emoji ");
}

$name = $name->append($localizedName);

if ($hasColor) {
$name = $name->append('</p>');
}

return $name->__toString();
}
}
32 changes: 32 additions & 0 deletions app/Enums/Models/Wiki/AnimeSeason.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,48 @@
namespace App\Enums\Models\Wiki;

use App\Concerns\Enums\LocalizesName;
use App\Concerns\Filament\Enums\HasColorOrEmoji;

/**
* Enum AnimeSeason.
*/
enum AnimeSeason: int
{
use HasColorOrEmoji;
use LocalizesName;

case WINTER = 0;
case SPRING = 1;
case SUMMER = 2;
case FALL = 3;

/**
* Get the rgb color for the enum.
*
* @return string
*/
public function getColor(): string
{
return match ($this) {
static::WINTER => '153, 204, 255',
static::SPRING => '255, 192, 203',
static::SUMMER => '255, 153, 51',
static::FALL => '204, 102, 0',
};
}

/**
* Get the unicode emoji for the enum.
*
* @return string
*/
public function getEmoji(): string
{
return match ($this) {
static::WINTER => "\u{2744}",
static::SPRING => "\u{1F33C}",
static::SUMMER => "\u{2600}",
static::FALL => "\u{1F342}",
};
}
}
11 changes: 4 additions & 7 deletions app/Filament/Actions/Models/Wiki/Anime/BackfillAnimeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Filament\Tables\Actions\Action;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Arr;
use Illuminate\Support\Sleep;
Expand Down Expand Up @@ -62,20 +61,18 @@ protected function setUp(): void
{
parent::setUp();

$this->action(fn (Model $record, array $data) => $this->handle($record, $data));
$this->action(fn (Anime $record, array $data) => $this->handle($record, $data));
}

/**
* Perform the action on the given models.
*
* @param Model $anime
* @param Anime $anime
* @param array $fields
* @return void
*/
public function handle(Model $anime, array $fields): void
public function handle(Anime $anime, array $fields): void
{
if (!$anime instanceof Anime) return;

if ($anime->resources()->doesntExist()) {
$this->fail(__('filament.actions.anime.backfill.message.resource_required_failure'));
return;
Expand Down Expand Up @@ -126,7 +123,7 @@ public function getForm(Form $form): ?Form
->label(__('filament.actions.anime.backfill.fields.resources.kitsu.name'))
->helperText(__('filament.actions.anime.backfill.fields.resources.kitsu.help'))
->default(fn () => $anime instanceof Anime && $anime->resources()->where(ExternalResource::ATTRIBUTE_SITE, ResourceSite::KITSU->value)->doesntExist()),

Checkbox::make(self::BACKFILL_ANILIST_RESOURCE)
->label(__('filament.actions.anime.backfill.fields.resources.anilist.name'))
->helperText(__('filament.actions.anime.backfill.fields.resources.anilist.help'))
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Actions/Models/Wiki/AttachResourceAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getForm(Form $form): ?Form

$fields[] = TextInput::make($resourceSite->name)
->label($resourceSite->localize())
->helperText(__("nova.actions.models.wiki.attach_resource.fields.{$resourceSiteLower}.help"))
->helperText(__("filament.actions.models.wiki.attach_resource.fields.{$resourceSiteLower}.help"))
->url()
->maxLength(192)
->rules(['max:192', $this->getFormatRule($resourceSite)]);
Expand Down
18 changes: 17 additions & 1 deletion app/Filament/Components/Columns/TextColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Filament\Resources\BaseRelationManager;
use App\Filament\Resources\BaseResource;
use App\Models\BaseModel;
use Filament\Support\Enums\FontWeight;
use Filament\Tables\Columns\TextColumn as ColumnsTextColumn;

/**
Expand All @@ -24,7 +25,9 @@ class TextColumn extends ColumnsTextColumn
public function urlToRelated(string $resourceRelated, string $relation): static
{
return $this
->color('info')
->weight(FontWeight::SemiBold)
->html()
->formatStateUsing(fn ($state) => "<p style='color: rgb(64, 184, 166);'>$state</p>")
->hiddenOn(BaseRelationManager::class)
->url(function (BaseModel $record) use ($resourceRelated, $relation) {
foreach (explode('.', $relation) as $element) {
Expand All @@ -34,4 +37,17 @@ public function urlToRelated(string $resourceRelated, string $relation): static
return $record !== null ? (new $resourceRelated)::getUrl('edit', ['record' => $record]) : null;
});
}

/**
* Make the column copyable.
*
* @param bool $condition
* @return static
*/
public function copyableWithMessage(bool $condition = true): static
{
return $this
->copyable($condition)
->copyMessage(__('filament.actions.base.copied'));
}
}
2 changes: 1 addition & 1 deletion app/Filament/Components/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function useScout(string $model, ?string $loadRelation = null): static
return $this
->searchable()
->getSearchResultsUsing(function (string $search) use ($model, $loadRelation) {
return (new $model)::search($search)
return (new $model)::search($search)
->get()
->load($loadRelation ?? [])
->mapWithKeys(fn (BaseModel $model) => [$model->getKey() => $model->getName()])
Expand Down
51 changes: 51 additions & 0 deletions app/Filament/Components/Infolist/TextEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Filament\Components\Infolist;

use App\Filament\Resources\BaseResource;
use App\Models\BaseModel;
use Filament\Infolists\Components\TextEntry as ComponentsTextEntry;
use Filament\Support\Enums\FontWeight;

/**
* Class TextEntry.
*/
class TextEntry extends ComponentsTextEntry
{
/**
* Used for entry relationships.
*
* @param class-string<BaseResource> $resourceRelated
* @param string $relation
* @return static
*/
public function urlToRelated(string $resourceRelated, string $relation): static
{
return $this
->weight(FontWeight::SemiBold)
->html()
->formatStateUsing(fn ($state) => "<p style='color: rgb(64, 184, 166);'>$state</p>")
->url(function (BaseModel $record) use ($resourceRelated, $relation) {
foreach (explode('.', $relation) as $element) {
$record = $record->$element;
}

return $record !== null ? (new $resourceRelated)::getUrl('edit', ['record' => $record]) : null;
});
}

/**
* Make the entry copyable.
*
* @param bool $condition
* @return static
*/
public function copyableWithMessage(bool $condition = true): static
{
return $this
->copyable($condition)
->copyMessage(__('filament.actions.base.copied'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,18 @@ protected function setUp(): void
{
parent::setUp();

$this->action(fn (Model $record, array $data) => $this->handle($record, $data));
$this->action(fn (Anime $record, array $data) => $this->handle($record, $data));
}

/**
* Perform the action on the given models.
*
* @param Model $anime
* @param Anime $anime
* @param array $fields
* @return void
*/
public function handle(Model $anime, array $fields): void
public function handle(Anime $anime, array $fields): void
{
if (!$anime instanceof Anime) return;

if ($anime->resources()->doesntExist()) {
$this->fail(__('filament.actions.anime.backfill.message.resource_required_failure'));
return;
Expand Down Expand Up @@ -126,7 +124,7 @@ public function getForm(Form $form): ?Form
->label(__('filament.actions.anime.backfill.fields.resources.kitsu.name'))
->helperText(__('filament.actions.anime.backfill.fields.resources.kitsu.help'))
->default(fn () => $anime instanceof Anime && $anime->resources()->where(ExternalResource::ATTRIBUTE_SITE, ResourceSite::KITSU->value)->doesntExist()),

Checkbox::make(self::BACKFILL_ANILIST_RESOURCE)
->label(__('filament.actions.anime.backfill.fields.resources.anilist.name'))
->helperText(__('filament.actions.anime.backfill.fields.resources.anilist.help'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getForm(Form $form): ?Form

$fields[] = TextInput::make($resourceSite->name)
->label($resourceSite->localize())
->helperText(__("nova.actions.models.wiki.attach_resource.fields.{$resourceSiteLower}.help"))
->helperText(__("filament.actions.models.wiki.attach_resource.fields.{$resourceSiteLower}.help"))
->url()
->maxLength(192)
->rules(['max:192', $this->getFormatRule($resourceSite)]);
Expand Down
19 changes: 17 additions & 2 deletions app/Filament/Resources/Admin/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Resources\Admin;

use App\Filament\Components\Columns\TextColumn;
use App\Filament\Components\Infolist\TextEntry;
use App\Filament\Resources\BaseResource;
use App\Filament\Resources\Admin\Announcement\Pages\CreateAnnouncement;
use App\Filament\Resources\Admin\Announcement\Pages\EditAnnouncement;
Expand Down Expand Up @@ -142,7 +143,7 @@ public static function table(Table $table): Table
->label(__('filament.fields.announcement.content'))
->sortable()
->searchable()
->copyable(),
->copyableWithMessage(),
])
->defaultSort(AnnouncementModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
Expand All @@ -162,8 +163,22 @@ public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Section::make(static::getRecordTitle($infolist->getRecord()))
->schema([
TextEntry::make(AnnouncementModel::ATTRIBUTE_ID)
->label(__('filament.fields.base.id')),

TextEntry::make(AnnouncementModel::ATTRIBUTE_CONTENT)
->label(__('filament.fields.announcement.content'))
->markdown()
->columnSpanFull()
->copyableWithMessage(),
])
->columns(3),

Section::make(__('filament.fields.base.timestamps'))
->schema(parent::timestamps()),
->schema(parent::timestamps())
->columns(3),
]);
}

Expand Down
17 changes: 15 additions & 2 deletions app/Filament/Resources/Admin/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Resources\Admin;

use App\Filament\Components\Columns\TextColumn;
use App\Filament\Components\Infolist\TextEntry;
use App\Filament\Resources\BaseResource;
use App\Filament\Resources\Admin\Dump\Pages\CreateDump;
use App\Filament\Resources\Admin\Dump\Pages\EditDump;
Expand Down Expand Up @@ -148,7 +149,7 @@ public static function table(Table $table): Table
->label(__('filament.fields.dump.path'))
->sortable()
->searchable()
->copyable(),
->copyableWithMessage(),
])
->defaultSort(DumpModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
Expand All @@ -169,8 +170,20 @@ public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Section::make(static::getRecordTitle($infolist->getRecord()))
->schema([
TextEntry::make(DumpModel::ATTRIBUTE_ID)
->label(__('filament.fields.base.id')),

TextEntry::make(DumpModel::ATTRIBUTE_PATH)
->label(__('filament.fields.dump.path'))
->copyableWithMessage(),
])
->columns(3),

Section::make(__('filament.fields.base.timestamps'))
->schema(parent::timestamps()),
->schema(parent::timestamps())
->columns(3),
]);
}

Expand Down
Loading

0 comments on commit 62b0b51

Please sign in to comment.