Skip to content

Commit 8c7ccbc

Browse files
committed
- Send EVENT_START_DATE_CHANGE / EVENT_END_DATE_CHANGE also when date is empty / deleted
- Better safeguard for inputDateParse / inputDateFormat when date is empty (prevent Invalid Date)
1 parent 6497dbd commit 8c7ccbc

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

js/src/date-range-picker.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,10 @@ class DateRangePicker extends BaseComponent {
315315
})
316316

317317
EventHandler.on(this._startInput, EVENT_INPUT, event => {
318-
const date = this._config.inputDateParse ?
319-
this._config.inputDateParse(event.target.value) :
320-
getLocalDateFromString(event.target.value, this._config.locale, this._config.timepicker)
318+
const date = this._parseDate(event.target.value)
321319

322-
if (date instanceof Date && date.getTime()) {
320+
// valid date or empty date
321+
if ((date instanceof Date && !isNaN(date)) || (date === null)) {
323322
this._startDate = date
324323
this._calendarDate = date
325324
this._calendar.update(this._getCalendarConfig())
@@ -358,16 +357,16 @@ class DateRangePicker extends BaseComponent {
358357
})
359358

360359
EventHandler.on(this._endInput, EVENT_INPUT, event => {
361-
const date = this._config.inputDateParse ?
362-
this._config.inputDateParse(event.target.value) :
363-
getLocalDateFromString(event.target.value, this._config.locale, this._config.timepicker)
364-
if (date instanceof Date && date.getTime()) {
360+
const date = this._parseDate(event.target.value)
361+
362+
// valid date or empty date
363+
if ((date instanceof Date && !isNaN(date)) || (date === null)) {
365364
this._endDate = date
366365
this._calendarDate = date
367366
this._calendar.update(this._getCalendarConfig())
368367

369368
EventHandler.trigger(this._element, EVENT_END_DATE_CHANGE, {
370-
date: value
369+
date: date
371370
})
372371
}
373372
})
@@ -813,7 +812,19 @@ class DateRangePicker extends BaseComponent {
813812
this._popper = Popper.createPopper(this._togglerElement, this._menu, popperConfig)
814813
}
815814

815+
_parseDate(str) {
816+
if (!str)
817+
return null;
818+
819+
return this._config.inputDateParse ?
820+
this._config.inputDateParse(str) :
821+
getLocalDateFromString(str, this._config.locale, this._config.timepicker)
822+
}
823+
816824
_formatDate(date) {
825+
if (!date)
826+
return '';
827+
817828
if (this._config.inputDateFormat) {
818829
return this._config.inputDateFormat(
819830
date instanceof Date ? new Date(date) : convertToDateObject(date, this._config.selectionType)

0 commit comments

Comments
 (0)