Skip to content

Commit 2fbd285

Browse files
committed
fix(uiSelectCtrl): fix input width calculations
input is not too big or too small - it takes the whole remaining part of the current row. If smaller than the min input width - goes to the next row closes angular-ui#1980, possibly angular-ui#2010
1 parent d62cac2 commit 2fbd285

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/uiSelectController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,14 +528,14 @@ uis.controller('uiSelectCtrl',
528528
container = ctrl.$element[0],
529529
calculateContainerWidth = function() {
530530
// Return the container width only if the search input is visible
531-
return container.clientWidth * !!input.offsetParent;
531+
return $(container).width() * !!input.offsetParent; // width must be WITHOUT padding - to calculate space for text correctly
532532
},
533533
updateIfVisible = function(containerWidth) {
534534
if (containerWidth === 0) {
535535
return false;
536536
}
537537
var inputWidth = containerWidth - input.offsetLeft;
538-
if (inputWidth < 50) inputWidth = containerWidth;
538+
if (inputWidth < 50) inputWidth = containerWidth; // TODO make 50 a parameter (minInputWidth) which can be set by user
539539
ctrl.searchInput.css('width', inputWidth+'px');
540540
return true;
541541
};

test/select.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,43 @@ describe('ui-select tests', function () {
20092009

20102010
el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
20112011
expect(el.scope().$select.sizeSearchInput).toHaveBeenCalled();
2012+
});
2013+
2014+
it('input should take the whole remaining part of the current row, or, if smaller than the min input width, go to the next row', function () {
2015+
//scope.selection.selectedMultiple = [scope.people[4], scope.people[5]]; //Wladimir & Samantha
2016+
var el = createUiSelectMultiple({
2017+
tagging: '',
2018+
taggingLabel: 'false'
2019+
});
2020+
2021+
//angular.element(document.body).css("width", "100%");
2022+
angular.element(document.body).append(el);
2023+
$timeout.flush(); // needed to make input take it's real width, not 4 or 10 px
2024+
2025+
var searchInput = el.find('.ui-select-search');
20122026

2027+
// no item selected, input should fill the whole row
2028+
expect(searchInput.outerWidth()).toBe(792);
2029+
2030+
clickItem(el, 'Wladimir');
2031+
$timeout.flush();
2032+
// 2 items are selected, input should be less than 100%
2033+
expect(searchInput.outerWidth()).toBe(548); // remaining width of the row
2034+
2035+
clickItem(el, 'Samantha');
2036+
$timeout.flush();
2037+
// input should be even smaller than before
2038+
expect(searchInput.outerWidth()).toBe(304);
2039+
2040+
clickItem(el, 'Adrian');
2041+
$timeout.flush();
2042+
// min input width is 50px (unfortunately hardcoded), so we are still in the first row
2043+
expect(searchInput.outerWidth()).toBe(98);
2044+
2045+
clickItem(el, 'Nicole');
2046+
$timeout.flush();
2047+
// input goes to the second row and should still fill the whole row minus the item width (whole row should be clickable)
2048+
expect(searchInput.outerWidth()).toBe(649);
20132049
});
20142050

20152051
it('should update size of search input use container width', function () {

0 commit comments

Comments
 (0)