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

Enter key still works on disabled select when ID is set #4695

Closed
8 of 16 tasks
Pompiedom opened this issue Nov 24, 2016 · 13 comments
Closed
8 of 16 tasks

Enter key still works on disabled select when ID is set #4695

Pompiedom opened this issue Nov 24, 2016 · 13 comments

Comments

@Pompiedom
Copy link

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate
  • The issue still exists against the latest master branch of Select2
  • This is not a usage question (Those should be directed to the community)
  • I have attempted to find the simplest possible steos to reproduce the issue
  • I have included a failing test as a pull request (Optional)

Steps to reproduce the issue

  1. Create a select with the "disabled" attribute
  2. Init select2
  3. Click with the mouse on the disabled select
  4. Press the enter key
  5. The dropdown doesn't open
  6. Create a select with the "disabled" attribute and add an id attribute e.g. "id='test'"
  7. Click with the mouse on the disabled select
  8. Press the enter key
  9. The dropdown does open

Expected behavior and actual behavior

When I follow those steps, I see the dropdown open on a disabled select, that should never be possible.

Environment

Browsers

  • Google Chrome
  • Mozilla Firefox
  • Internet Explorer 11

Operating System

  • Windows
  • Mac OS X
  • Linux
  • Mobile

Libraries

  • jQuery version: 1.9.1
  • Select2 version: 4.0.3

Isolating the problem

@esl-douglas
Copy link

esl-douglas commented Jul 20, 2017

Still a bug.

I made a temporary fix.
$(document).on('select2:opening.disabled', ':disabled', function() { return false; })

@ericmdantas
Copy link

Yup, also happened to me.

@esl-douglas's fix seems to do the trick. Thanks!

@ericmdantas
Copy link

By the way, these lines seem to be the ones forcing the opening:

this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key === KEYS.ESC || key === KEYS.TAB ||
(key === KEYS.UP && evt.altKey)) {
self.close();
evt.preventDefault();
} else if (key === KEYS.ENTER) {
self.trigger('results:select', {});
evt.preventDefault();
} else if ((key === KEYS.SPACE && evt.ctrlKey)) {
self.trigger('results:toggle', {});
evt.preventDefault();
} else if (key === KEYS.UP) {
self.trigger('results:previous', {});
evt.preventDefault();
} else if (key === KEYS.DOWN) {
self.trigger('results:next', {});
evt.preventDefault();
}
} else {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
(key === KEYS.DOWN && evt.altKey)) {
self.open();
evt.preventDefault();
}
}
});

Maybe just checking the state of the select should do the trick?

@alexweissman
Copy link
Contributor

Sure, want to make the fix, write up a few tests, and submit a PR?

Looks like in other parts of the codebase, we use some simple logic to return early when the control is disabled:

if (this.options.get('disabled')) {
return;
}

@Doolan
Copy link

Doolan commented Jan 23, 2018

This issue also exists in the examples app.
https://select2.org/appearance

Clicking on the disabled dropdown and hitting the space key will allow the user to chose a new option and change the value of the dropdown.

@stale
Copy link

stale bot commented Mar 13, 2019

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 Mar 13, 2019
@stale stale bot closed this as completed Mar 20, 2019
@JonBetts
Copy link

This still seems to be an issue in 4.0.5. I'm surprised more people haven't reported it!

@fr0z3nfyr
Copy link

Any plans to close this in next release?

I guess this is where the test has to be made and return false (on master)

} else {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
(key === KEYS.DOWN && evt.altKey)) {
self.open();
evt.preventDefault();
}
}

@VpocopocoV
Copy link

For anyone using select2.full.min.js, you may search for "a.open" and replace that part of the code with the one below:

this.options.get("disabled") ? b.preventDefault() : a.open()

This will inspect if the select2 element is disabled and prevent any further action.

@stale
Copy link

stale bot commented Oct 12, 2019

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 Oct 12, 2019
@kevin-brown kevin-brown added this to the 4.0.12 milestone Oct 13, 2019
@stale stale bot removed the status: stale label Oct 13, 2019
@kevin-brown kevin-brown modified the milestones: 4.0.12, 4.0.13 Nov 6, 2019
@stale
Copy link

stale bot commented Jan 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 Jan 5, 2020
@stale stale bot closed this as completed Jan 12, 2020
@paxnovem
Copy link
Member

I am going to reopen this issue for another stale cycle.

@kevin-brown
Copy link
Member

This was fixed in 4.0.13 by #5751.

@kevin-brown kevin-brown modified the milestones: 4.1.0, 4.0.13 Apr 25, 2020
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