Skip to content

Commit ee9ce50

Browse files
committed
refactor(Calendar, DatePicker, DateRangePicker, TimePicker): prevents changing the time when the day is changed
1 parent c6aee7e commit ee9ce50

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

js/src/calendar.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
isToday,
3030
isYearDisabled,
3131
isYearInRange,
32-
isYearSelected
32+
isYearSelected,
33+
setTimeFromDate
3334
} from './util/calendar.js'
3435

3536
/**
@@ -342,10 +343,10 @@ class Calendar extends BaseComponent {
342343
return
343344
}
344345

345-
this._hoverDate = date
346+
this._hoverDate = setTimeFromDate(date, this._selectEndDate ? this._endDate : this._startDate)
346347

347348
EventHandler.trigger(this._element, EVENT_CELL_HOVER, {
348-
date: getDateBySelectionType(date, this._config.selectionType)
349+
date: getDateBySelectionType(this._hoverDate, this._config.selectionType)
349350
})
350351

351352
this._updateClassNamesAndAriaLabels()
@@ -483,14 +484,14 @@ class Calendar extends BaseComponent {
483484
}
484485

485486
_setEndDate(date) {
486-
this._endDate = date
487+
this._endDate = setTimeFromDate(date, this._endDate)
487488
EventHandler.trigger(this._element, EVENT_END_DATE_CHANGE, {
488489
date: getDateBySelectionType(this._endDate, this._config.selectionType)
489490
})
490491
}
491492

492493
_setStartDate(date) {
493-
this._startDate = date
494+
this._startDate = setTimeFromDate(date, this._startDate)
494495
EventHandler.trigger(this._element, EVENT_START_DATE_CHANGE, {
495496
date: getDateBySelectionType(this._startDate, this._config.selectionType)
496497
})

js/src/time-picker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class TimePicker extends BaseComponent {
256256
'am'
257257
this._timePickerBody.innerHTML = ''
258258
this._createTimePickerSelection()
259+
this._setUpSelects()
259260
}
260261

261262
// Private

js/src/util/calendar.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,26 @@ export const removeTimeFromDate = date => {
637637
clearedDate.setHours(0, 0, 0, 0)
638638
return clearedDate
639639
}
640+
641+
/**
642+
* Copies the time (hours, minutes, seconds, milliseconds) from one Date to another.
643+
*
644+
* @param {Date} target - The date whose time will be updated.
645+
* @param {Date} source - The date to copy the time from.
646+
* @returns {Date} A new Date instance with the date from `target` and time from `source`.
647+
*/
648+
export const setTimeFromDate = (target, source) => {
649+
if (!(source instanceof Date)) {
650+
return target
651+
}
652+
653+
const result = new Date(target) // create a copy to avoid mutation
654+
result.setHours(
655+
source.getHours(),
656+
source.getMinutes(),
657+
source.getSeconds(),
658+
source.getMilliseconds()
659+
)
660+
661+
return result
662+
}

0 commit comments

Comments
 (0)