-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Datepicker years go off the top of the page when scrollposition is not at the top #6388
Comments
Any idea how can this fixed? |
I'm unable to replicate this. I'm not sure how it's possible to perform the 'current behaviour' step in the test: "if i scroll down the page and do the same thing as in the expected behavior the year options go up off the screen." Pretty vague instructions. How does one click the date picker if it's not visible on the page? |
@pwcreative It is still visible on the page as you can see in the example image where it is broken. Another way ive managed to replicate it is by trying the same thing using responsive design mode in the browser. It seems to only open that way when scrolled to a certain position. |
1+ |
@pwcreative here's a jsfiddle to replicate. It seems to depend upon the y-coordinate of the input field to which the date picker is attached i.e. if the input field is near the top of the visible viewport. |
I've had some success by forcing the date input to scroll into the centre of the screen when it receives focus: $(document).ready(function() {
// initialize the datepicker
$('.datepicker').datepicker()
// when it gets focus, scroll it to the centre
.on("focus", function() {
var center = $(window).height() / 2;
var top = $(this).offset().top;
if (top > center) {
$('html, body').animate({
scrollTop: top - center
}, 'fast');
}
// now open the date picker
$(this).datepicker("open");
});
}); Here's a jsfiddle with the above : https://jsfiddle.net/jnk21do6/18/ |
I was just playing around with a pen but not quite there yet. I did think about the scroll thing but not quite working for me. Here's my thoughts on a workaround:
The last line here
EDIT: This css seems to be working in my example:
https://codepen.io/doughballs/pen/mdeKybx Note, when you open the dropdown, the page scrolls behind it. Some funky stuff going on. I just tried those to lines of CSS on the docs page and it seems also to fix it, but note that it would apply to all dropdown content, so you may need to be more specific with the selector. |
@doughballs thanks for sharing this I'll give it a try! Is the |
Ah I see now: I'm guessing that the |
Sorry, the setTimeout is to give the modal time to open and position itself - without it, we get a value of 0. What we really want is onOpenEnd, but inexplicably it's not available in the datepicker options (but is apparently available to timepicker?!) |
Yes, possibly, I think an These are all majorily hacky, as I don't fully understand the component! |
Edit: This doesn't work properly, the classNames get reverted by some other event. (previous comment:)
|
OK this solution is working perfectly for me, thanks for the pointers @doughballs . (edit: almost perfectly... if you open a dropdown a second time, it regresses... :sigh: ) jsfiddle: https://jsfiddle.net/t9uzf3xn/ // a hack to work around https://github.com/Dogfalo/materialize/issues/6388
const datePickerSelectAddClass = function() {
const self = this;
setTimeout(function() {
let selector = self.el;
if (!selector) {
selector = ".datepicker"
}
$(selector).siblings(".datepicker-modal") // modals are generated alongside each date picker
.find(".select-dropdown.dropdown-trigger") // the .dropdown-trigger element is the trigger for the <selects> to appear
.each((index, item) => {
const dateDropdownID = $(item).attr("data-target"); // now we've got the ID of one of the generated <ul> elements
const dropdownULJQ = $(`#${dateDropdownID}`);
// add an 'onclick' event handler
dropdownULJQ.children("li").on("click",()=>{
datePickerSelectAddClass(); // yeah for some reason our UL elements get destroyed and recreated so we have to do this again.
});
dropdownULJQ.addClass("dropdown-content-datepicker") // add our own className to it so we can target it.
});
}, 500);
};
$('.datepicker').datepicker({
onOpen: datePickerSelectAddClass
}); .dropdown-content-datepicker {
position: fixed;
height:300px !important;
top: 10% !important;
} |
Good work! |
how to implement the same without any modal pop-up view? |
Expected Behavior
The issue i am bringing up can be replicated from the documentation
When i open the datepicker with the scroll position at the top of the page and select years it displays as expected showing the years going down the way.
![datepicker_how_it_should_always_be](https://user-images.githubusercontent.com/4376874/59349545-ccb6c000-8d11-11e9-94a6-0fe9940cb7d7.png)
Current Behavior
if i scroll down the page and do the same thing as in the expected behavior the year options go up off the screen.
![datepicker_example](https://user-images.githubusercontent.com/4376874/59349589-ea842500-8d11-11e9-9b3d-9ec5057a758b.png)
Possible Solution
Add an option so the select options always go down from the input?
Steps to Reproduce (for bugs)
Context
Your Environment
The text was updated successfully, but these errors were encountered: