Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date-picker / date-range-picker event fixes #550

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -315,14 +315,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 @@ -354,13 +357,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 @@ -805,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
Loading