Skip to content

Commit 7c2121a

Browse files
author
Tomas Kirda
committed
Preserve original width option. Adjust width of the suggestions container each time if width is set to 'auto'.
1 parent 3512a4a commit 7c2121a

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

demo.htm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ <h2>Custom Lookup Container</h2>
2929
<div id="suggestions-container" style="position: relative; float: left; width: 400px; margin: 10px;"></div>
3030
</div>
3131
</div>
32+
33+
<div style="width: 50%; margin: 0 auto; clear: both;">
34+
<h2>Dynamic Width</h2>
35+
<p>Type country name in english:</p>
36+
<div>
37+
<input type="text" name="country" id="autocomplete-dynamic" style="width: 100%; border-width: 5px;"/>
38+
</div>
39+
</div>
3240

3341
<script type="text/javascript" src="scripts/jquery-1.8.2.min.js"></script>
3442
<script type="text/javascript" src="scripts/jquery.mockjax.js"></script>

scripts/demo.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ $(function () {
5353
lookup: countriesArray,
5454
appendTo: '#suggestions-container'
5555
});
56+
57+
// Initialize autocomplete with custom appendTo:
58+
$('#autocomplete-dynamic').autocomplete({
59+
lookup: countriesArray
60+
});
5661

5762
});
5863

spec/autocompleteBehavior.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('Autocomplete', function () {
132132
});
133133
});
134134

135-
it('Should execute onSearchCompleted', function () {
135+
it('Should execute onSearchComplete', function () {
136136
var input = document.createElement('input'),
137137
completeQuery,
138138
ajaxExecuted = false,
@@ -421,4 +421,21 @@ describe('Autocomplete', function () {
421421
expect(data).toBeFalsy();
422422
});
423423
});
424+
425+
it('Should set width to be greater than zero', function () {
426+
var input = $(document.createElement('input')),
427+
instance,
428+
width;
429+
430+
input.autocomplete({
431+
lookup: [{ value: 'Jamaica', data: 'B' }]
432+
});
433+
434+
input.val('Jam');
435+
instance = input.autocomplete();
436+
instance.onValueChange();
437+
width = $(instance.suggestionsContainer).width();
438+
439+
expect(width).toBeGreaterThan(0);
440+
});
424441
});

spec/runner.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<!-- jQuery -->
1111
<script src="../scripts/jquery-1.8.2.min.js"></script>
1212
<script src="../scripts/jquery.mockjax.js"></script>
13+
<script type="text/javascript">
14+
window.JSON || document.write('<scr' + 'ipt src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.min.js"><\/scr' + 'ipt>');
15+
</script>
1316

1417
<!-- Autocomplete -->
1518
<script src="../src/jquery.autocomplete.js"></script>

src/jquery.autocomplete.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,16 @@
138138
}
139139
};
140140

141-
// Determine suggestions width:
142-
if (!options.width || options.width === 'auto') {
143-
options.width = that.el.outerWidth();
144-
}
145-
146141
that.suggestionsContainer = Autocomplete.utils.createNode('<div class="' + options.containerClass + '" style="position: absolute; display: none;"></div>');
147142

148143
container = $(that.suggestionsContainer);
149144

150-
container.appendTo(options.appendTo).width(options.width);
145+
container.appendTo(options.appendTo);
146+
147+
// Only set width if it was provided:
148+
if (options.width !== 'auto') {
149+
container.width(options.width);
150+
}
151151

152152
// Listen for mouse over event on suggestions list:
153153
container.on('mouseover.autocomplete', suggestionSelector, function () {
@@ -441,13 +441,23 @@
441441
className = that.classes.suggestion,
442442
classSelected = that.classes.selected,
443443
container = $(that.suggestionsContainer),
444-
html = '';
444+
html = '',
445+
width;
445446

446447
// Build suggestions inner HTML:
447448
$.each(that.suggestions, function (i, suggestion) {
448449
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>';
449450
});
450451

452+
// If width is auto, adjust width before displaying suggestions,
453+
// because if instance was created before input had width, it will be zero.
454+
// Also it adjusts if input width has changed.
455+
// -2px to account for suggestions border.
456+
if (that.options.width === 'auto') {
457+
width = that.el.outerWidth() - 2;
458+
container.width(width > 0 ? width : 300);
459+
}
460+
451461
container.html(html).show();
452462
that.visible = true;
453463

0 commit comments

Comments
 (0)