Skip to content

Commit 348d562

Browse files
committed
Update _checkOffset to handle invisible input
The _findPos function already searches for the nearest visible element, and returns its position. This is good. The _checkOffset function, however, just deals with the current element, which causes issues where your invisible element is placed oddly by the browser. The problem is only evident when the datepicker's input element is invisible (e.g. if you're showing an altField instead, and have a button to show the picker) and a descendant of a fixed element, and the screen is scrolled. The conditions never matched, so the documents scroll amount was never subtracted from the datepicker's position. This fix uses code adapted from the _findPos function to find the nearest visible element and uses that to test the offset. Note that inputHeight and inputWidth are reset to 0, as that's the effective width of an invisible element (although using .outerHeight() actually returns a positive number).
1 parent 956c2cd commit 348d562

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ui/jquery.ui.datepicker.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,17 @@ $.extend(Datepicker.prototype, {
765765
var inputHeight = inst.input ? inst.input.outerHeight() : 0;
766766
var viewWidth = document.documentElement.clientWidth + $(document).scrollLeft();
767767
var viewHeight = document.documentElement.clientHeight + $(document).scrollTop();
768+
var isRTL = this._get(inst, "isRTL");
769+
var offsetTarget = inst.input;
768770

769-
offset.left -= (this._get(inst, 'isRTL') ? (dpWidth - inputWidth) : 0);
770-
offset.left -= (isFixed && offset.left == inst.input.offset().left) ? $(document).scrollLeft() : 0;
771-
offset.top -= (isFixed && offset.top == (inst.input.offset().top + inputHeight)) ? $(document).scrollTop() : 0;
771+
while (offsetTarget && !offsetTarget.is(':visible')) {
772+
offsetTarget = isRTL ? offsetTarget.prev() : offsetTarget.next();
773+
inputHeight = inputWidth = 0
774+
}
775+
776+
offset.left -= (isRTL ? (dpWidth - inputWidth) : 0);
777+
offset.left -= (isFixed && offset.left == offsetTarget.offset().left) ? $(document).scrollLeft() : 0;
778+
offset.top -= (isFixed && offset.top == (offsetTarget.offset().top + inputHeight)) ? $(document).scrollTop() : 0;
772779

773780
// now check if datepicker is showing outside window viewport - move to a better place if so.
774781
offset.left -= Math.min(offset.left, (offset.left + dpWidth > viewWidth && viewWidth > dpWidth) ?

0 commit comments

Comments
 (0)