Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 3f097e0

Browse files
authored
Merge pull request #1905 from DaniVarga/master
fix minimum-input-length with refresh-delay #1898
2 parents 08a0752 + 4910cba commit 3f097e0

File tree

5 files changed

+57
-116
lines changed

5 files changed

+57
-116
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/.idea
55
/.tmp
66
.DS_Store
7+
npm-debug.log
78
*~

npm-debug.log

-104
This file was deleted.

src/uiSelectChoicesDirective.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,26 @@ uis.directive('uiSelectChoices',
3939

4040
choices.attr('ng-repeat', parserResult.repeatExpression(groupByExp))
4141
.attr('ng-if', '$select.open'); //Prevent unnecessary watches when dropdown is closed
42-
42+
4343

4444
var rowsInner = tElement.querySelectorAll('.ui-select-choices-row-inner');
4545
if (rowsInner.length !== 1) {
4646
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
4747
}
4848
rowsInner.attr('uis-transclude-append', ''); //Adding uisTranscludeAppend directive to row element after choices element has ngRepeat
4949

50-
// If IE8 then need to target rowsInner to apply the ng-click attr as choices will not capture the event.
50+
// If IE8 then need to target rowsInner to apply the ng-click attr as choices will not capture the event.
5151
var clickTarget = $window.document.addEventListener ? choices : rowsInner;
5252
clickTarget.attr('ng-click', '$select.select(' + parserResult.itemName + ',$select.skipFocusser,$event)');
53-
53+
5454
return function link(scope, element, attrs, $select) {
5555

56-
56+
5757
$select.parseRepeatAttr(attrs.repeat, groupByExp, groupFilterExp); //Result ready at $select.parserResult
5858
$select.disableChoiceExpression = attrs.uiDisableChoice;
5959
$select.onHighlightCallback = attrs.onHighlight;
60-
$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;
60+
$select.minimumInputLength = parseInt(attrs.minimumInputLength) || 0;
61+
$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;
6162

6263
scope.$watch('$select.search', function(newValue) {
6364
if(newValue && !$select.open && $select.multiple) $select.activate(false, true);

src/uiSelectController.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,16 @@ uis.controller('uiSelectCtrl',
297297
$timeout.cancel(_refreshDelayPromise);
298298
}
299299
_refreshDelayPromise = $timeout(function() {
300-
var refreshPromise = $scope.$eval(refreshAttr);
301-
if (refreshPromise && angular.isFunction(refreshPromise.then) && !ctrl.refreshing) {
302-
ctrl.refreshing = true;
303-
refreshPromise.finally(function() {
304-
ctrl.refreshing = false;
305-
});
306-
}}, ctrl.refreshDelay);
300+
if ($scope.$select.search.length >= $scope.$select.minimumInputLength) {
301+
var refreshPromise = $scope.$eval(refreshAttr);
302+
if (refreshPromise && angular.isFunction(refreshPromise.then) && !ctrl.refreshing) {
303+
ctrl.refreshing = true;
304+
refreshPromise.finally(function() {
305+
ctrl.refreshing = false;
306+
});
307+
}
308+
}
309+
}, ctrl.refreshDelay);
307310
}
308311
};
309312

test/select.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,46 @@ describe('ui-select tests', function () {
17091709
expect(scope.fetchFromServer).toHaveBeenCalledWith('red');
17101710
});
17111711

1712+
1713+
it('should call refresh function respecting minimum input length option with given refresh-delay', function () {
1714+
1715+
var el = compileTemplate(
1716+
'<ui-select ng-model="selection.selected"> \
1717+
<ui-select-match> \
1718+
</ui-select-match> \
1719+
<ui-select-choices repeat="person in people | filter: $select.search" \
1720+
refresh="fetchFromServer($select.search)" refresh-delay="1" minimum-input-length="3"> \
1721+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1722+
<div ng-if="person.name==\'Wladimir\'"> \
1723+
<span class="only-once">I should appear only once</span>\
1724+
</div> \
1725+
</ui-select-choices> \
1726+
</ui-select>'
1727+
);
1728+
1729+
scope.fetchFromServer = function(){};
1730+
1731+
spyOn(scope, 'fetchFromServer');
1732+
1733+
el.scope().$select.search = 'redd';
1734+
scope.$digest();
1735+
$timeout.flush();
1736+
expect(scope.fetchFromServer).toHaveBeenCalledWith('redd');
1737+
1738+
1739+
el.scope().$select.search = 'red';
1740+
scope.$digest();
1741+
el.scope().$select.search = 're';
1742+
scope.$digest();
1743+
el.scope().$select.search = 'r';
1744+
scope.$digest();
1745+
$timeout.flush();
1746+
expect(scope.fetchFromServer).not.toHaveBeenCalledWith('r');
1747+
1748+
1749+
});
1750+
1751+
17121752
it('should format view value correctly when using single property binding and refresh function', function () {
17131753

17141754
var el = compileTemplate(

0 commit comments

Comments
 (0)