Skip to content

Conversation

joepetrakovich
Copy link
Contributor

Changed tab key press to behave the same as enter key and select the highlighted option. This is consistent with modern browser behavior for <select> elements.

#4820

…highlighted option to be consistent with regular html selects in modern browsers.
@joepetrakovich
Copy link
Contributor Author

I just realized this may be a breaking change for a lot of people. If they've gone this long using select2 with the assumption that TAB should not select, changing it may mess their surrounding navigation code up.

One scenario I found is when the user tabs to the dropdown, I have the dropdown automatically open up so they can start typing, but when you call open, the first option is already highlighted, so if you hit TAB again (with the desire to skip the field and leave it blank), you can't do it because it will now select that first highlighted option. You'd be forced to use the mouse to then clear it.

This reminds me of the other issues that mention the desire for a blank first option.

@kevin-brown kevin-brown added this to the 4.1.0 milestone Feb 4, 2020
@kevin-brown
Copy link
Member

If they've gone this long using select2 with the assumption that TAB should not select, changing it may mess their surrounding navigation code up.

We have a minor release coming up soon which will feature some of these small-but-important somewhat-breaking changes so I'm comfortable knowing that.

I have the dropdown automatically open up so they can start typing

Custom UX behaviours aren't our concern.

You'd be forced to use the mouse to then clear it.

Good news! This is one of the places where Select2 is accessible through a keyboard.

if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
this._handleClear(evt);
}

you can't do it because it will now select that first highlighted option

A fun feature request which could help here is to provide the keyboard event data to the select event, so you could detect that case and prevent the selection.

@stale
Copy link

stale bot commented Apr 5, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Apr 5, 2020
@stale stale bot closed this Apr 12, 2020
@benkoshy
Copy link

Changed tab key press to behave the same as enter key and select the highlighted option. This is consistent with modern browser behavior for <select> elements.

#4820

@joepetrakovich thx i appreciate the PR. question: is there a way of adding the minor code changes in your PR so that it overrides the existing functionality in src/js/select2/core.js? any pointers would be much appreciated.

@joepetrakovich
Copy link
Contributor Author

@BKSpurgeon if you use the non-minified version of the library you can just do a text search for some of the lines I changed (like key === KEYS.ESC) and make the same change yourself. Is that what you are asking?

@benkoshy
Copy link

Hi @joepetrakovich appreciate the response. Was wanting to know if there was a way to quickly override the original core functionality provided by select2: (i'm getting it from a yarn / npm package so presumably that package is not minified): if that is true, then is there a way to retain the original package and specifically override those 1 or 2 lines of code?

@kevin-brown kevin-brown reopened this Apr 25, 2020
@stale stale bot removed the status: stale label Apr 25, 2020
@devkinetic
Copy link

@BKSpurgeon In the intermediate, I've been using using the events system:

mySelect2Widget.on('select2:select', function (e) {
  $(this).focus();
});

@benkoshy
Copy link

@BKSpurgeon In the intermediate, I've been using using the events system:

mySelect2Widget.on('select2:select', function (e) {
  $(this).focus();
});

@devkinetic thx - do you have the context / larger code snippet of where to place the code? Not sure if i'm doing the right thing, but i couldn't get the above to work.

@stale
Copy link

stale bot commented Aug 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Aug 15, 2020
@stale stale bot closed this Aug 22, 2020
@devkinetic
Copy link

devkinetic commented Oct 30, 2020

@BKSpurgeon Sorry, i don't check github as often as I should. To follow up from my example mySelect2Widget is selector for an element with select2() applied to it.

var mySelect2Widget  = $('.css-class-to-target');
mySelect2Widget.on('select2:select', function (e) {
        $(this).focus();
      });

@kevin-brown
Copy link
Member

Reopening this for another stale cycle before the next release. Need to double check on ARIA for this before reviewing it.

@kevin-brown kevin-brown reopened this Jan 8, 2021
@stale stale bot removed the status: stale label Jan 8, 2021
@kevin-brown kevin-brown mentioned this pull request Jan 22, 2021
2 tasks
@kevin-brown
Copy link
Member

ARIA has zero guidance on this front so I'm going to default back to selectWoo and how the browser implements it. So this PR looks good and will land for the 4.1.0 release candidate.

@AndrijaX
Copy link

I noticed that if you navigate through web form with TAB keys (ARIA behaviour) and you are landing on select2 (multiple items) you get stacked in select2. TAB doesn't allow you to get to the next form field. This shouldn't happen. I'm missing something?

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

Successfully merging this pull request may close these issues.

5 participants