diff --git a/_ide_helper.php b/_ide_helper.php index 3a24006..4467e17 100644 --- a/_ide_helper.php +++ b/_ide_helper.php @@ -9,16 +9,16 @@ class DateTimePicker {} namespace Filament\Tables\Columns { /** - * @method static \Filament\Tables\Columns\TextColumn jalaliDate(?string $format = null, ?string $timezone = null) - * @method static \Filament\Tables\Columns\TextColumn jalaliDateTime(?string $format = null, ?string $timezone = null) + * @method static \Filament\Tables\Columns\TextColumn jalaliDate(string | \Closure | null $format = null, ?string $timezone = null) + * @method static \Filament\Tables\Columns\TextColumn jalaliDateTime(string | \Closure | null $format = null, ?string $timezone = null) */ class TextColumn {} } namespace Filament\Infolists\Components { /** - * @method static \Filament\Infolists\Components\TextEntry jalaliDate(?string $format = null, ?string $timezone = null) - * @method static \Filament\Infolists\Components\TextEntry jalaliDateTime(?string $format = null, ?string $timezone = null) + * @method static \Filament\Infolists\Components\TextEntry jalaliDate(string | \Closure | null $format = null, ?string $timezone = null) + * @method static \Filament\Infolists\Components\TextEntry jalaliDateTime(string | \Closure | null $format = null, ?string $timezone = null) */ class TextEntry {} } diff --git a/src/FilamentJalaliServiceProvider.php b/src/FilamentJalaliServiceProvider.php index 730259d..a302522 100644 --- a/src/FilamentJalaliServiceProvider.php +++ b/src/FilamentJalaliServiceProvider.php @@ -2,6 +2,7 @@ namespace Mokhosh\FilamentJalali; +use Closure; use Filament\Forms\Components\DateTimePicker; use Filament\Infolists\Components\Component; use Filament\Infolists\Components\TextEntry; @@ -33,7 +34,7 @@ public function packageBooted(): void AlpineComponent::make('filament-jalali', __DIR__.'/../resources/js/dist/components/filament-jalali.js'), ], 'mokhosh/filament-jalali'); - TextColumn::macro('jalaliDate', function (?string $format = null, ?string $timezone = null) { + TextColumn::macro('jalaliDate', function (string | Closure | null $format = null, ?string $timezone = null) { $format ??= Table::$defaultDateDisplayFormat; $this->formatStateUsing(static function (Column $column, $state) use ($format, $timezone): ?string { @@ -44,7 +45,7 @@ public function packageBooted(): void return CalendarUtils::convertNumbers( Jalalian::fromCarbon( Carbon::parse($state)->setTimezone($timezone ?? $column->getTimezone()) - )->format($format), + )->format($column->evaluate($format)), ! App::isLocale('fa') ); }); @@ -52,7 +53,7 @@ public function packageBooted(): void return $this; }); - TextColumn::macro('jalaliDateTime', function (?string $format = null, ?string $timezone = null) { + TextColumn::macro('jalaliDateTime', function (string | Closure | null $format = null, ?string $timezone = null) { $format ??= Table::$defaultDateTimeDisplayFormat; $this->jalaliDate($format, $timezone); @@ -60,7 +61,7 @@ public function packageBooted(): void return $this; }); - TextEntry::macro('jalaliDate', function (?string $format = null, ?string $timezone = null) { + TextEntry::macro('jalaliDate', function (string | Closure | null $format = null, ?string $timezone = null) { $format ??= Infolist::$defaultDateDisplayFormat; $this->formatStateUsing(static function (Component $component, $state) use ($format, $timezone): ?string { @@ -71,7 +72,7 @@ public function packageBooted(): void return CalendarUtils::convertNumbers( Jalalian::fromCarbon( Carbon::parse($state)->setTimezone($timezone ?? $component->getTimezone()) - )->format($format), + )->format($component->evaluate($format)), ! App::isLocale('fa') ); }); @@ -79,7 +80,7 @@ public function packageBooted(): void return $this; }); - TextEntry::macro('jalaliDateTime', function (?string $format = null, ?string $timezone = null) { + TextEntry::macro('jalaliDateTime', function (string | Closure | null $format = null, ?string $timezone = null) { $format ??= Infolist::$defaultDateTimeDisplayFormat; $this->jalaliDate($format, $timezone); diff --git a/tests/Tests/FilamentJalaliTest.php b/tests/Tests/FilamentJalaliTest.php index 2db3682..f318fbb 100644 --- a/tests/Tests/FilamentJalaliTest.php +++ b/tests/Tests/FilamentJalaliTest.php @@ -2,6 +2,7 @@ use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\App; @@ -32,3 +33,20 @@ ->formatState(Carbon::parse('1989-10-07')) ->toBe('مهر ۱۵, ۱۳۶۸'); }); + +it('evaluates closures for format', function () { + class User extends Model + { + protected $guarded = []; + } + + expect(TextColumn::make('created_at')) + ->jalaliDateTime(fn ($state) => now()->isSameDay($state) ? 'H:i:s' : 'Y-m-d') + ->record(User::make(['created_at' => '1989-10-07'])) + ->formatState('1989-10-07') + ->toBe('1368-07-15') + ->record(User::make(['created_at' => now()])) + ->formatState(now()) + ->toBe(now()->format('H:i:s')); +}); +