tui.calendar icon indicating copy to clipboard operation
tui.calendar copied to clipboard

getBoundingClientRect() always fails the first time with 'undefined' error

Open Avinodh opened this issue 2 years ago • 3 comments

Version

2.1.3

Test Environment

Google Chrome

Current Behavior

I am trying to configure custom popups for event creation. The following code sets up the event handler to listen on the selectDateTime event:

        this.calendarInstance.on('selectDateTime', (info) => {
            console.log(info.start + " - " + info.end);
            var gridElems = info.gridSelectionElements;
            console.log(gridElems[0].getBoundingClientRect());
        });

The first time I click the calendar, I get the following error: image

However, any subsequent clicks seem to work. just fine (the below screenshot shows that the second click did in fact print the boundingRect DOM element): image

My guess is that there is some sort of a race here between when the first event gets fired off and the DOM being ready. But why does this consistently happen on only the first click?

Avinodh avatar Jan 21 '23 22:01 Avinodh

@Avinodh

You are correct, it is a bug caused by a race condition that occurs when selecting datetime by clicking on cells.

Unlike dragging, the calendar needs to determine whether it is a single or double click. Once the calendar recognizes the click interaction, it draws the selection element and re-renders. (code)

However, the gridSelectionElements refer to variables declared during the previous render phase, which is why the proper gridSelectionElements property cannot be obtained.

This may seem peculiar, but it is how React (the calendar uses Preact internally) works.

Resolving this issue would be pretty difficult. As a workaround, you can disable clicks through an option and only allow users to drag cells.

adhrinae avatar Jan 25 '23 00:01 adhrinae

Thanks @adhrinae ! I'll look into disabling the click event, and just going with the drag behavior.

Avinodh avatar Jan 29 '23 17:01 Avinodh

@Avinodh I'll leave this issue open since it's not resolved yet.

adhrinae avatar Jan 29 '23 23:01 adhrinae