Skip to content
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

autofocus isn't supported #3696

Closed
alvarotrigo opened this issue Aug 20, 2015 · 9 comments
Closed

autofocus isn't supported #3696

alvarotrigo opened this issue Aug 20, 2015 · 9 comments

Comments

@alvarotrigo
Copy link

The select box when in auto-focus doesn't have any different style than when its not in auto-focus.
This might create confusion in the users.

Reproduction online

Not only that, but it also doesn't seem to work at all in Firefox. By neither using the autofocus attribute or calling the focus function.

Using the arrow keys won't change the selection.

@kevin-brown kevin-brown changed the title Auto-focus doesn't work as expected autofocus isn't supported Aug 20, 2015
@kevin-brown
Copy link
Member

You should notice that the original (now hidden) <select> is being focused instead of Select2.

@arturoribes
Copy link

As of version 4.0.0 I did the following modifications:

In _registerEvents function, for auto focus:

this.on('focus', function () {
    self.$container.addClass('select2-container--focus');

    // ADDED: auto opening on focus
    if (!self.$container.hasClass('select2-container--disabled') && !self.isOpen()) {
        self.open();
    }
});

In this.on('keypress'), to use tab correctly

else if (key === KEYS.TAB) {
    // ADDED: use tab to select and then jump to next field 
    self.trigger('results:select');

}

I don't know how to add an initialization option to enable/disable these behaviours, neither I know how to contribute this to the repository, so I leave it here.

@bangzek
Copy link

bangzek commented Feb 16, 2017

One workaround is:
$("[autofocus] + .select2 .select2-selection").focus();

@pedrofurtado
Copy link
Contributor

The solution provided by @arturoribes works great in select2 4.0.3.
@kevin-brown Is possible to include him solution in the next version of select2?

@pedrofurtado
Copy link
Contributor

@kevin-brown Any news about this issue? The solution provided by @arturoribes (#3696 (comment)) seems to be a simple and functional fix.

@pedrofurtado
Copy link
Contributor

@kevin-brown @alexweissman Any news about this issue?

@alexweissman
Copy link
Contributor

Is there an open pull request? I don't know much about autofocus.

@marciojg
Copy link

@kevin-brown @alexweissman Any news about this issue?

pedrofurtado added a commit that referenced this issue Sep 11, 2018
* Update core.js

* Update core.js

* Update core.js
@pedrofurtado
Copy link
Contributor

@arturoribes @alvarotrigo PR merged, in next release this change will be available for all users 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants