Skip to content

Commit d603358

Browse files
authored
IBX-6662: Handled re-positioning date picker on scroll (#1654)
1 parent 98ad9fc commit d603358

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/bundle/Resources/public/js/scripts/core/date.time.picker.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,41 @@ import { setInstance } from '../helpers/object.instances';
44

55
const { ibexa } = window;
66

7+
const SECTION_ADJUSTMENT = 24;
8+
const PICKER_ADJUSTMENT = 2;
79
const DEFAULT_CONFIG = {
810
enableTime: true,
911
time_24hr: true,
1012
formatDate: (date) => formatShortDateTime(date, null),
13+
onOpen: (selectedDates, dateStr, instance) => {
14+
instance.scrollHandler = () => {
15+
if (instance.isOpen) {
16+
const { calendarContainer, input } = instance;
17+
const rect = input.getBoundingClientRect();
18+
const pickerHeight = calendarContainer.offsetHeight;
19+
const spaceBelow = global.innerHeight - (rect.bottom + SECTION_ADJUSTMENT);
20+
21+
if (pickerHeight > spaceBelow) {
22+
calendarContainer.style.top = `${rect.top + global.scrollY - pickerHeight - PICKER_ADJUSTMENT}px`;
23+
calendarContainer.classList.remove('arrowTop');
24+
calendarContainer.classList.add('arrowBottom');
25+
} else {
26+
calendarContainer.style.top = `${rect.bottom + global.scrollY + PICKER_ADJUSTMENT}px`;
27+
calendarContainer.classList.remove('arrowBottom');
28+
calendarContainer.classList.add('arrowTop');
29+
}
30+
}
31+
};
32+
33+
window.addEventListener('scroll', instance.scrollHandler, true);
34+
document.addEventListener('scroll', instance.scrollHandler, true);
35+
36+
instance.scrollHandler();
37+
},
38+
onClose: (selectedDates, dateStr, instance) => {
39+
window.removeEventListener('scroll', instance.scrollHandler, true);
40+
document.removeEventListener('scroll', instance.scrollHandler, true);
41+
},
1142
};
1243

1344
class DateTimePicker {

0 commit comments

Comments
 (0)