Materialize: Date Picker in Modal displays incorrectly

Created on 29 Mar 2015  路  30Comments  路  Source: Dogfalo/materialize

I have a modal that appears and presents the user with a form. In this form are two Date Pickers. The problem is when I click one of the Date Picker form fields to select a date, the UI that appears for date selection is stuck within the constraints of the modal. Therefore, it gets cut off on the top and/or bottom depending on where in the modal I have scrolled. Moreover, the Date Picker UI is also cut off at the base line for the Date Picker form field.

Seeing as I have not done anything special in my modal or to the Date Picker, I believe this is an issue with how the Date Picker UI is being rendered within the modal.

Perhaps it should be reparented?

http://codepen.io/anon/pen/RPwJvY

Most helpful comment

Why just not use this? http://amsul.ca/pickadate.js/date/#container

juiced here:

$('.datepicker').pickadate({
  container: 'body'
})

This works for me.

All 30 comments

Please post your code.

        <!-- Modal Structure -->
        <div id="create-event" class="modal">
            <div class="modal-content">
                <div class="row">
                    <form class="col s12">
                        <div class="row">
                            <h4>Create a new event</h4>
                        </div>
                        <div class="row">
                            <div class="input-field col s6">
                                <input id="{{ event_form.title.id_for_label }}" type="text" class="validate">
                                <label for="{{ event_form.title.id_for_label }}">{{ event_form.title.label }}</label>
                            </div>
                            <div class="input-field col s6">
                                <input id="{{ event_form.host.id_for_label }}" type="text" class="validate">
                                <label for="{{ event_form.host.id_for_label }}">{{ event_form.host.label }}</label>
                            </div>
                        </div>
                        <div class="row">
                            <div class="input-field col s6">
                                <input id="{{ event_form.menu_description.id_for_label }}" type="text" class="validate">
                                <label for="{{ event_form.menu_description.id_for_label }}">{{ event_form.menu_description.label }}</label>
                            </div>
                            <div class="input-field col s6">
                                <input id="{{ event_form.location_description.id_for_label }}" type="text" class="validate">
                                <label for="{{ event_form.location_description.id_for_label }}">{{ event_form.location_description.label }}</label>
                            </div>
                        </div>
                        <div class="row">
                            <div class="input-field col s12">
                                <textarea id="{{ event_form.event_description.id_for_label }}" class="materialize-textarea validate"></textarea>
                                <label for="{{ event_form.event_description.id_for_label }}">{{ event_form.event_description.label }}</label>
                            </div>
                        </div>
                        <div class="row">
                            <div class="input-field col s6">
                                <input id="{{ event_form.start_date.id_for_label }}" type="date" class="datepicker validate">
                                <label for="{{ event_form.start_date.id_for_label }}">{{ event_form.start_date.label }}</label>
                            </div>
                            <div class="input-field col s6">
                                <input id="{{ event_form.end_date.id_for_label }}" type="date" class="datepicker validate">
                                <label for="{{ event_form.end_date.id_for_label }}">{{ event_form.end_date.label }}</label>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="modal-footer">
                <div class="row center-align">
                    <a id="event-submit" class="btn waves-effect waves-light btn-large indigo lighten-3 col s11">Create Event</a>
                </div>
            </div>
        </div>

This is because of how modals work, a fixed element within a fixed element will make it fixed within the other. Expanding the modal is the best solution.

The other is to take position absolute off .picker and position: absolute off .picker__holder and make .modal overflow: visible;

I tried both solutions. The latter did not work because the Date Picker would no longer appear when clicking the form field, likewise setting overflow: visible had undesirable effects on the modal.

Using the first solution, I was able to expand the modal and have the majority of the Date Picker appear. While not perfect, I may be able to work with this for large screens.

However, for small screens (in particular phones), the Date Picker is nearly unusable due to how it renders in the modal. Is there a way to get the Date Picker to work nicely on small screens? Perhaps, is there a way to scale down its size?

The datepicker modal should probably be attached to the document.body and not to the parent element of the field. That way it will have the entire display surface to show and not only the modal surface

I've run into this issue in my project, I have a modal with a form in it, and there's a datepicker field, when calendar opens up, it opens up inside of the modal. Looked at DOM and calendar gets generated inside of modal.

is there a fix out there for this?

The fix right now would be to have some js run to reparent the form to the body, don't know if this breaks the event handlers on the element.

@Dogfalo ok, i ran onRender() event to reattach it on body level

Hey Albert, can you explain better how did you solve the problem? When I tried to reparente the datepicker to the body using developer console it lost the event handlers when clicking on a date.

I have a big SaaS and I really need to use this datepicker inside modal. I already use datetimepicker bootstrap.

Can you help how did you solve?

[Edit]
The same thing happens with