fix(datepicker): close datepickers on focusout or click outside
This commit is contained in:
parent
3f9ca5e230
commit
7fa0124fe2
@ -180,14 +180,27 @@ export class Datepicker {
|
|||||||
// change the selected date in the tail.datetime instance if the value of the input element is changed
|
// 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 });
|
this._element.addEventListener('change', setDatepickerDate, { once: true });
|
||||||
|
|
||||||
// close the instance on focus loss of the corresponding input element
|
// close the instance on focusout of any element if the target is outside of the instance
|
||||||
// (only close the instance if source and target IDs differ, since interactions with the opened datepicker also triggers this event in some cases)
|
window.addEventListener('focusout', event => {
|
||||||
this._element.addEventListener('focusout', event => {
|
console.log('focusout', event);
|
||||||
if (!event.relatedTarget || event.relatedTarget.id !== event.srcElement.id)
|
const targetIsOutside = !this.datepickerInstance.dt.contains(event.explicitOriginalTarget);
|
||||||
|
const targetIsNotTimepicker = !this.datepickerInstance.dt.contains(event.relatedTarget);
|
||||||
|
console.log(targetIsOutside, targetIsNotTimepicker);
|
||||||
|
if (targetIsOutside && targetIsNotTimepicker)
|
||||||
|
this.datepickerInstance.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
// close the instance on click on any element outside of the datepicker (except the input element itself)
|
||||||
|
window.addEventListener('click', event => {
|
||||||
|
const targetIsOutside = !this.datepickerInstance.dt.contains(event.target);
|
||||||
|
const targetIsInDocument = window.document.contains(event.target);
|
||||||
|
const targetIsNotInput = event.target !== this._element;
|
||||||
|
console.log(targetIsOutside, targetIsInDocument, targetIsNotInput);
|
||||||
|
if (targetIsOutside && targetIsInDocument && targetIsNotInput)
|
||||||
this.datepickerInstance.close();
|
this.datepickerInstance.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
// close the datepicker on escape keydown events
|
// close the instance on escape keydown events
|
||||||
this._element.addEventListener('keydown', event => {
|
this._element.addEventListener('keydown', event => {
|
||||||
if (event.keyCode === KEYCODE_ESCAPE) {
|
if (event.keyCode === KEYCODE_ESCAPE) {
|
||||||
this.datepickerInstance.close();
|
this.datepickerInstance.close();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user