t1m0n/air-datepicker

Impossible to use after $datepickersContainer is removed from the DOM

mholt opened this issue · 5 comments

mholt commented

I have an SPA where the <body> is replaced on page transitions, and a new date picker needs to be created. Unfortunately this makes it impossible to use AirDatepicker after a page with an AirDatepicker is replaced with a new page that also has an AirDatepicker, because the lib only allows creating a container element once:

function buildDatepickersContainer(id) {
containerBuilt = true;
$datepickersContainer = createElement({className: id, id});
$body.appendChild($datepickersContainer);
return $datepickersContainer;
}

This results in an error when clicking the input to open the date picker, because this.$datepicker.offsetParent from this line:

$dpOffset = this.$datepicker.offsetParent,

is null, so it goes boom 💥 on this line:

let dpOffsetDims = $dpOffset.getBoundingClientRect();

I think a more robust approach would be to allow creating the container again if it does not still exist in the DOM.

Edit: I should clarify, this is only an issue when container is not specified in the options when constructing a new AirDatepicker. The problem is that I can't pass in a container because at the time of constructing it I do not know where it will go yet.

t1m0n commented

@mholt hi, interesting case) I will figure something out.

t1m0n commented

Added extra check if container still exists on init, should do the trick

mholt commented

@t1m0n Oh awesome!! I will try it as soon as I can, thank you!

PS. Air Datepicker has been amazing to work with otherwise. It has just about everything I was looking for and will be very suitable for my rather advanced use case :) (I'm building a timeline project, so lots of dates and times.) Thank you for building/maintaining it!

mholt commented

Finally had a chance to test this. It seems to work for me! Thank you very very much! 💯

t1m0n commented

@mholt Oh great! Glad to hear that it worked for you.
Also thanks for a nice feedback, I've must have missed the message :)