Skip to content

Commit

Permalink
add ->allowDescription() to TypeColumn to show description on tooltip…
Browse files Browse the repository at this point in the history
… & add ordering
  • Loading branch information
3x1io committed Sep 11, 2024
1 parent 2b73a42 commit 3c14f01
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
32 changes: 32 additions & 0 deletions database/migrations/2024_09_11_143941_update_types_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('types', function (Blueprint $table) {
$table->integer('order')->default(0)->after('id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('types', function (Blueprint $table) {
$table->dropColumn('order');
});
}
};
4 changes: 3 additions & 1 deletion resources/views/columns/type-column.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
$type = \TomatoPHP\FilamentTypes\Models\Type::where('key', $getState())->first();
}
$description = null;
if($type){
$value = $type->name;
$hex = $type->color;
$icon = $type->icon;
$description = $type->description;
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
$colorRGB= array($r, $g, $b);
}
Expand All @@ -32,7 +34,7 @@
}
@endphp
@if($value || config('filament-types.empty_state'))
<span style="{{ implode([
<span @if($description && $getAllowDescription()) x-tooltip="{content: '{{$description}}', theme: $store.theme }" @endif style="{{ implode([
"background-color: rgba(".$colorRGB[0].", ".$colorRGB[1].", ".$colorRGB[2].", 0.2);",
"color: rgba(".$colorRGB[0].", ".$colorRGB[1].", ".$colorRGB[2].", 1);"
]) }}" class="fi-badge flex items-center justify-center gap-x-1 rounded-md text-xs font-medium ring-1 ring-inset px-2 min-w-[theme(spacing.6)] py-1 fi-color-custom bg-custom-50 text-custom-600 ring-custom-600/10 dark:bg-custom-400/10 dark:text-custom-400 dark:ring-custom-400/30 fi-color-primary">
Expand Down
20 changes: 16 additions & 4 deletions src/Components/TypeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

class TypeColumn extends TextColumn
{
public ?string $for = null;
public ?string $type = null;
public string|\Closure|null $for = null;
public string|\Closure|null $type = null;
public bool|\Closure|null $allowDescription = false;

protected string $view = 'filament-types::columns.type-column';

Expand All @@ -26,15 +27,26 @@ public function getType(): ?string
return (string) $this->evaluate($this->type);
}

public function for(string $for): static
public function for(string|\Closure $for): static
{
$this->for = $for;
return $this;
}

public function type(string $type): static
public function type(string|\Closure $type): static
{
$this->type = $type;
return $this;
}

public function allowDescription(bool|\Closure $allowDescription): static
{
$this->allowDescription = $allowDescription;
return $this;
}

public function getAllowDescription(): bool
{
return (bool) $this->evaluate($this->allowDescription);
}
}
2 changes: 1 addition & 1 deletion src/Models/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Type extends CachedModel implements HasMedia
/**
* @var array
*/
protected $fillable = ['for','name', 'key','type', 'description', 'color', 'icon', 'parent_id','model_type','model_id','is_activated','created_at', 'updated_at'];
protected $fillable = ['order', 'for','name', 'key','type', 'description', 'color', 'icon', 'parent_id','model_type','model_id','is_activated','created_at', 'updated_at'];

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
Expand Down
36 changes: 22 additions & 14 deletions src/Resources/TypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use TomatoPHP\FilamentIcons\Components\IconColumn;
use Filament\Resources\Concerns\Translatable;
use Illuminate\Database\Eloquent\Builder;
use TomatoPHP\FilamentTypes\Components\TypeColumn;
use TomatoPHP\FilamentTypes\Facades\FilamentTypes;
use TomatoPHP\FilamentTypes\Resources\TypeResource\Pages;
use TomatoPHP\FilamentTypes\Models\Type;
Expand Down Expand Up @@ -151,21 +152,13 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('type')
->label(trans('filament-types::messages.form.type'))
->searchable(),
Tables\Columns\TextColumn::make('name')
->label(trans('filament-types::messages.form.name'))
->searchable(),
Tables\Columns\TextColumn::make('key')
TypeColumn::make('key')
->for(fn($record) => $record->for)
->type(fn($record) => $record->type)
->label(trans('filament-types::messages.form.key'))
->searchable(),
Tables\Columns\ColorColumn::make('color')
->label(trans('filament-types::messages.form.color'))
->searchable(),
IconColumn::make('icon')
->label(trans('filament-types::messages.form.icon'))
->searchable(),
Tables\Columns\IconColumn::make('is_activated')
->label(trans('filament-types::messages.form.is_activated'))
->boolean(),
Tables\Columns\ToggleColumn::make('is_activated')
->label(trans('filament-types::messages.form.is_activated')),
Tables\Columns\TextColumn::make('created_at')
->label(trans('filament-types::messages.form.created_at'))
->dateTime()
Expand All @@ -177,7 +170,13 @@ public static function table(Table $table): Table
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->groups([
Tables\Grouping\Group::make('for'),
Tables\Grouping\Group::make('type'),
])
->defaultGroup('for')
->defaultSort('order')
->reorderable('order')
->filters([
Tables\Filters\Filter::make('created_at')
->form([
Expand All @@ -192,6 +191,7 @@ public static function table(Table $table): Table
->live(),
Forms\Components\Select::make('type')
->label(trans('filament-types::messages.form.type'))
->disabled(fn(Forms\Get $get) => empty($get('for')))
->options(fn(Forms\Get $get) => $get('for') ? static::getTypes($get('for')) : [])
->searchable(),
Forms\Components\Select::make('parent_id')
Expand All @@ -218,7 +218,15 @@ public static function table(Table $table): Table
})
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\EditAction::make()
->tooltip(__('filament-actions::edit.single.label'))
->iconButton(),
Tables\Actions\ReplicateAction::make()
->tooltip(__('filament-actions::replicate.single.label'))
->iconButton(),
Tables\Actions\DeleteAction::make()
->tooltip(__('filament-actions::delete.single.label'))
->iconButton(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Expand Down

0 comments on commit 3c14f01

Please sign in to comment.