Skip to content

Commit

Permalink
- Send EVENT_START_DATE_CHANGE / EVENT_END_DATE_CHANGE also when date…
Browse files Browse the repository at this point in the history
… is empty / deleted

- Better safeguard for inputDateParse / inputDateFormat when date is empty (prevent Invalid Date)
  • Loading branch information
vladrusu committed Oct 18, 2024
1 parent 6497dbd commit 8c7ccbc
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions js/src/date-range-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,10 @@ class DateRangePicker extends BaseComponent {
})

EventHandler.on(this._startInput, EVENT_INPUT, event => {
const date = this._config.inputDateParse ?
this._config.inputDateParse(event.target.value) :
getLocalDateFromString(event.target.value, this._config.locale, this._config.timepicker)
const date = this._parseDate(event.target.value)

if (date instanceof Date && date.getTime()) {
// valid date or empty date
if ((date instanceof Date && !isNaN(date)) || (date === null)) {
this._startDate = date
this._calendarDate = date
this._calendar.update(this._getCalendarConfig())
Expand Down Expand Up @@ -358,16 +357,16 @@ class DateRangePicker extends BaseComponent {
})

EventHandler.on(this._endInput, EVENT_INPUT, event => {
const date = this._config.inputDateParse ?
this._config.inputDateParse(event.target.value) :
getLocalDateFromString(event.target.value, this._config.locale, this._config.timepicker)
if (date instanceof Date && date.getTime()) {
const date = this._parseDate(event.target.value)

// valid date or empty date
if ((date instanceof Date && !isNaN(date)) || (date === null)) {
this._endDate = date
this._calendarDate = date
this._calendar.update(this._getCalendarConfig())

EventHandler.trigger(this._element, EVENT_END_DATE_CHANGE, {
date: value
date: date
})
}
})
Expand Down Expand Up @@ -813,7 +812,19 @@ class DateRangePicker extends BaseComponent {
this._popper = Popper.createPopper(this._togglerElement, this._menu, popperConfig)
}

_parseDate(str) {
if (!str)
return null;

return this._config.inputDateParse ?
this._config.inputDateParse(str) :
getLocalDateFromString(str, this._config.locale, this._config.timepicker)
}

_formatDate(date) {
if (!date)
return '';

if (this._config.inputDateFormat) {
return this._config.inputDateFormat(
date instanceof Date ? new Date(date) : convertToDateObject(date, this._config.selectionType)
Expand Down

0 comments on commit 8c7ccbc

Please sign in to comment.