From 5cc4aef2e51670b569d8613aa815cf800ffa294f Mon Sep 17 00:00:00 2001 From: Jan Rosier Date: Wed, 1 Oct 2025 19:23:02 +0200 Subject: [PATCH] [flatpickr] Fix flatpickr calendar buttons and localization --- assets/js/flatpicker.js | 38 ++++++++-------------------- src/Form/Type/DateTimePickerType.php | 11 +++----- templates/form/fields.html.twig | 15 +++-------- 3 files changed, 17 insertions(+), 47 deletions(-) diff --git a/assets/js/flatpicker.js b/assets/js/flatpicker.js index d6bae2afb..07c5c6650 100644 --- a/assets/js/flatpicker.js +++ b/assets/js/flatpicker.js @@ -2,32 +2,14 @@ import 'flatpickr'; import 'flatpickr/dist/flatpickr.min.css'; import l10n from "flatpickr/dist/l10n"; -flatpickr.defaultConfig.animate = window.navigator.userAgent.indexOf('MSIE') === -1; -let lang = document.documentElement.getAttribute('lang') || 'en'; -const Locale = l10n[`${lang}`] || l10n.default; -flatpickr.localize(Locale); -const configs = { - standard: { - enableTime: true, - dateFormat: "Y-m-d H:i", - allowInput: true, - time_24hr: true, - defaultHour: 24, - parseDate: (datestr, format) => { - return flatpickr.parseDate(datestr, format); - }, - formatDate: (date, format, locale) => { - return flatpickr.formatDate(date, format); - } - } -}; +const lang = document.documentElement.getAttribute("lang") || "en"; +const Locale = l10n.default[lang] || l10n.default; -const flatpickrs = document.querySelectorAll(".flatpickr"); -for (let i = 0; i < flatpickrs.length; i++) { - let element = flatpickrs[i]; - let configValue = configs[element.getAttribute("data-flatpickr-class")] || {}; - // Overrides the default format with the one sent by data attribute - configValue.dateFormat = element.getAttribute("data-date-format") || 'Y-m-d H:i'; - // ...and then initialize the flatpickr - flatpickr(element, configValue); -} +flatpickr(".flatpickr", { + allowInput: true, + dateFormat: "Y-m-d H:i", + enableTime: true, + locale: Locale, + time_24hr: true, + wrap: true +}); diff --git a/src/Form/Type/DateTimePickerType.php b/src/Form/Type/DateTimePickerType.php index 81223c2ae..8c6f6040f 100644 --- a/src/Form/Type/DateTimePickerType.php +++ b/src/Form/Type/DateTimePickerType.php @@ -14,7 +14,6 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\OptionsResolver\OptionsResolver; -use function Symfony\Component\String\u; /** * Defines the custom form field type used to manipulate datetime values across @@ -32,16 +31,12 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setDefaults([ 'widget' => 'single_text', 'input' => 'datetime_immutable', - // if true, the browser will display the native date picker widget + // If true, the browser will display the native date picker widget // however, this app uses a custom JavaScript widget, so it must be set to false 'html5' => false, - // adds a class that can be selected in JavaScript + // Add attributes for flatpickr usage that can be selected in JavaScript 'attr' => [ - 'class' => 'flatpickr', - // Attributes for flatpickr usage - 'data-flatpickr-class' => 'standard', - 'data-date-locale' => u(\Locale::getDefault())->replace('_', '-')->lower(), - 'data-date-format' => 'Y-m-d H:i', + 'data-input' => 'data-input', ], 'format' => 'yyyy-MM-dd HH:mm', 'input_format' => 'Y-m-d H:i', diff --git a/templates/form/fields.html.twig b/templates/form/fields.html.twig index 9fd41701f..4ba9dcdf3 100644 --- a/templates/form/fields.html.twig +++ b/templates/form/fields.html.twig @@ -8,23 +8,16 @@ #} {% block date_time_picker_widget %} -
+
{{ block('datetime_widget') }} -
- -
+ +
{% endblock %} {% block tags_input_widget %}
{{ form_widget(form, {'attr': {'data-toggle': 'tagsinput', 'data-tags': tags|json_encode}}) }} -
- - - -
+
{% endblock %}