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

Fix search box expanding width of container #5595

Merged
merged 1 commit into from Jul 30, 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
2 changes: 1 addition & 1 deletion src/js/select2/selection/search.js
Expand Up @@ -214,7 +214,7 @@ define([
var width = '';

if (this.$search.attr('placeholder') !== '') {
width = this.$selection.find('.select2-selection__rendered').innerWidth();
width = this.$selection.find('.select2-selection__rendered').width();
} else {
var minimumWidth = this.$search.val().length + 1;

Expand Down
55 changes: 55 additions & 0 deletions tests/selection/search-placeholder-tests.js
@@ -0,0 +1,55 @@
module('Selection containers - Inline search - Placeholder');

var MultipleSelection = require('select2/selection/multiple');
var InlineSearch = require('select2/selection/search');
var SelectionPlaceholder = require('select2/selection/placeholder');

var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');

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

test('width does not extend the search box', function (assert) {
assert.expect(2);

var $container = $(
'<div style="width: 100px; display: table-cell">' +
'<div style="width: 100%" ' +
'class="select2-container select2-container--default"></div>' +
'</div>'
);
var container = new MockContainer();

var $element = $('#qunit-fixture .multiple');
var selection = new CustomSelection($element, new Options({
placeholder: 'Test placeholder'
}));

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

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

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

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

assert.equal(
$search.outerWidth(),
60,
'The search should not be the entire width of the container'
);

assert.equal(
$container.children().outerWidth(),
100,
'The container should be the width assigned to the parent in CSS'
);
});
2 changes: 1 addition & 1 deletion tests/selection/search-tests.js
Expand Up @@ -274,4 +274,4 @@ test('search box with text should not close dropdown', function (assert) {
var $search = $selection.find('input');
$search.val('test');
$search.trigger('click');
});
});
1 change: 1 addition & 0 deletions tests/unit-jq1.html
Expand Up @@ -91,6 +91,7 @@
<script src="selection/multiple-tests.js" type="text/javascript"></script>
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
<script src="selection/search-tests.js" type="text/javascript"></script>
<script src="selection/search-placeholder-tests.js" type="text/javascript"></script>
<script src="selection/single-tests.js" type="text/javascript"></script>
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>

Expand Down
1 change: 1 addition & 0 deletions tests/unit-jq2.html
Expand Up @@ -91,6 +91,7 @@
<script src="selection/multiple-tests.js" type="text/javascript"></script>
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
<script src="selection/search-tests.js" type="text/javascript"></script>
<script src="selection/search-placeholder-tests.js" type="text/javascript"></script>
<script src="selection/single-tests.js" type="text/javascript"></script>
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>

Expand Down
1 change: 1 addition & 0 deletions tests/unit-jq3.html
Expand Up @@ -91,6 +91,7 @@
<script src="selection/multiple-tests.js" type="text/javascript"></script>
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
<script src="selection/search-tests.js" type="text/javascript"></script>
<script src="selection/search-placeholder-tests.js" type="text/javascript"></script>
<script src="selection/single-tests.js" type="text/javascript"></script>
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>

Expand Down