fix(datepicker): close datepickers on focus loss

This commit is contained in:
Sarah Vaupel 2019-11-13 17:25:56 +01:00
parent 22163c8fdb
commit 3f9ca5e230

View File

@ -180,11 +180,11 @@ export class Datepicker {
// change the selected date in the tail.datetime instance if the value of the input element is changed
this._element.addEventListener('change', setDatepickerDate, { once: true });
// close the instance if something other than the instance was clicked (i.e. if the target is not within the datepicker instance and if any previously clicked calendar view was replaced (is not in the window anymore) because it was clicked). YES, I KNOW
window.addEventListener('click', event => {
if (!this.datepickerInstance.dt.contains(event.target) && window.document.contains(event.target)) {
// close the instance on focus loss of the corresponding input element
// (only close the instance if source and target IDs differ, since interactions with the opened datepicker also triggers this event in some cases)
this._element.addEventListener('focusout', event => {
if (!event.relatedTarget || event.relatedTarget.id !== event.srcElement.id)
this.datepickerInstance.close();
}
});
// close the datepicker on escape keydown events