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

Do not propagate click when search box is not empty #5580

Merged
merged 1 commit into from Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/js/select2/selection/search.js
Expand Up @@ -90,6 +90,12 @@ define([
}
});

this.$selection.on('click', '.select2-search--inline', function (evt) {
if (self.$search.val()) {
evt.stopPropagation();
}
});

// Try to detect the IE version should the `documentMode` property that
// is stored on the document. This is only implemented in IE and is
// slightly cleaner than doing a user agent check.
Expand Down
86 changes: 86 additions & 0 deletions tests/selection/search-tests.js
Expand Up @@ -188,4 +188,90 @@ test('the focus event shifts the focus', function (assert) {
$search[0],
'The search did not have focus originally'
);
});

test('search box without text should propagate click', function (assert) {
assert.expect(1);

var $container = $('#qunit-fixture .event-container');
var container = new MockContainer();

var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);

var $element = $('#qunit-fixture .multiple');
var selection = new CustomSelection($element, options);

var $selection = selection.render();
selection.bind(container, $container);

// Update the selection so the search is rendered
selection.update([]);

// Make it visible so the browser can place focus on the search
$container.append($selection);

$selection.on('click', function () {
assert.ok(true, 'The click event should not have been trapped');
});

var $search = $selection.find('input');
$search.trigger('click');
});

test('search box with text should not propagate click', function (assert) {
assert.expect(0);

var $container = $('#qunit-fixture .event-container');
var container = new MockContainer();

var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);

var $element = $('#qunit-fixture .multiple');
var selection = new CustomSelection($element, options);

var $selection = selection.render();
selection.bind(container, $container);

// Update the selection so the search is rendered
selection.update([]);

// Make it visible so the browser can place focus on the search
$container.append($selection);

$selection.on('click', function () {
assert.ok(false, 'The click event should have been trapped');
});

var $search = $selection.find('input');
$search.val('test');
$search.trigger('click');
});

test('search box with text should not close dropdown', function (assert) {
assert.expect(0);

var $container = $('#qunit-fixture .event-container');
var container = new MockContainer();

var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);

var $element = $('#qunit-fixture .multiple');
var selection = new CustomSelection($element, options);

var $selection = selection.render();
selection.bind(container, $container);

// Update the selection so the search is rendered
selection.update([]);

// Make it visible so the browser can place focus on the search
$container.append($selection);

container.on('close', function () {
assert.ok(false, 'The dropdown should not have closed');
});

var $search = $selection.find('input');
$search.val('test');
$search.trigger('click');
});