Fix coupon enum formatting in Filament table
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-01-03 15:23:37 +01:00
parent f41578905f
commit 3d0ff40382
2 changed files with 38 additions and 2 deletions

View File

@@ -40,7 +40,7 @@ class CouponsTable
TextColumn::make('type') TextColumn::make('type')
->label(__('Type')) ->label(__('Type'))
->badge() ->badge()
->formatStateUsing(fn ($state) => Str::headline($state)) ->formatStateUsing(fn ($state) => static::formatEnumState($state))
->sortable(), ->sortable(),
TextColumn::make('amount') TextColumn::make('amount')
->label(__('Amount')) ->label(__('Amount'))
@@ -59,7 +59,7 @@ class CouponsTable
->label(__('Status')) ->label(__('Status'))
->badge() ->badge()
->sortable() ->sortable()
->formatStateUsing(fn ($state) => Str::headline($state)), ->formatStateUsing(fn ($state) => static::formatEnumState($state)),
TextColumn::make('starts_at') TextColumn::make('starts_at')
->label(__('Starts')) ->label(__('Starts'))
->date() ->date()
@@ -151,4 +151,21 @@ class CouponsTable
]), ]),
]); ]);
} }
public static function formatEnumState(mixed $state): string
{
if ($state instanceof CouponType || $state instanceof CouponStatus) {
return $state->label();
}
if ($state instanceof \BackedEnum) {
return Str::headline($state->value);
}
if (is_string($state)) {
return Str::headline($state);
}
return '';
}
} }

View File

@@ -0,0 +1,19 @@
<?php
namespace Tests\Unit;
use App\Enums\CouponStatus;
use App\Enums\CouponType;
use App\Filament\Resources\Coupons\Tables\CouponsTable;
use Tests\TestCase;
class CouponsTableTest extends TestCase
{
public function test_it_formats_enum_states(): void
{
$this->assertSame(CouponType::FLAT->label(), CouponsTable::formatEnumState(CouponType::FLAT));
$this->assertSame(CouponStatus::PAUSED->label(), CouponsTable::formatEnumState(CouponStatus::PAUSED));
$this->assertSame('Flat Per Seat', CouponsTable::formatEnumState('flat_per_seat'));
$this->assertSame('', CouponsTable::formatEnumState(null));
}
}