Skip to content

Commit

Permalink
Merge pull request #10 from LukasFreyCZ/feature/generic-timestamp
Browse files Browse the repository at this point in the history
Feature/generic timestamp
  • Loading branch information
ralphjsmit authored Oct 6, 2022
2 parents 6bd305d + 0c0aa47 commit 596b587
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
'time' => [
'created_at' => 'Created at',
'updated_at' => 'Updated at',
'deleted_at' => 'Deleted at',
]
];
6 changes: 2 additions & 4 deletions src/Forms/CreatedAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class CreatedAt
{
public static function make(): Placeholder
public static function make(string $label = null): Placeholder
{
return Placeholder::make('created_at')
->label(tr('time.created_at'))
->content(fn ($record): string => $record?->created_at ? $record->created_at->diffForHumans() : '-');
return Timestamp::make('created_at', $label);
}
}
13 changes: 13 additions & 0 deletions src/Forms/DeletedAt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RalphJSmit\Filament\Components\Forms;

use Filament\Forms\Components\Placeholder;

class DeletedAt
{
public static function make(string $label = null): Placeholder
{
return Timestamp::make('deleted_at', $label);
}
}
26 changes: 26 additions & 0 deletions src/Forms/Timestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace RalphJSmit\Filament\Components\Forms;

use Filament\Forms\Components\Placeholder;

class Timestamp
{
public static function make(string $column, string $label = null): Placeholder
{
return Placeholder::make($column)
->label(function () use ($column, $label) {
if ($label) {
return $label;
}

return match ( $column ) {
'created_at' => tr('time.created_at'),
'updated_at' => tr('time.updated_at'),
'deleted_at' => tr('time.deleted_at'),
default => null,
};
})
->content(fn ($record): string => $record?->$column ? $record->$column->diffForHumans() : '-');
}
}
6 changes: 2 additions & 4 deletions src/Forms/UpdatedAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class UpdatedAt
{
public static function make(): Placeholder
public static function make(string $label = null): Placeholder
{
return Placeholder::make('updated_at')
->label(tr('time.updated_at'))
->content(fn ($record): string => $record?->updated_at ? $record->updated_at->diffForHumans() : '-');
return Timestamp::make('updated_at', $label);
}
}

0 comments on commit 596b587

Please sign in to comment.