@wernfried/daterangepicker 5.5.1 → 5.6.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.
@@ -453,10 +453,10 @@ class DateRangePicker {
453
453
  this.buttonClasses = options.buttonClasses;
454
454
  }
455
455
  if (typeof options.onOutsideClick === "string") {
456
- if (["cancel", "apply"].includes(options.onOutsideClick))
456
+ if (["cancel", "apply", "hide"].includes(options.onOutsideClick))
457
457
  this.onOutsideClick = options.onOutsideClick;
458
458
  else
459
- console.error(`Option 'onOutsideClick' must be 'cancel' or 'apply'`);
459
+ console.error(`Option 'onOutsideClick' must be 'cancel', 'apply' or 'hide'`);
460
460
  }
461
461
  if (this.locale.firstDay != 1) {
462
462
  let iterator = this.locale.firstDay;
@@ -1697,14 +1697,10 @@ class DateRangePicker {
1697
1697
  */
1698
1698
  hide() {
1699
1699
  if (!this.isShowing) return;
1700
- if (!this.#endDate) {
1700
+ if (!this.#endDate && !this.singleDatePicker) {
1701
1701
  this.#startDate = this.oldStartDate;
1702
1702
  this.#endDate = this.oldEndDate;
1703
1703
  }
1704
- if (typeof this.callback === "function") {
1705
- if (this.#startDate && !this.#startDate.equals(this.oldStartDate ?? DateTime) || this.#endDate && !this.singleDatePicker && !this.#endDate.equals(this.oldEndDate ?? DateTime))
1706
- this.callback(this.startDate, this.endDate, this.chosenLabel);
1707
- }
1708
1704
  this.updateElement();
1709
1705
  const event = this.triggerEvent(this.#events.onBeforeHide);
1710
1706
  if (event.defaultPrevented)
@@ -1776,8 +1772,12 @@ class DateRangePicker {
1776
1772
  if (this.onOutsideClick === "cancel") {
1777
1773
  this.#startDate = this.oldStartDate;
1778
1774
  this.#endDate = this.oldEndDate;
1775
+ this.hide();
1776
+ } else if (this.onOutsideClick === "apply") {
1777
+ this.clickApply();
1778
+ } else {
1779
+ this.hide();
1779
1780
  }
1780
- this.hide();
1781
1781
  }
1782
1782
  /**
1783
1783
  * Move calendar to previous month
@@ -2140,6 +2140,11 @@ class DateRangePicker {
2140
2140
  * @private
2141
2141
  */
2142
2142
  clickApply() {
2143
+ this.updateElement();
2144
+ if (typeof this.callback === "function") {
2145
+ if (this.#startDate && (this.#endDate || !this.singleDatePicker))
2146
+ this.callback(this.startDate, this.endDate, this.chosenLabel);
2147
+ }
2143
2148
  this.hide();
2144
2149
  this.triggerEvent(this.#events.onApply);
2145
2150
  }
@@ -2334,24 +2339,37 @@ class DateRangePicker {
2334
2339
  });
2335
2340
  }
2336
2341
  /**
2337
- * Adds an event listener to the anchor element (e.g., `<input>`) the DateRangePicker is attached to.
2338
- * This allows external code to subscribe to native DOM events such as 'click', 'focus', 'keyup', etc.
2342
+ * Adding event listener to input element where dateragnepicker is attached to.
2343
+ * Wrapper to give easy access for `addEventListener()` on DateRangePicker instance.
2339
2344
  *
2340
- * @param {string} type - The event name (e.g., 'click', 'focus', 'keyup')
2345
+ * @param {string} type - The event name, either native DOM-Event or [DateRangePicker](API_Doc.md#event) specific event
2341
2346
  * @param {function} listener - The callback function invoked when the event fires
2342
2347
  * @param {boolean|object} [options] - Optional event options:
2343
2348
  * - `boolean capture`: Whether to listen in the capture phase (default: `false`)
2344
2349
  * - `object signal`: An `AbortSignal` for later removal of the listener (default: `undefined`)
2345
2350
  *
2346
2351
  * @example
2347
- * daterangepicker.addEventListener('click', (e) => {
2348
- * console.log('Picker element was clicked', e);
2352
+ * picker.addEventListener('hide', (e) => {
2353
+ * console.log('Picker element was hidden', e);
2349
2354
  * });
2350
2355
  *
2351
2356
  */
2352
2357
  addEventListener(type, listener, options) {
2353
2358
  this.element.addEventListener(type, listener, options);
2354
2359
  }
2360
+ /**
2361
+ * Counterpart to `addEventListener()`
2362
+ * Note, `listener` most be a named function to enable remove.
2363
+ *
2364
+ * @param {string} type - The event name, either native
2365
+ * @param {function} listener - The callback function invoked when the event fires
2366
+ * @param {boolean|object} [options] - Optional event options:
2367
+ * - `boolean capture`: Whether to listen in the capture phase (default: `false`)
2368
+ * - `object signal`: An `AbortSignal` for later removal of the listener (default: `undefined`)
2369
+ */
2370
+ removeEventListener(type, listener, options) {
2371
+ this.element.removeEventListener(type, listener, options);
2372
+ }
2355
2373
  }
2356
2374
  function createElementFromHTML(html) {
2357
2375
  const template = document.createElement("template");