Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fef3287

Browse files
committedNov 22, 2024
Merge branch 'main' of https://github.com/coreui/coreui-pro
2 parents 056f41a + b33e9c3 commit fef3287

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed
 

‎js/src/date-picker.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
2929

3030
const CLASS_NAME_SHOW = 'show'
3131

32-
const SELECTOR_CALENDAR = '.calendar'
3332
const SELECTOR_DATA_TOGGLE = '[data-coreui-toggle="date-picker"]:not(.disabled):not(:disabled)'
3433
const SELECTOR_DATA_TOGGLE_SHOWN = `${SELECTOR_DATA_TOGGLE}.${CLASS_NAME_SHOW}`
3534

@@ -65,20 +64,14 @@ class DatePicker extends DateRangePicker {
6564
}
6665

6766
// Overrides
68-
_addCalendarEventListeners() {
69-
super._addCalendarEventListeners()
70-
for (const calendar of SelectorEngine.find(SELECTOR_CALENDAR, this._element)) {
71-
EventHandler.on(calendar, 'startDateChange.coreui.calendar', event => {
72-
this._startDate = event.date
73-
this._startInput.value = this._setInputValue(event.date)
74-
this._selectEndDate = false
75-
this._calendar.update(this._getCalendarConfig())
67+
_addEventListeners() {
68+
super._addEventListeners()
7669

70+
EventHandler.on(this._element, 'startDateChange.coreui.date-range-picker', event => {
7771
EventHandler.trigger(this._element, EVENT_DATE_CHANGE, {
7872
date: event.date
7973
})
80-
})
81-
}
74+
})
8275
}
8376

8477
// Static

‎js/src/date-range-picker.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,17 @@ class DateRangePicker extends BaseComponent {
342342
})
343343

344344
EventHandler.on(this._startInput, EVENT_INPUT, event => {
345-
const date = this._config.inputDateParse ?
346-
this._config.inputDateParse(event.target.value) :
347-
getLocalDateFromString(event.target.value, this._config.locale, this._config.timepicker)
345+
const date = this._parseDate(event.target.value)
348346

349-
if (date instanceof Date && date.getTime()) {
347+
// valid date or empty date
348+
if ((date instanceof Date && !isNaN(date)) || (date === null)) {
350349
this._startDate = date
351350
this._calendarDate = date
352351
this._calendar.update(this._getCalendarConfig())
352+
353+
EventHandler.trigger(this._element, EVENT_START_DATE_CHANGE, {
354+
date: date
355+
})
353356
}
354357
})
355358

@@ -381,13 +384,17 @@ class DateRangePicker extends BaseComponent {
381384
})
382385

383386
EventHandler.on(this._endInput, EVENT_INPUT, event => {
384-
const date = this._config.inputDateParse ?
385-
this._config.inputDateParse(event.target.value) :
386-
getLocalDateFromString(event.target.value, this._config.locale, this._config.timepicker)
387-
if (date instanceof Date && date.getTime()) {
387+
const date = this._parseDate(event.target.value)
388+
389+
// valid date or empty date
390+
if ((date instanceof Date && !isNaN(date)) || (date === null)) {
388391
this._endDate = date
389392
this._calendarDate = date
390393
this._calendar.update(this._getCalendarConfig())
394+
395+
EventHandler.trigger(this._element, EVENT_END_DATE_CHANGE, {
396+
date: date
397+
})
391398
}
392399
})
393400

@@ -837,7 +844,19 @@ class DateRangePicker extends BaseComponent {
837844
this._popper = Popper.createPopper(this._togglerElement, this._menu, popperConfig)
838845
}
839846

847+
_parseDate(str) {
848+
if (!str)
849+
return null;
850+
851+
return this._config.inputDateParse ?
852+
this._config.inputDateParse(str) :
853+
getLocalDateFromString(str, this._config.locale, this._config.timepicker)
854+
}
855+
840856
_formatDate(date) {
857+
if (!date)
858+
return '';
859+
841860
if (this._config.inputDateFormat) {
842861
return this._config.inputDateFormat(
843862
date instanceof Date ? new Date(date) : convertToDateObject(date, this._config.selectionType)

0 commit comments

Comments
 (0)
Please sign in to comment.