Skip to content

Commit f14bdf6

Browse files
authored
Fix search box expanding width of container (select2#5595)
This fixes a bug with the search box where, when it had a placeholder, it would expand the width of the selection container because it was too large. This bug was specifically caused by the search box not factoring in the padding surrounding it when caclualting the width it needed to be, which resulted in the search box extending outside of the selection container. This bug was easy to notice if your Select2 was set to have 100% width and if the container it was held within was not a block element. This fixes the bug by switching to using `width()` for calculating the search width instead of using `innerWidth()`, which ignored the surrounding padding. Fixes select2#5517 Closes select2#5518
1 parent 472e5ad commit f14bdf6

6 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/js/select2/selection/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ define([
214214
var width = '';
215215

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module('Selection containers - Inline search - Placeholder');
2+
3+
var MultipleSelection = require('select2/selection/multiple');
4+
var InlineSearch = require('select2/selection/search');
5+
var SelectionPlaceholder = require('select2/selection/placeholder');
6+
7+
var $ = require('jquery');
8+
var Options = require('select2/options');
9+
var Utils = require('select2/utils');
10+
11+
var CustomSelection = Utils.Decorate(
12+
Utils.Decorate(MultipleSelection, SelectionPlaceholder),
13+
InlineSearch
14+
);
15+
16+
test('width does not extend the search box', function (assert) {
17+
assert.expect(2);
18+
19+
var $container = $(
20+
'<div style="width: 100px; display: table-cell">' +
21+
'<div style="width: 100%" ' +
22+
'class="select2-container select2-container--default"></div>' +
23+
'</div>'
24+
);
25+
var container = new MockContainer();
26+
27+
var $element = $('#qunit-fixture .multiple');
28+
var selection = new CustomSelection($element, new Options({
29+
placeholder: 'Test placeholder'
30+
}));
31+
32+
var $selection = selection.render();
33+
selection.bind(container, $container);
34+
35+
// Make it visible so the browser can place focus on the search
36+
$container.append($selection);
37+
$('#qunit-fixture').append($container);
38+
39+
// Update the selection so the search is rendered
40+
selection.update([]);
41+
42+
var $search = $selection.find('input');
43+
44+
assert.equal(
45+
$search.outerWidth(),
46+
60,
47+
'The search should not be the entire width of the container'
48+
);
49+
50+
assert.equal(
51+
$container.children().outerWidth(),
52+
100,
53+
'The container should be the width assigned to the parent in CSS'
54+
);
55+
});

tests/selection/search-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,4 @@ test('search box with text should not close dropdown', function (assert) {
274274
var $search = $selection.find('input');
275275
$search.val('test');
276276
$search.trigger('click');
277-
});
277+
});

tests/unit-jq1.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<script src="selection/multiple-tests.js" type="text/javascript"></script>
9292
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
9393
<script src="selection/search-tests.js" type="text/javascript"></script>
94+
<script src="selection/search-placeholder-tests.js" type="text/javascript"></script>
9495
<script src="selection/single-tests.js" type="text/javascript"></script>
9596
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
9697

tests/unit-jq2.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<script src="selection/multiple-tests.js" type="text/javascript"></script>
9292
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
9393
<script src="selection/search-tests.js" type="text/javascript"></script>
94+
<script src="selection/search-placeholder-tests.js" type="text/javascript"></script>
9495
<script src="selection/single-tests.js" type="text/javascript"></script>
9596
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
9697

tests/unit-jq3.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<script src="selection/multiple-tests.js" type="text/javascript"></script>
9292
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
9393
<script src="selection/search-tests.js" type="text/javascript"></script>
94+
<script src="selection/search-placeholder-tests.js" type="text/javascript"></script>
9495
<script src="selection/single-tests.js" type="text/javascript"></script>
9596
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
9697

0 commit comments

Comments
 (0)