Skip to content

Commit 31840dc

Browse files
committed
[flatpickr] Fix flatpickr calendar buttons
1 parent be1ff47 commit 31840dc

File tree

3 files changed

+15
-45
lines changed

3 files changed

+15
-45
lines changed

assets/js/flatpicker.js

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,14 @@ import 'flatpickr';
22
import 'flatpickr/dist/flatpickr.min.css';
33
import l10n from "flatpickr/dist/l10n";
44

5-
flatpickr.defaultConfig.animate = window.navigator.userAgent.indexOf('MSIE') === -1;
6-
let lang = document.documentElement.getAttribute('lang') || 'en';
5+
const lang = document.documentElement.getAttribute('lang') || 'en';
76
const Locale = l10n[`${lang}`] || l10n.default;
87
flatpickr.localize(Locale);
9-
const configs = {
10-
standard: {
11-
enableTime: true,
12-
dateFormat: "Y-m-d H:i",
13-
allowInput: true,
14-
time_24hr: true,
15-
defaultHour: 24,
16-
parseDate: (datestr, format) => {
17-
return flatpickr.parseDate(datestr, format);
18-
},
19-
formatDate: (date, format, locale) => {
20-
return flatpickr.formatDate(date, format);
21-
}
22-
}
23-
};
248

25-
const flatpickrs = document.querySelectorAll(".flatpickr");
26-
for (let i = 0; i < flatpickrs.length; i++) {
27-
let element = flatpickrs[i];
28-
let configValue = configs[element.getAttribute("data-flatpickr-class")] || {};
29-
// Overrides the default format with the one sent by data attribute
30-
configValue.dateFormat = element.getAttribute("data-date-format") || 'Y-m-d H:i';
31-
// ...and then initialize the flatpickr
32-
flatpickr(element, configValue);
33-
}
9+
flatpickr(".flatpickr", {
10+
allowInput: true,
11+
dateFormat: "Y-m-d H:i",
12+
enableTime: true,
13+
time_24hr: true,
14+
wrap: true
15+
});

src/Form/Type/DateTimePickerType.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
1616
use Symfony\Component\OptionsResolver\OptionsResolver;
17-
use function Symfony\Component\String\u;
1817

1918
/**
2019
* Defines the custom form field type used to manipulate datetime values across
@@ -32,16 +31,12 @@ public function configureOptions(OptionsResolver $resolver): void
3231
$resolver->setDefaults([
3332
'widget' => 'single_text',
3433
'input' => 'datetime_immutable',
35-
// if true, the browser will display the native date picker widget
34+
// If true, the browser will display the native date picker widget
3635
// however, this app uses a custom JavaScript widget, so it must be set to false
3736
'html5' => false,
38-
// adds a class that can be selected in JavaScript
37+
// Add attributes for flatpickr usage that can be selected in JavaScript
3938
'attr' => [
40-
'class' => 'flatpickr',
41-
// Attributes for flatpickr usage
42-
'data-flatpickr-class' => 'standard',
43-
'data-date-locale' => u(\Locale::getDefault())->replace('_', '-')->lower(),
44-
'data-date-format' => 'Y-m-d H:i',
39+
'data-input' => 'data-input',
4540
],
4641
'format' => 'yyyy-MM-dd HH:mm',
4742
'input_format' => 'Y-m-d H:i',

templates/form/fields.html.twig

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,16 @@
88
#}
99

1010
{% block date_time_picker_widget %}
11-
<div class="input-group date">
11+
<div class="input-group flatpickr">
1212
{{ block('datetime_widget') }}
13-
<div class="input-group-append">
14-
<button type="button" class="btn btn-outline-secondary">
15-
<twig:ux:icon name="tabler:calendar-month" aria-hidden="false" aria-label="Pick a date"/>
16-
</button>
17-
</div>
13+
<button class="btn btn-outline-secondary" data-toggle type="button"><twig:ux:icon name="tabler:calendar-month" /></button>
14+
<button class="btn btn-outline-danger" data-clear type="button"><twig:ux:icon name="tabler:trash" /></button>
1815
</div>
1916
{% endblock %}
2017

2118
{% block tags_input_widget %}
2219
<div class="input-group">
2320
{{ form_widget(form, {'attr': {'data-toggle': 'tagsinput', 'data-tags': tags|json_encode}}) }}
24-
<div class="input-group-append">
25-
<span class="input-group-text">
26-
<twig:ux:icon name="tabler:tags"/>
27-
</span>
28-
</div>
21+
<span class="input-group-text"><twig:ux:icon name="tabler:tags" /></span>
2922
</div>
3023
{% endblock %}

0 commit comments

Comments
 (0)