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

Select trigger should no run close event when select2 is already closed #4829

Closed
stmn opened this issue Mar 13, 2017 · 3 comments
Closed

Select trigger should no run close event when select2 is already closed #4829

stmn opened this issue Mar 13, 2017 · 3 comments

Comments

@stmn
Copy link

stmn commented Mar 13, 2017

I believe that code below should only be responsible for value change:

$("#selectOutsideViewport").select2('trigger', 'select', {data: {id: 123, text: 321}});

But currently it also trigger 'close' event when closeOnSelect option is enabled what causes that page is scrolled to that select what is not desirable.

It can be reproduced here on any browser: https://select2.github.io/examples.html
Slide down to bottom and paste:

$("#id_label_single").select2('trigger', 'select', {data: {id: 123, text: 321}});

Closing should be triggered only when something is opened or page scrolling should be as option.
I fixed it by changing:

  CloseOnSelect.prototype._selectTriggered = function (_, evt) {
    var originalEvent = evt.originalEvent;

    // Don't close if the control key is being held
    if (originalEvent && originalEvent.ctrlKey) {
      return;
    }
    
    this.trigger('close', {
      originalEvent: originalEvent,
      originalSelect2Event: evt
    });
  };

to:

 CloseOnSelect.prototype._selectTriggered = function (_, evt) {
   var originalEvent = evt.originalEvent;

   // Don't close if the control key is being held
   if (originalEvent && originalEvent.ctrlKey) {
     return;
   }

   // Don't close something what is already closed
   if (!this.$element.data('select2').isOpen()) {
     return;
   }

   this.trigger('close', {
       originalEvent: originalEvent,
       originalSelect2Event: evt
   });
 };
@alexweissman
Copy link
Contributor

This appears to be related to the bug described in #4983. Is this correct?

@stmn
Copy link
Author

stmn commented Sep 25, 2017

Partly yes... but I suggest to focus not only on site scrolling problem but also on triggering unnecessary event because it can produce some other problems when someone set listener on closing event (close event triggered but nothing was closed, only selected). IMHO.

@alexweissman
Copy link
Contributor

Ok. Since that issue seems to explain the manifestation of this bug a little better, could you please copy your findings over to that issue? There is another proposed solution there which is somewhat different from yours, so I'd like to see a discussion about the best way to solve this.

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

2 participants