Skip to content

Commit 66cf8f0

Browse files
DmitryGoncharmsiemczyk
authored andcommitted
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 (cherry picked from commit 2fbd285)
1 parent 32829a3 commit 66cf8f0

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/uiSelectController.js

+2-2
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

+36
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,43 @@ describe('ui-select tests', function () {
20052005

20062006
el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
20072007
expect(el.scope().$select.sizeSearchInput).toHaveBeenCalled();
2008+
});
2009+
2010+
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 () {
2011+
//scope.selection.selectedMultiple = [scope.people[4], scope.people[5]]; //Wladimir & Samantha
2012+
var el = createUiSelectMultiple({
2013+
tagging: '',
2014+
taggingLabel: 'false'
2015+
});
2016+
2017+
//angular.element(document.body).css("width", "100%");
2018+
angular.element(document.body).append(el);
2019+
$timeout.flush(); // needed to make input take it's real width, not 4 or 10 px
2020+
2021+
var searchInput = el.find('.ui-select-search');
20082022

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

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

0 commit comments

Comments
 (0)