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

Add option 'limit' to limit the number of items that can be selected #348

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@
processed = _handleMatchSelection(key);
}

if (!processed && ctrl.multiple && ctrl.limit !== undefined && ctrl.limit <= ctrl.selected.length) {
processed = true;
}

if (!processed && ctrl.items.length > 0) {
processed = _handleDropDownSelection(key);
}
Expand Down Expand Up @@ -605,6 +609,7 @@

$select.onSelectCallback = $parse(attrs.onSelect);
$select.onRemoveCallback = $parse(attrs.onRemove);
$select.limit = (angular.isDefined(attrs.limit)) ? parseInt(attrs.limit, 10) : undefined;

//From view --> model
ngModel.$parsers.unshift(function (inputValue) {
Expand Down
43 changes: 39 additions & 4 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ describe('ui-select tests', function() {
return el.scope().$select.open && el.hasClass('open');
}

function createKeydownEvent(keyCode) {
var event = jQuery.Event("keydown");
event.which = keyCode;
event.keyCode = keyCode;
return event;
}

function triggerKeydown(element, keyCode) {
var e = jQuery.Event("keydown");
e.which = keyCode;
e.keyCode = keyCode;
element.trigger(e);
element.trigger(createKeydownEvent(keyCode));
}

function setSearchText(el, text) {
Expand Down Expand Up @@ -1011,6 +1015,7 @@ describe('ui-select tests', function() {
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
if (attrs.limit !== undefined) { attrsHtml += ' limit="' + attrs.limit + '"'; }
}

return compileTemplate(
Expand Down Expand Up @@ -1307,6 +1312,36 @@ describe('ui-select tests', function() {
expect(el.find('.ui-select-match-item').length).toBe(2);
});

it('should allow more searches if limit is set but not reached', function () {
var el,
searchInput,
event = createKeydownEvent('a'),
preventDefaultCalled = false;
scope.selection.selectedMultiple = [scope.people[4], scope.people[5]];
el = createUiSelectMultiple({limit: 3});
searchInput = el.find('.ui-select-search');
event.preventDefault = function () {
preventDefaultCalled = true;
};
searchInput.trigger(event);
expect(preventDefaultCalled).toBe(false);
});

it('should not allow more searches if limit is set and reached', function () {
var el,
searchInput,
event = createKeydownEvent('a'),
preventDefaultCalled = false;
scope.selection.selectedMultiple = [scope.people[4], scope.people[5], scope.people[6]];
el = createUiSelectMultiple({limit: 3});
searchInput = el.find('.ui-select-search');
event.preventDefault = function () {
preventDefaultCalled = true;
};
searchInput.trigger(event);
expect(preventDefaultCalled).toBe(true);
});

it('should parse the items correctly using single property binding', function() {

scope.selection.selectedMultiple = ['wladimir@email.com', 'samantha@email.com'];
Expand Down