From 84252f7615e3ab2cbef250bd60d4aab064676470 Mon Sep 17 00:00:00 2001 From: lihe6666 Date: Tue, 19 Dec 2023 14:06:52 +0800 Subject: [PATCH] Fixed: DateTimer format24hr() doesn't work #2742 --- resources/js/controllers/datetime_controller.js | 2 +- src/Screen/Fields/DateTimer.php | 6 +++--- tests/Unit/Screen/Fields/DateTimerTest.php | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/resources/js/controllers/datetime_controller.js b/resources/js/controllers/datetime_controller.js index a3aa2aace2..17854ed3f4 100644 --- a/resources/js/controllers/datetime_controller.js +++ b/resources/js/controllers/datetime_controller.js @@ -17,7 +17,7 @@ export default class extends ApplicationController { const configsAttributes = { enableTime: 'enable-time', - time_24hr: 'time-24hr', + time_24hr: 'time_24hr', allowInput: 'allow-input', dateFormat: 'date-format', noCalendar: 'no-calendar', diff --git a/src/Screen/Fields/DateTimer.php b/src/Screen/Fields/DateTimer.php index b69e873363..0c74e9857f 100644 --- a/src/Screen/Fields/DateTimer.php +++ b/src/Screen/Fields/DateTimer.php @@ -45,7 +45,7 @@ class DateTimer extends Field protected $attributes = [ 'class' => 'form-control', 'data-datetime-enable-time' => 'false', - 'data-datetime-time-24hr' => 'false', + 'data-datetime-time_24hr' => 'false', 'data-datetime-allow-input' => 'false', 'data-datetime-date-format' => 'Y-m-d H:i:S', 'data-datetime-no-calendar' => 'false', @@ -86,7 +86,7 @@ class DateTimer extends Field 'tabindex', 'value', 'data-datetime-enable-time', - 'data-datetime-time-24hr', + 'data-datetime-time_24hr', 'data-datetime-allow-input', 'data-datetime-date-format', 'data-datetime-no-calendar', @@ -116,7 +116,7 @@ public function enableTime(bool $time = true): self */ public function format24hr(bool $time = true): self { - $this->set('data-datetime-time-24hr', var_export($time, true)); + $this->set('data-datetime-time_24hr', var_export($time, true)); return $this; } diff --git a/tests/Unit/Screen/Fields/DateTimerTest.php b/tests/Unit/Screen/Fields/DateTimerTest.php index c5840599d5..2acd6a6282 100644 --- a/tests/Unit/Screen/Fields/DateTimerTest.php +++ b/tests/Unit/Screen/Fields/DateTimerTest.php @@ -67,4 +67,16 @@ public function testWithoutServerFormat(): void $this->assertStringContainsString('value="2021-02-01 03:45:27"', $view); } + + public function testEnableTimeAndFormat24hr() + { + $field = DateTimer::make('date') + ->format('Y-m-d H:i:s') + ->enableTime() + ->format24hr(); + + $view = self::renderField($field); + + $this->assertStringContainsString('data-datetime-time_24hr="true"', $view); + } }