@wernfried/daterangepicker 5.6.0 → 5.7.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.
@@ -383,8 +383,6 @@ class DateRangePicker {
383
383
  }
384
384
  if (!this.#startDate && this.initialMonth) {
385
385
  this.#endDate = null;
386
- if (this.timePicker)
387
- console.error(`Option 'initialMonth' works only with 'timePicker: false'`);
388
386
  } else {
389
387
  const violations = this.validateInput(null, false);
390
388
  if (violations != null) {
@@ -871,6 +869,31 @@ class DateRangePicker {
871
869
  this.updateView(monthChange);
872
870
  return violations;
873
871
  }
872
+ /**
873
+ * Clear selected date values.
874
+ * Does not invoke callback function, disables "Apply" button
875
+ * @param {sting|external:DateTime|Date} month - The month to be shown, if null then current month is selected.
876
+ */
877
+ clearDate(month) {
878
+ const initialMonth = this.parseDate(month ?? this.#startDate.startOf("month"));
879
+ this.oldStartDate = this.#startDate;
880
+ this.oldEndDate = this.#endDate;
881
+ this.#startDate = null;
882
+ this.#endDate = null;
883
+ if (this.isInputText)
884
+ this.element.value = "";
885
+ if (!this.singleDatePicker)
886
+ this.chosenLabel = null;
887
+ if (this.altInput) {
888
+ if (this.singleDatePicker) {
889
+ this.altInput.value = "";
890
+ } else {
891
+ this.altInput[0].value = "";
892
+ this.altInput[1].value = "";
893
+ }
894
+ }
895
+ this.updateView(!initialMonth.hasSame(this.oldStartDate, "month"));
896
+ }
874
897
  /**
875
898
  * Parse date value
876
899
  * @param {sting|external:DateTime|Date} value - The value to be parsed
@@ -883,7 +906,7 @@ class DateRangePicker {
883
906
  } else if (value instanceof Date) {
884
907
  return DateTime.fromJSDate(value);
885
908
  } else {
886
- throw RangeError(`Value must be a luxon.DateTime or Date or string`);
909
+ throw RangeError(`Value must be a luxon.DateTime or Date or string. Or use clearDate() to clear date values`);
887
910
  }
888
911
  } else if (typeof value === "string") {
889
912
  const format = typeof this.locale.format === "string" ? this.locale.format : DateTime.parseFormatForOpts(this.locale.format);
@@ -1213,42 +1236,48 @@ class DateRangePicker {
1213
1236
  updateCalendars(monthChange) {
1214
1237
  if (this.timePicker) {
1215
1238
  var hour, minute, second;
1216
- if (this.#endDate) {
1217
- hour = parseInt(this.container.querySelector(".start-time .hourselect").value, 10);
1218
- if (isNaN(hour))
1219
- hour = parseInt(this.container.querySelector(".start-time .hourselect option:last-child").value, 10);
1220
- minute = 0;
1221
- if (this.timePickerOpts.showMinutes) {
1222
- minute = parseInt(this.container.querySelector(".start-time .minuteselect").value, 10);
1223
- if (isNaN(minute))
1224
- minute = parseInt(this.container.querySelector(".start-time .minuteselect option:last-child").value, 10);
1225
- }
1226
- second = 0;
1227
- if (this.timePickerOpts.showSeconds) {
1228
- second = parseInt(this.container.querySelector(".start-time .secondselect").value, 10);
1229
- if (isNaN(second))
1230
- second = parseInt(this.container.querySelector(".start-time .secondselect option:last-child").value, 10);
1231
- }
1239
+ if (this.#startDate == null && this.#endDate == null) {
1240
+ this.leftCalendar.month = this.leftCalendar.month.set({ hour: 0, minute: 0, second: 0 });
1241
+ if (!this.singleMonthView)
1242
+ this.rightCalendar.month = this.rightCalendar.month.set({ hour: 0, minute: 0, second: 0 });
1232
1243
  } else {
1233
- hour = parseInt(this.container.querySelector(".end-time .hourselect").value, 10);
1234
- if (isNaN(hour))
1235
- hour = parseInt(this.container.querySelector(".end-time .hourselect option:last-child").value, 10);
1236
- minute = 0;
1237
- if (this.timePickerOpts.showMinutes) {
1238
- minute = parseInt(this.container.querySelector(".end-time .minuteselect").value, 10);
1239
- if (isNaN(minute))
1240
- minute = parseInt(this.container.querySelector(".end-time .minuteselect option:last-child").value, 10);
1241
- }
1242
- second = 0;
1243
- if (this.timePickerOpts.showSeconds) {
1244
- second = parseInt(this.container.querySelector(".end-time .secondselect").value, 10);
1245
- if (isNaN(second))
1246
- second = parseInt(this.container.querySelector(".end-time .secondselect option:last-child").value, 10);
1244
+ if (this.#endDate) {
1245
+ hour = parseInt(this.container.querySelector(".start-time .hourselect").value, 10);
1246
+ if (isNaN(hour))
1247
+ hour = parseInt(this.container.querySelector(".start-time .hourselect option:last-child").value, 10);
1248
+ minute = 0;
1249
+ if (this.timePickerOpts.showMinutes) {
1250
+ minute = parseInt(this.container.querySelector(".start-time .minuteselect").value, 10);
1251
+ if (isNaN(minute))
1252
+ minute = parseInt(this.container.querySelector(".start-time .minuteselect option:last-child").value, 10);
1253
+ }
1254
+ second = 0;
1255
+ if (this.timePickerOpts.showSeconds) {
1256
+ second = parseInt(this.container.querySelector(".start-time .secondselect").value, 10);
1257
+ if (isNaN(second))
1258
+ second = parseInt(this.container.querySelector(".start-time .secondselect option:last-child").value, 10);
1259
+ }
1260
+ } else {
1261
+ hour = parseInt(this.container.querySelector(".end-time .hourselect").value, 10);
1262
+ if (isNaN(hour))
1263
+ hour = parseInt(this.container.querySelector(".end-time .hourselect option:last-child").value, 10);
1264
+ minute = 0;
1265
+ if (this.timePickerOpts.showMinutes) {
1266
+ minute = parseInt(this.container.querySelector(".end-time .minuteselect").value, 10);
1267
+ if (isNaN(minute))
1268
+ minute = parseInt(this.container.querySelector(".end-time .minuteselect option:last-child").value, 10);
1269
+ }
1270
+ second = 0;
1271
+ if (this.timePickerOpts.showSeconds) {
1272
+ second = parseInt(this.container.querySelector(".end-time .secondselect").value, 10);
1273
+ if (isNaN(second))
1274
+ second = parseInt(this.container.querySelector(".end-time .secondselect option:last-child").value, 10);
1275
+ }
1247
1276
  }
1277
+ this.leftCalendar.month = this.leftCalendar.month.set({ hour, minute, second });
1278
+ if (!this.singleMonthView)
1279
+ this.rightCalendar.month = this.rightCalendar.month.set({ hour, minute, second });
1248
1280
  }
1249
- this.leftCalendar.month = this.leftCalendar.month.set({ hour, minute, second });
1250
- if (!this.singleMonthView)
1251
- this.rightCalendar.month = this.rightCalendar.month.set({ hour, minute, second });
1252
1281
  } else {
1253
1282
  this.leftCalendar.month = this.leftCalendar.month.set({ hour: 0, minute: 0, second: 0 });
1254
1283
  if (!this.singleMonthView)
@@ -1403,20 +1432,27 @@ class DateRangePicker {
1403
1432
  * @emits "beforeRenderTimePicker"
1404
1433
  */
1405
1434
  renderTimePicker(side) {
1406
- if (side === "end" && !this.#endDate) return;
1435
+ if (side === "end" && !this.#endDate && this.#startDate) return;
1407
1436
  var selected, minLimit, minDate, maxDate = this.maxDate;
1408
1437
  let html = "";
1409
1438
  if (this.showWeekNumbers || this.showISOWeekNumbers)
1410
1439
  html += "<th></th>";
1411
- if (this.maxSpan && (!this.maxDate || this.#startDate.plus(this.maxSpan) < this.maxDate))
1412
- maxDate = this.#startDate.plus(this.maxSpan);
1413
- if (this.minSpan && side === "end")
1414
- minLimit = this.#startDate.plus(this.minSpan);
1440
+ if (this.#startDate) {
1441
+ if (this.maxSpan && (!this.maxDate || this.#startDate.plus(this.maxSpan) < this.maxDate))
1442
+ maxDate = this.#startDate.plus(this.maxSpan);
1443
+ if (this.minSpan && side === "end")
1444
+ minLimit = this.#startDate.plus(this.minSpan);
1445
+ } else {
1446
+ if (this.maxSpan && (!this.maxDate || this.initialMonth.plus(this.maxSpan) < this.maxDate))
1447
+ maxDate = this.initialMonth.plus(this.maxSpan);
1448
+ if (this.minSpan && side === "end")
1449
+ minLimit = this.initialMonth.plus(this.minSpan);
1450
+ }
1415
1451
  if (side === "start") {
1416
- selected = this.#startDate;
1452
+ selected = this.#startDate ?? this.initialMonth.startOf("day");
1417
1453
  minDate = this.minDate;
1418
1454
  } else if (side === "end") {
1419
- selected = this.#endDate;
1455
+ selected = this.#endDate ?? this.initialMonth.startOf("day");
1420
1456
  minDate = this.#startDate;
1421
1457
  let timeSelector = this.container.querySelector(".drp-calendar .calendar-time.end-time");
1422
1458
  if (timeSelector.innerHTML != "") {