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 loses focus after selecting Item #5185

Closed
PieterjanDeClippel opened this issue Jan 5, 2018 · 18 comments · Fixed by #5576
Closed

Select2 loses focus after selecting Item #5185

PieterjanDeClippel opened this issue Jan 5, 2018 · 18 comments · Fixed by #5576

Comments

@PieterjanDeClippel
Copy link

PieterjanDeClippel commented Jan 5, 2018

Issue apparently already happened earlier: #4971.
However I haven't been bothered by it yet. All the time I've been using Select2, it's been working fine (v4.0.4 works fine)

Issue is available at the very demo page. Just start typing something, and select a suggested item. Notice that the Select2 loses focus. My version is v4.0.6-rc.1

@alexweissman alexweissman modified the milestone: 4.0.6 Jan 8, 2018
@alexweissman
Copy link
Contributor

I have confirmed that this problem is reintroduced in 4.0.6-rc.1. See this bin.

@PieterjanDeClippel in the future, you need to create a minimal, complete, and verifiable example using our JSBin template so that we can easily confirm these regressions.

@alexweissman
Copy link
Contributor

The only change I can think of that might have introduced this bug, is the addition of blur in this commit. This was merged from #4680 without extensive testing. Could you please confirm if this is the culprit?

@s1lver
Copy link

s1lver commented Feb 12, 2018

Hi!
Select2 4.0.5

I seem to have a similar problem. In the IE (Edge) browser, there is a loss of focus. Multi-selection and tagging are enabled. Normally only the first value is entered, all subsequent ones on 1-2 characters lose focus.

@timblanq
Copy link

Hej :)
@alexweissman, the referenced commit seems not to be the issue.
I removed the line to test it out, but the behavior didn't change.

@kzkn
Copy link

kzkn commented Feb 20, 2018

Probably this commit. See this bin.

Here is a commit for the jsbin: kzkn@680984f

@timblanq
Copy link

@kzkn This fix doesn't seem to work for me…
But it works in your JSBin example. This one really starts to confuse me.

@kzkn
Copy link

kzkn commented Feb 21, 2018

@tallon1252 The commit edits dist/js/select2.js directly, and use this from the jsbin. Perhaps, you built it yourself?

@timblanq
Copy link

@kzkn I just removed the lines you mentioned in directly in the select2.js…

@kzkn
Copy link

kzkn commented May 20, 2018

Updated my hotfix-5185 branch. How is this?
I have tested on Firefox and Chrome (on Linux), works fine.

By the way, what is 45ee734 solving? Can we remove this?

@nino-s
Copy link
Contributor

nino-s commented Jun 11, 2018

@alexweissman heyho alex, I am not at home actually to test it correctly, but @kzkn seems to be right.

Why should a 0 milliseconds delayed command should help while focusing? #45ee734 What happens is, that some parts of the code afterwards is executed even before self.$selection.focus(); is executed.

The focus function

window.setTimeout(function () {
  self.$selection.focus();
}, 0);

triggers the focus event

this.$selection.on('focus', function (evt) {
  self.trigger('focus', evt);
});

I may be wrong, but I think the problem is, that the delay causes the event evt to have a different relatedTarget as it would have without the delay

@fuzzbomb
Copy link

It also loses focus after deleting an item, in the multiple select time on the basic usage page.

@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
@s1lver
Copy link

s1lver commented Mar 14, 2019

This issue is still relevant

@stale stale bot removed the status: stale label Mar 14, 2019
@jmalatia
Copy link

+1

1 similar comment
@HeppyEkberg
Copy link

+1

@mtaylor091
Copy link

I found that 45ee734 fixes and issue with scrolling when they have a select element open. Before this commit if the select element was open and you scrolled down the page and clicked a button or input it would scroll you back to the select. This check in lets the active element change and then it checks if its different before selecting focus again.

On the other hand, without the commit and active element check, it lets you tab through a form with a select element. Without it selecting the select2 element, it loses focus all together and you cant tab to the next input. In my case the active document is the body.

Somehow it needs to know that I just closed the select but I am still on select form versus I just closed the select because I am trying to type in another field or select a button while the select2 still has focus.

@mjlabe
Copy link

mjlabe commented May 23, 2019

As a temporary fix, I got it to work with this:

$('.my_class').click(function () {
    $('.select2-search__field').focus()
});

@ivaynberg ivaynberg added the 4.x label Jun 27, 2019
kevin-brown added a commit that referenced this issue Jul 21, 2019
This behaviour, where the focus was happening on a small asynchronous
delay, was added in commit 933189b late last year in a commit with
no supporting tickets or commit message explaining what the change
was trying to accomplish. This commit has effectly been reverted within
this commit, since it caused a regression in how focus was being
assigned that made it inconsistent with a standard `<select>`.

A test was added for this which ensures that we won't see that
regression again.

Fixes #5532
Fixes #5185
Closes #5552
kevin-brown added a commit that referenced this issue Jul 21, 2019
This behaviour, where the focus was happening on a small asynchronous
delay, was added in commit 933189b late last year in a commit with
no supporting tickets or commit message explaining what the change
was trying to accomplish. This commit has effectly been reverted within
this commit, since it caused a regression in how focus was being
assigned that made it inconsistent with a standard `<select>`.

A test was added for this which ensures that we won't see that
regression again.

Fixes #5532
Fixes #5185
Closes #5552
@CemplexIT
Copy link

We've been having a similar issue - it turned out that the underlying select element was gaining focus. I believe that some of our CSS was doing something to make this happen but I couldn't find the culprit. We added a display none tag to the css for the class below that is applied when the select is turned into a select2 to fully hide the select and that seemed to do the trick for us.

.select2-hidden-accessible { display: none; }

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

Successfully merging a pull request may close this issue.