enketo / enketo

Enketo web forms monorepo
Apache License 2.0
9 stars 15 forks source link

Datepicker popover automatically opens when date is first question on page #21

Open jkuester opened 6 months ago

jkuester commented 6 months ago

Describe the bug When opening a new page in a form, the first question is automatically focused on. When that question is a date (or dateTime) question, this will cause the bootstrap datepicker popover to open. Depending on the other content in the form, the popover might even cover up the actual label for the question:

image

To Reproduce

  1. Create a form with some date questions that are the first (or only) questions on the page:
type name label::en
date date_1 First Date
date date_2 Second Date
  1. Open the form and navigate to the next page
  2. See the datepicker open automatically

Expected behavior Focusing on the first question makes sense, but opening the datepicker before you even have a chance to read the question is not a great experience. I think it would make more sense to keep the datepicker closed at first, but then be able to open it by selecting the question (or hitting tab if navigating the form via a keyboard).

I am not a bootstrap expert, but it seems like there is no support currently for defining the on-focus behavior. On StackOverflow, I found a suggestion to just automatically hide the popover after the focus event.

Hacking around with this locally, I was able to get my desired behavior by updating the DatePickerExtended._setFocusHandler function to look like this:

    _setFocusHandler($fakeDateI) {
        // Handle focus on original input (goTo functionality)
        $(this.element).on('applyfocus', () => {
            $fakeDateI[0].focus();
            // Do not automatically pop open the date picker when just focusing on outer element
            $fakeDateI.datepicker('hide');
            $fakeDateI.blur();
        });
    }

Not sure if this is the best approach though....

Browser and OS:

Additional context

I can re-create the behavior out on https://getodk.org/xlsform/