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

select2:unselect fires before changing value in single-select dropdowns #5049

Closed
6 of 7 tasks
gronostajo opened this issue Sep 22, 2017 · 7 comments
Closed
6 of 7 tasks

Comments

@gronostajo
Copy link
Contributor

gronostajo commented Sep 22, 2017

  1. Create a single-select dropdown with allowClear
  2. Subscribe to select2:unselect event
  3. Select some option
  4. Click the clear button (X)
  5. $(dropdown).val() still returns the value in event handler
  6. Now do the same with multi-select. When select2:unselect fires, the value is already removed.

Expected behavior and actual behavior

This should be consistent for single-selects and multi-selects. Currently for single-selects select2:unselect event is fired before removing the value, but for multi-select value is already removed when the event fires.

Environment

Browsers

  • Google Chrome
  • Mozilla Firefox
  • [?] Internet Explorer

Operating System

  • Windows
  • [?] Mac OS X
  • [?] Linux
  • [?] Mobile

Libraries

  • jQuery version: 3.2.1
  • Select2 version: 4.0.3, master (#c454407)

Isolating the problem

  • This bug happens on the examples page
  • The bug happens consistently across all tested browsers
  • This bug happens when using Select2 without other plugins
  • I can reproduce this bug in a gistrun
@alexweissman
Copy link
Contributor

Does it even clear the value at all, for single select? See #4845.

@gronostajo
Copy link
Contributor Author

Yes, everything seems to work correctly except for the event which is sent too early. (see the gistrun)

As a workaround, the unselect callback can be wrapped in setTimeout.

@alexweissman
Copy link
Contributor

Agreed, it should clear the value before triggering the event. Can you please check the issues and open PRs carefully, to see if this has been mentioned before? I'd be quite surprised as this is a pretty obvious bug.

@gronostajo
Copy link
Contributor Author

I have looked at event- and unselect-related issues and haven't found anything similar.

I imagine that it could be useful to know what was just unselected, but it would be better to pass it as an event field.

@gronostajo
Copy link
Contributor Author

Please see PR #5058 for a fix.

Actually, removed items are already sent as event data, so I have just reversed clearing and event emitting.

@Stadly
Copy link

Stadly commented Oct 27, 2017

If I understand correctly, the issue here was that select2:unselect fired too early. Maybe #4371 is similar, that select2:select fires too early as well, so the value is not populated? If that is the case, maybe it can be fixed in the same way?

@alexweissman
Copy link
Contributor

It's possible. #5058 has already been merged in and released in the 4.0.6-rc.0 pre-release. So, you can try updating to the pre-release and see if that fixes your issue. That would also help us test the pre-release and get it ready for a stable release!

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

3 participants