Skip to content

Commit

Permalink
Merge pull request #550 from vladrusu/main-patch5
Browse files Browse the repository at this point in the history
date-picker / date-range-picker event fixes
  • Loading branch information
mrholek authored Nov 22, 2024
2 parents 63d5c29 + 8c7ccbc commit b33e9c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
15 changes: 4 additions & 11 deletions js/src/date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`

const CLASS_NAME_SHOW = 'show'

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

Expand Down Expand Up @@ -65,20 +64,14 @@ class DatePicker extends DateRangePicker {
}

// Overrides
_addCalendarEventListeners() {
super._addCalendarEventListeners()
for (const calendar of SelectorEngine.find(SELECTOR_CALENDAR, this._element)) {
EventHandler.on(calendar, 'startDateChange.coreui.calendar', event => {
this._startDate = event.date
this._startInput.value = this._setInputValue(event.date)
this._selectEndDate = false
this._calendar.update(this._getCalendarConfig())
_addEventListeners() {
super._addEventListeners()

EventHandler.on(this._element, 'startDateChange.coreui.date-range-picker', event => {
EventHandler.trigger(this._element, EVENT_DATE_CHANGE, {
date: event.date
})
})
}
})
}

// Static
Expand Down
35 changes: 27 additions & 8 deletions js/src/date-range-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,17 @@ 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())

EventHandler.trigger(this._element, EVENT_START_DATE_CHANGE, {
date: date
})
}
})

Expand Down Expand Up @@ -381,13 +384,17 @@ 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: date
})
}
})

Expand Down Expand Up @@ -837,7 +844,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 b33e9c3

Please sign in to comment.