Skip to content

Commit

Permalink
allow closures for format
Browse files Browse the repository at this point in the history
  • Loading branch information
mokhosh committed Oct 24, 2024
1 parent 0573b99 commit f623533
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
8 changes: 4 additions & 4 deletions _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
13 changes: 7 additions & 6 deletions src/FilamentJalaliServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mokhosh\FilamentJalali;

use Closure;
use Filament\Forms\Components\DateTimePicker;
use Filament\Infolists\Components\Component;
use Filament\Infolists\Components\TextEntry;
Expand Down Expand Up @@ -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 {
Expand All @@ -44,23 +45,23 @@ 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')
);
});

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);

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 {
Expand All @@ -71,15 +72,15 @@ 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')
);
});

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);
Expand Down
18 changes: 18 additions & 0 deletions tests/Tests/FilamentJalaliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'));
});

0 comments on commit f623533

Please sign in to comment.