flatpickr-ce 4.8.0 → 4.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,6 +42,29 @@ npm install flatpickr@npm:flatpickr-ce
42
42
 
43
43
  ## What's changed since flatpickr 4.6.13
44
44
 
45
+ ### 4.9.0
46
+
47
+ - New yearSelect plugin: a year-only picker modeled on monthSelect, for single, multiple and range mode ([#5](https://github.com/colinli007/flatpickr-ce/issues/5), [#6](https://github.com/colinli007/flatpickr-ce/pull/6))
48
+ - In range mode, a same-day range written as one date reads back as that range instead of a start that a click outside drops ([#3010](https://github.com/flatpickr/flatpickr/issues/3010))
49
+ - rangePlugin with `allowInput` commits what is typed into its two inputs as one range: a click outside keeps the range, and `onChange` fires once per commit
50
+ - A date or time with a field out of range, such as `02/31/2025` or `24:00`, is invalid instead of rolling over ([#2988](https://github.com/flatpickr/flatpickr/issues/2988))
51
+ - Month names, weekday names and AM/PM are parsed in any case ([#3057](https://github.com/flatpickr/flatpickr/pull/3057))
52
+ - `allowInvalidPreload` no longer throws on a value it can't parse ([#3000](https://github.com/flatpickr/flatpickr/issues/3000), [#2760](https://github.com/flatpickr/flatpickr/issues/2760), [#2856](https://github.com/flatpickr/flatpickr/issues/2856))
53
+ - `clear()`, and `set("minDate")` or `set("maxDate")`, keep the view within the current limits ([#3002](https://github.com/flatpickr/flatpickr/issues/3002))
54
+ - With `showMonths > 1`, picking a day in a shown month of the next year keeps the view ([#3003](https://github.com/flatpickr/flatpickr/issues/3003))
55
+ - Enter and the arrow keys in the month dropdown no longer change the year or move the focus ([#3045](https://github.com/flatpickr/flatpickr/pull/3045))
56
+ - Seconds are only clamped to `minTime`/`maxTime` within their hour ([#3023](https://github.com/flatpickr/flatpickr/issues/3023), [#3024](https://github.com/flatpickr/flatpickr/pull/3024))
57
+ - With `wrap`, `static` and `altInput`, the altInput stays next to the input ([#3037](https://github.com/flatpickr/flatpickr/issues/3037))
58
+ - The Hebrew range separator is " עד ", and the Breton name of Wednesday is fixed ([#3014](https://github.com/flatpickr/flatpickr/issues/3014), [#3088](https://github.com/flatpickr/flatpickr/pull/3088))
59
+
60
+ Behavior changes to check when upgrading:
61
+
62
+ - In range mode without `enableTime`, a string with one date (the input's value, `defaultDate`, `setDate`, typed text) is a same-day range, so the next day picked starts a new range instead of finishing it. With rangePlugin, `setDate("2024-04-19")` sets a same-day range in both inputs.
63
+ - A date with a field out of range is reported to `errorHandler` and selects nothing, also in `minDate`, `maxDate`, `enable` and `disable`. `maxTime: "24:00"` is dropped; use `"23:59"`.
64
+ - Hebrew ranges saved with " אל " read as a same-day range of their first date. Replace the separator in stored values, or keep it with `locale: { ...Hebrew, rangeSeparator: " אל " }`.
65
+ - With rangePlugin and `allowInput`, an emptied or unparsable end leaves the other end as both ends instead of clearing the range, and an end typed before the start takes the start along.
66
+ - With `wrap`, `static` and `altInput`, the altInput is inside the wrap element, which changes DOM snapshots.
67
+
45
68
  ### 4.8.0
46
69
 
47
70
  - A typed value is committed the same way on Enter, blur and a click outside, and `onChange` now fires when a click outside commits it ([#2941](https://github.com/flatpickr/flatpickr/issues/2941), [#3136](https://github.com/flatpickr/flatpickr/pull/3136))
@@ -114,6 +137,10 @@ Framework addons (they depend on the `flatpickr` package, so use the alias above
114
137
  * [vue-flatpickr component](https://github.com/ankurk91/vue-flatpickr-component)
115
138
  * [lit-flatpickr component](https://github.com/Matsuuu/lit-flatpickr)
116
139
 
140
+ ### Options are trusted
141
+
142
+ A few options are inserted into the calendar as HTML, not text: `prevArrow`, `nextArrow`, what `getWeek` returns, the locale's short weekday names, and confirmDate's `confirmIcon`. Like the rest of the config, they are meant to come from your code: don't build them from user input or other untrusted data without escaping it.
143
+
117
144
 
118
145
  ## Contributing
119
146
 
package/dist/esm/index.js CHANGED
@@ -42,6 +42,8 @@ function FlatpickrInstance(element, instanceConfig) {
42
42
  self._bind = bind;
43
43
  self._setHoursFromDate = setHoursFromDate;
44
44
  self._positionCalendar = positionCalendar;
45
+ self._showValue = showValue;
46
+ self._typedValue = typedValue;
45
47
  self.changeMonth = changeMonth;
46
48
  self.changeYear = changeYear;
47
49
  self.clear = clear;
@@ -200,27 +202,29 @@ function FlatpickrInstance(element, instanceConfig) {
200
202
  ? self.config.maxTime
201
203
  : self.config.maxDate;
202
204
  hours = Math.min(hours, maxTime.getHours());
203
- if (hours === maxTime.getHours())
205
+ if (hours === maxTime.getHours()) {
204
206
  minutes = Math.min(minutes, maxTime.getMinutes());
205
- if (minutes === maxTime.getMinutes())
206
- seconds = Math.min(seconds, maxTime.getSeconds());
207
+ if (minutes === maxTime.getMinutes())
208
+ seconds = Math.min(seconds, maxTime.getSeconds());
209
+ }
207
210
  }
208
211
  if (limitMinHours) {
209
212
  var minTime = self.config.minTime !== undefined
210
213
  ? self.config.minTime
211
214
  : self.config.minDate;
212
215
  hours = Math.max(hours, minTime.getHours());
213
- if (hours === minTime.getHours() && minutes < minTime.getMinutes())
214
- minutes = minTime.getMinutes();
215
- if (minutes === minTime.getMinutes())
216
- seconds = Math.max(seconds, minTime.getSeconds());
216
+ if (hours === minTime.getHours()) {
217
+ minutes = Math.max(minutes, minTime.getMinutes());
218
+ if (minutes === minTime.getMinutes())
219
+ seconds = Math.max(seconds, minTime.getSeconds());
220
+ }
217
221
  }
218
222
  }
219
223
  setHours(hours, minutes, seconds);
220
224
  }
221
225
  function setHoursFromDate(dateObj) {
222
226
  var date = dateObj || self.latestSelectedDateObj;
223
- if (date && date instanceof Date) {
227
+ if (date) {
224
228
  setHours(date.getHours(), date.getMinutes(), date.getSeconds());
225
229
  }
226
230
  }
@@ -241,6 +245,8 @@ function FlatpickrInstance(element, instanceConfig) {
241
245
  }
242
246
  function onYearInput(event) {
243
247
  var eventTarget = getEventTarget(event);
248
+ if (self.yearElements.indexOf(eventTarget) === -1)
249
+ return;
244
250
  var year = parseInt(eventTarget.value) + (event.delta || 0);
245
251
  if (year / 1000 > 1 ||
246
252
  (event.key === "Enter" && !/[^\d]/.test(year.toString()))) {
@@ -332,15 +338,18 @@ function FlatpickrInstance(element, instanceConfig) {
332
338
  });
333
339
  }
334
340
  }
341
+ function withinLimits(date) {
342
+ var _a = self.config, minDate = _a.minDate, maxDate = _a.maxDate;
343
+ if (minDate && minDate.getTime() > date.getTime())
344
+ return minDate;
345
+ if (maxDate && maxDate.getTime() < date.getTime())
346
+ return maxDate;
347
+ return date;
348
+ }
335
349
  function jumpToDate(jumpDate, triggerChange) {
336
350
  var jumpTo = jumpDate !== undefined
337
351
  ? self.parseDate(jumpDate)
338
- : self.latestSelectedDateObj ||
339
- (self.config.minDate && self.config.minDate > self.now
340
- ? self.config.minDate
341
- : self.config.maxDate && self.config.maxDate < self.now
342
- ? self.config.maxDate
343
- : self.now);
352
+ : self.latestSelectedDateObj || withinLimits(self.now);
344
353
  var oldYear = self.currentYear;
345
354
  var oldMonth = self.currentMonth;
346
355
  try {
@@ -424,8 +433,8 @@ function FlatpickrInstance(element, instanceConfig) {
424
433
  if (self.element.parentNode)
425
434
  self.element.parentNode.insertBefore(wrapper, self.element);
426
435
  wrapper.appendChild(self.element);
427
- if (self.altInput)
428
- wrapper.appendChild(self.altInput);
436
+ if (self.altInput && self.input.parentNode)
437
+ self.input.parentNode.insertBefore(self.altInput, self.input.nextSibling);
429
438
  wrapper.appendChild(self.calendarContainer);
430
439
  }
431
440
  }
@@ -868,16 +877,15 @@ function FlatpickrInstance(element, instanceConfig) {
868
877
  function clear(triggerChangeEvent, toInitial) {
869
878
  if (triggerChangeEvent === void 0) { triggerChangeEvent = true; }
870
879
  if (toInitial === void 0) { toInitial = true; }
871
- self.input.value = "";
872
- if (self.altInput !== undefined)
873
- self.altInput.value = "";
874
880
  if (self.mobileInput !== undefined)
875
881
  self.mobileInput.value = "";
876
882
  self.selectedDates = [];
877
883
  self.latestSelectedDateObj = undefined;
884
+ self._showValue();
878
885
  if (toInitial === true) {
879
- self.currentYear = self._initialDate.getFullYear();
880
- self.currentMonth = self._initialDate.getMonth();
886
+ var initialDate = withinLimits(self._initialDate);
887
+ self.currentYear = initialDate.getFullYear();
888
+ self.currentMonth = initialDate.getMonth();
881
889
  }
882
890
  if (self.config.enableTime === true) {
883
891
  var _a = getDefaultHours(self.config), hours = _a.hours, minutes = _a.minutes, seconds = _a.seconds;
@@ -1080,15 +1088,21 @@ function FlatpickrInstance(element, instanceConfig) {
1080
1088
  !(e.relatedTarget && isCalendarElem(e.relatedTarget)))
1081
1089
  announceCommit(commitTypedValue());
1082
1090
  }
1091
+ function typedValue() {
1092
+ return self._input.value.trimEnd() === getDateStr()
1093
+ ? undefined
1094
+ : self._input.value;
1095
+ }
1083
1096
  function commitTypedValue() {
1084
- if (self._input.value.trimEnd() === getDateStr())
1097
+ var typed = self._typedValue();
1098
+ if (typed === undefined)
1085
1099
  return undefined;
1086
1100
  var committed = {
1087
1101
  times: getSelectedTimes(),
1088
1102
  year: self.currentYear,
1089
1103
  month: self.currentMonth,
1090
1104
  };
1091
- self.setDate(self._input.value, false, self.config.altInput ? self.config.altFormat : self.config.dateFormat);
1105
+ self.setDate(typed, false, self.config.altInput ? self.config.altFormat : self.config.dateFormat);
1092
1106
  if (self.hourElement !== undefined) {
1093
1107
  setHoursFromInputs();
1094
1108
  updateValue(false);
@@ -1116,6 +1130,9 @@ function FlatpickrInstance(element, instanceConfig) {
1116
1130
  var allowInput = self.config.allowInput;
1117
1131
  var allowKeydown = self.isOpen && (!allowInput || !isInput);
1118
1132
  var allowInlineKeydown = self.config.inline && isInput && !allowInput;
1133
+ var isMonthDropdownArrowKey = eventTarget === self.monthsDropdownContainer &&
1134
+ e.keyCode >= 37 &&
1135
+ e.keyCode <= 40;
1119
1136
  if (e.keyCode === 13 && isInput) {
1120
1137
  if (allowInput) {
1121
1138
  announceCommit(commitTypedValue());
@@ -1126,9 +1143,10 @@ function FlatpickrInstance(element, instanceConfig) {
1126
1143
  self.open();
1127
1144
  }
1128
1145
  }
1129
- else if (isCalendarElem(eventTarget) ||
1130
- allowKeydown ||
1131
- allowInlineKeydown) {
1146
+ else if (!isMonthDropdownArrowKey &&
1147
+ (isCalendarElem(eventTarget) ||
1148
+ allowKeydown ||
1149
+ allowInlineKeydown)) {
1132
1150
  var isTimeObj = !!self.timeContainer &&
1133
1151
  self.timeContainer.contains(eventTarget);
1134
1152
  switch (e.keyCode) {
@@ -1358,6 +1376,10 @@ function FlatpickrInstance(element, instanceConfig) {
1358
1376
  if (self.selectedDates) {
1359
1377
  if (!self.config.allowInvalidPreload)
1360
1378
  self.selectedDates = filterEnabledDates(self.selectedDates, true);
1379
+ if (self.latestSelectedDateObj &&
1380
+ self.selectedDates.indexOf(self.latestSelectedDateObj) === -1)
1381
+ self.latestSelectedDateObj =
1382
+ self.selectedDates[self.selectedDates.length - 1];
1361
1383
  if (!self.selectedDates.length && type === "min")
1362
1384
  setHoursFromDate(dateObj);
1363
1385
  updateValue();
@@ -1514,7 +1536,7 @@ function FlatpickrInstance(element, instanceConfig) {
1514
1536
  .slice()
1515
1537
  .sort(function (a, b) { return b.length - a.length; })
1516
1538
  .join("|") + ")";
1517
- tokenRegex.K = "(" + self.l10n.amPM[0] + "|" + self.l10n.amPM[1] + "|" + self.l10n.amPM[0].toLowerCase() + "|" + self.l10n.amPM[1].toLowerCase() + ")";
1539
+ tokenRegex.K = "(" + self.l10n.amPM[0] + "|" + self.l10n.amPM[1] + ")";
1518
1540
  if (userConfig.time_24hr === undefined &&
1519
1541
  flatpickr.defaultConfig.time_24hr === undefined) {
1520
1542
  self.config.time_24hr = self.l10n.time_24hr;
@@ -1638,9 +1660,10 @@ function FlatpickrInstance(element, instanceConfig) {
1638
1660
  return;
1639
1661
  var target = t;
1640
1662
  var selectedDate = (self.latestSelectedDateObj = new Date(target.dateObj.getTime()));
1641
- var shouldChangeMonth = (selectedDate.getMonth() < self.currentMonth ||
1642
- selectedDate.getMonth() >
1643
- self.currentMonth + self.config.showMonths - 1) &&
1663
+ var firstMonth = self.currentYear * 12 + self.currentMonth;
1664
+ var selectedMonth = selectedDate.getFullYear() * 12 + selectedDate.getMonth();
1665
+ var shouldChangeMonth = (selectedMonth < firstMonth ||
1666
+ selectedMonth > firstMonth + self.config.showMonths - 1) &&
1644
1667
  self.config.mode !== "range";
1645
1668
  self.selectedDateElem = target;
1646
1669
  if (self.config.mode === "single")
@@ -1765,6 +1788,8 @@ function FlatpickrInstance(element, instanceConfig) {
1765
1788
  dates = inputDate
1766
1789
  .split(self.l10n.rangeSeparator)
1767
1790
  .map(function (date) { return self.parseDate(date, format); });
1791
+ if (dates.length === 1 && !self.config.enableTime)
1792
+ dates.push(self.parseDate(inputDate, format));
1768
1793
  break;
1769
1794
  default:
1770
1795
  break;
@@ -1772,9 +1797,10 @@ function FlatpickrInstance(element, instanceConfig) {
1772
1797
  }
1773
1798
  else
1774
1799
  self.config.errorHandler(new Error("Invalid date supplied: " + JSON.stringify(inputDate)));
1800
+ var parsed = dates.filter(function (d) { return d instanceof Date; });
1775
1801
  self.selectedDates = self.config.allowInvalidPreload
1776
- ? dates
1777
- : filterEnabledDates(dates.filter(function (d) { return d instanceof Date; }), false);
1802
+ ? parsed
1803
+ : filterEnabledDates(parsed, false);
1778
1804
  if (self.config.mode === "range")
1779
1805
  self.selectedDates.sort(function (a, b) { return a.getTime() - b.getTime(); });
1780
1806
  }
@@ -1832,13 +1858,7 @@ function FlatpickrInstance(element, instanceConfig) {
1832
1858
  self._initialDate =
1833
1859
  self.selectedDates.length > 0
1834
1860
  ? self.selectedDates[0]
1835
- : self.config.minDate &&
1836
- self.config.minDate.getTime() > self.now.getTime()
1837
- ? self.config.minDate
1838
- : self.config.maxDate &&
1839
- self.config.maxDate.getTime() < self.now.getTime()
1840
- ? self.config.maxDate
1841
- : self.now;
1861
+ : withinLimits(self.now);
1842
1862
  self.currentYear = self._initialDate.getFullYear();
1843
1863
  self.currentMonth = self._initialDate.getMonth();
1844
1864
  if (self.selectedDates.length > 0)
@@ -1956,9 +1976,7 @@ function FlatpickrInstance(element, instanceConfig) {
1956
1976
  }
1957
1977
  function isDateSelected(date) {
1958
1978
  for (var i = 0; i < self.selectedDates.length; i++) {
1959
- var selectedDate = self.selectedDates[i];
1960
- if (selectedDate instanceof Date &&
1961
- compareDates(selectedDate, date) === 0)
1979
+ if (compareDates(self.selectedDates[i], date) === 0)
1962
1980
  return "" + i;
1963
1981
  }
1964
1982
  return false;
@@ -2021,12 +2039,15 @@ function FlatpickrInstance(element, instanceConfig) {
2021
2039
  self.formatDate(currentDate, self.mobileFormatStr) !== mobileValue)
2022
2040
  self.mobileInput.value = mobileValue;
2023
2041
  }
2042
+ self._showValue();
2043
+ if (triggerChange !== false)
2044
+ triggerEvent("onValueUpdate");
2045
+ }
2046
+ function showValue() {
2024
2047
  self.input.value = getDateStr(self.config.dateFormat);
2025
2048
  if (self.altInput !== undefined) {
2026
2049
  self.altInput.value = getDateStr(self.config.altFormat);
2027
2050
  }
2028
- if (triggerChange !== false)
2029
- triggerEvent("onValueUpdate");
2030
2051
  }
2031
2052
  function onMonthNavClick(e) {
2032
2053
  var eventTarget = getEventTarget(e);
@@ -11,7 +11,7 @@ export var Breton = {
11
11
  "Disul",
12
12
  "Dilun",
13
13
  "Dimeurzh",
14
- "Dimerc'hed",
14
+ "Dimerc'her",
15
15
  "Diriaou",
16
16
  "Digwener",
17
17
  "Disadorn",
@@ -38,7 +38,7 @@ export var Hebrew = {
38
38
  "דצמבר",
39
39
  ],
40
40
  },
41
- rangeSeparator: " אל ",
41
+ rangeSeparator: " עד ",
42
42
  time_24hr: true,
43
43
  };
44
44
  fp.l10ns.he = Hebrew;
@@ -10,6 +10,27 @@ function rangePlugin(config) {
10
10
  return function (fp) {
11
11
  var dateFormat = "", secondInput, _secondInputFocused, _prevDates = [];
12
12
  var firstInputAnchor = function () { return fp.config.positionElement || fp._input; };
13
+ var shownValue = function () {
14
+ var _a = fp.selectedDates.map(function (d) {
15
+ return fp.formatDate(d, dateFormat);
16
+ }), _b = _a[0], first = _b === void 0 ? "" : _b, _c = _a[1], second = _c === void 0 ? "" : _c;
17
+ return [first, second];
18
+ };
19
+ var typedValue = function () {
20
+ var shown = shownValue();
21
+ var ends = [fp._input.value, secondInput.value];
22
+ var typed = [0, 1].filter(function (i) { return ends[i] !== shown[i]; });
23
+ if (typed.length === 0)
24
+ return undefined;
25
+ var dates = ends.map(function (text) { return fp.parseDate(text, dateFormat); });
26
+ typed.forEach(function (i) {
27
+ if (!dates[i])
28
+ ends[i] = dates[1 - i] ? ends[1 - i] : "";
29
+ });
30
+ if (typed.length === 1 && dates[0] && dates[1] && dates[0] > dates[1])
31
+ ends[1 - typed[0]] = ends[typed[0]];
32
+ return ends.join(fp.l10n.rangeSeparator);
33
+ };
13
34
  var createSecondInput = function () {
14
35
  if (config.input) {
15
36
  secondInput =
@@ -32,8 +53,8 @@ function rangePlugin(config) {
32
53
  }
33
54
  if (!fp.config.defaultDate && secondInput.value) {
34
55
  var parsedDate = fp.parseDate(secondInput.value, dateFormat);
35
- if (parsedDate && fp.selectedDates.length === 1)
36
- fp.selectedDates.push(parsedDate);
56
+ if (parsedDate && fp.selectedDates.length > 0)
57
+ fp.selectedDates = [fp.selectedDates[0], parsedDate];
37
58
  }
38
59
  secondInput.setAttribute("data-fp-omit", "");
39
60
  if (fp.config.clickOpens) {
@@ -55,7 +76,9 @@ function rangePlugin(config) {
55
76
  if (fp.config.allowInput)
56
77
  fp._bind(secondInput, "keydown", function (e) {
57
78
  if (e.key === "Enter") {
58
- fp.setDate([fp.selectedDates[0], secondInput.value], true, dateFormat);
79
+ var typed = typedValue();
80
+ if (typed !== undefined)
81
+ fp.setDate(typed, true, dateFormat);
59
82
  secondInput.click();
60
83
  }
61
84
  });
@@ -79,6 +102,14 @@ function rangePlugin(config) {
79
102
  else {
80
103
  secondInput.setAttribute("readonly", "readonly");
81
104
  }
105
+ var showValue = fp._showValue;
106
+ fp._showValue = function () {
107
+ showValue();
108
+ var _a = shownValue(), first = _a[0], second = _a[1];
109
+ fp._input.value = first;
110
+ secondInput.value = second;
111
+ };
112
+ fp._typedValue = typedValue;
82
113
  fp._bind(fp._input, "focus", function () {
83
114
  var anchor = firstInputAnchor();
84
115
  if (fp.isOpen && fp._positionElement !== anchor)
@@ -89,11 +120,6 @@ function rangePlugin(config) {
89
120
  _secondInputFocused = false;
90
121
  fp.jumpToDate(fp.selectedDates[0]);
91
122
  });
92
- if (fp.config.allowInput)
93
- fp._bind(fp._input, "keydown", function (e) {
94
- if (e.key === "Enter")
95
- fp.setDate([fp._input.value, fp.selectedDates[1]], true, dateFormat);
96
- });
97
123
  fp.setDate(fp.selectedDates, false);
98
124
  plugin.onValueUpdate(fp.selectedDates);
99
125
  fp.loadedPlugins.push("range");
@@ -106,7 +132,6 @@ function rangePlugin(config) {
106
132
  setTimeout(function () {
107
133
  if (fp.selectedDates.length)
108
134
  return;
109
- secondInput.value = "";
110
135
  _prevDates = [];
111
136
  }, 10);
112
137
  }
@@ -143,11 +168,6 @@ function rangePlugin(config) {
143
168
  else {
144
169
  _prevDates = __spreadArrays(selDates);
145
170
  }
146
- var _a = fp.selectedDates.map(function (d) {
147
- return fp.formatDate(d, dateFormat);
148
- }), _b = _a[0], first = _b === void 0 ? "" : _b, _c = _a[1], second = _c === void 0 ? "" : _c;
149
- fp._input.value = first;
150
- secondInput.value = second;
151
171
  },
152
172
  };
153
173
  return plugin;