From c79c862e9e9239190266acc8f4661728dfa7207a Mon Sep 17 00:00:00 2001 From: Lukas Frey Date: Wed, 5 Oct 2022 17:45:59 +0200 Subject: [PATCH 1/4] feat: added generic timestamp field --- src/Forms/Timestamp.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/Forms/Timestamp.php diff --git a/src/Forms/Timestamp.php b/src/Forms/Timestamp.php new file mode 100644 index 0000000..6d609b3 --- /dev/null +++ b/src/Forms/Timestamp.php @@ -0,0 +1,15 @@ +label($label ?? tr("time.$column")) + ->content(fn ($record): string => $record?->$column ? $record->$column->diffForHumans() : '-'); + } +} From cebcf9107ffc49671e1ab117acb9a27a39bcd8f0 Mon Sep 17 00:00:00 2001 From: Lukas Frey Date: Wed, 5 Oct 2022 17:47:06 +0200 Subject: [PATCH 2/4] feat: added DeletedAt field, updated fields to use generic field --- resources/lang/en/translations.php | 1 + src/Forms/CreatedAt.php | 4 +--- src/Forms/DeletedAt.php | 13 +++++++++++++ src/Forms/UpdatedAt.php | 4 +--- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/Forms/DeletedAt.php diff --git a/resources/lang/en/translations.php b/resources/lang/en/translations.php index f80bb71..4bb478a 100644 --- a/resources/lang/en/translations.php +++ b/resources/lang/en/translations.php @@ -4,5 +4,6 @@ 'time' => [ 'created_at' => 'Created at', 'updated_at' => 'Updated at', + 'deleted_at' => 'Deleted at', ] ]; diff --git a/src/Forms/CreatedAt.php b/src/Forms/CreatedAt.php index b2149c9..c60586b 100644 --- a/src/Forms/CreatedAt.php +++ b/src/Forms/CreatedAt.php @@ -8,8 +8,6 @@ class CreatedAt { public static function make(): 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'); } } diff --git a/src/Forms/DeletedAt.php b/src/Forms/DeletedAt.php new file mode 100644 index 0000000..597235b --- /dev/null +++ b/src/Forms/DeletedAt.php @@ -0,0 +1,13 @@ +label(tr('time.updated_at')) - ->content(fn ($record): string => $record?->updated_at ? $record->updated_at->diffForHumans() : '-'); + return Timestamp::make('updated_at'); } } From 0f0fd78d8b3112ee135a384d0efccc1cf00083e8 Mon Sep 17 00:00:00 2001 From: Lukas Frey Date: Wed, 5 Oct 2022 17:59:47 +0200 Subject: [PATCH 3/4] refactor: added possibility to set label for predefined timestamps --- src/Forms/CreatedAt.php | 4 ++-- src/Forms/DeletedAt.php | 4 ++-- src/Forms/UpdatedAt.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Forms/CreatedAt.php b/src/Forms/CreatedAt.php index c60586b..38ac04b 100644 --- a/src/Forms/CreatedAt.php +++ b/src/Forms/CreatedAt.php @@ -6,8 +6,8 @@ class CreatedAt { - public static function make(): Placeholder + public static function make(string $label = null): Placeholder { - return Timestamp::make('created_at'); + return Timestamp::make('created_at', $label); } } diff --git a/src/Forms/DeletedAt.php b/src/Forms/DeletedAt.php index 597235b..7129dc1 100644 --- a/src/Forms/DeletedAt.php +++ b/src/Forms/DeletedAt.php @@ -6,8 +6,8 @@ class DeletedAt { - public static function make(): Placeholder + public static function make(string $label = null): Placeholder { - return Timestamp::make('deleted_at'); + return Timestamp::make('deleted_at', $label); } } diff --git a/src/Forms/UpdatedAt.php b/src/Forms/UpdatedAt.php index eea8ff2..04410fc 100644 --- a/src/Forms/UpdatedAt.php +++ b/src/Forms/UpdatedAt.php @@ -6,8 +6,8 @@ class UpdatedAt { - public static function make(): Placeholder + public static function make(string $label = null): Placeholder { - return Timestamp::make('updated_at'); + return Timestamp::make('updated_at', $label); } } From 0c0aa4717dce414ecdf38ec20c2809edb2f562e9 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:30:12 +0200 Subject: [PATCH 4/4] Limit to available translations --- src/Forms/Timestamp.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Forms/Timestamp.php b/src/Forms/Timestamp.php index 6d609b3..6d52821 100644 --- a/src/Forms/Timestamp.php +++ b/src/Forms/Timestamp.php @@ -9,7 +9,18 @@ class Timestamp public static function make(string $column, string $label = null): Placeholder { return Placeholder::make($column) - ->label($label ?? tr("time.$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() : '-'); } }