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

Bootstrap-select steals focus on form.checkValidity (line 449) #1764

Closed
WimWidgets opened this issue Jul 12, 2017 · 2 comments
Closed

Bootstrap-select steals focus on form.checkValidity (line 449) #1764

WimWidgets opened this issue Jul 12, 2017 · 2 comments

Comments

@WimWidgets
Copy link

WimWidgets commented Jul 12, 2017

I have a case in which the submit button of a form should stay disabled until the form is valid. In order to do this I listen for changes in input and constantly check if the form is valid with the form.checkValidity method. When using checkValidity all underlying inputs get validated. This triggers the invalid event on line 446 in bootstrap-select.js which in turn focuses the bootstrap-select button on line 449 (in the dist version).

this.$element.on('invalid', function () {
  that.$button
    .addClass('bs-invalid')
    .focus();
  [...]
});

So when I'm inserting some input in a random field and the script checks if the form is valid already, the bootstrap-select component steals focus away from the field I was working in.

See my example:
https://codepen.io/Wimski/pen/weRPVo
Start typing in the first field.

All the code I use:

$(function() {
	$('.bss').selectpicker();
	var frm = $('form');
	frm.on('input change', ':input', function() {
		toggleSubmitButton(frm);
	});
	toggleSubmitButton(frm);
	frm.find(':input').blur(); // remove bootstrap-select focus on initial check
});
function toggleSubmitButton (frm) {
	if (!frm.length) return;
	var valid = (!frm.get(0).checkValidity || frm.get(0).checkValidity());
	frm.find('button[type="submit"]').prop('disabled', !valid);
}
@WimWidgets WimWidgets changed the title Bootstrap-select steals focus on form.checkValidity (line 426) Bootstrap-select steals focus on form.checkValidity (line 449) Jul 12, 2017
@WimWidgets
Copy link
Author

WimWidgets commented Jul 14, 2017

@caseyjhol no reply? Nothing?

Here is an example showing the expected behavior of a select element being focused when responding to validation. See the difference between checkValidity and reportValidity/submit.
https://codepen.io/Wimski/pen/jwJwNR

It's too bad the focusing of the element lags a bit behind the invalid event, but a simple timeout can tell you a lot. So my suggestion would be to only focus the bootstrap-select button when the select element gets focused immediately after being invalidated.

@caseyjhol
Copy link
Member

caseyjhol commented Jul 14, 2017

Sorry - I closed it because I just fixed in master (via 620fd5a), and will be available in the next release. Here's an example: https://codepen.io/caseyjhol/pen/MoxQwq.

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

No branches or pull requests

2 participants