Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
feat(tagsInput): Make maxLength consistent with minLength
Browse files Browse the repository at this point in the history
Make maxLength option behave like minLength option, i.e. instead of
preventing lengthy tags from being typed it allows them but keep them
from being added.

Closes #53.
  • Loading branch information
mbenford committed Mar 24, 2014
1 parent 0205f58 commit 1458ba6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
var tagText = getTagText(tag);

return tagText.length >= options.minLength &&
tagText.length <= (options.maxLength || tagText.length) &&
options.allowedTagsPattern.test(tagText) &&
!findInObjectArray(self.items, tag, options.displayProperty);
};
Expand Down
1 change: 0 additions & 1 deletion templates/tags-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<input type="text"
class="input"
placeholder="{{options.placeholder}}"
maxlength="{{options.maxLength}}"
tabindex="{{options.tabindex}}"
ng-model="newTag.text"
ng-change="newTagChange()"
Expand Down
65 changes: 53 additions & 12 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,28 @@ describe('tags-input directive', function() {
});

describe('min-length option', function() {
it('adds a new tag only if its length is greater than or equal to min-length option', function() {
it('initializes the option to 3', function() {
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.minLength).toBe(3);
});

it('adds a new tag if the input length is greater than the min-length option', function() {
// Arrange
compile('min-length="5"');

// Act
newTag('foobar');

// Assert
expect($scope.tags).toEqual([{ text: 'foobar' }]);
expect(getInput()).not.toHaveClass('invalid-tag');

});

it('does not add a new tag if the input length is less than the min-length option', function() {
// Arrange
compile('min-length="5"');

Expand All @@ -596,32 +617,52 @@ describe('tags-input directive', function() {

// Assert
expect($scope.tags).toEqual([]);
expect(getInput()).toHaveClass('invalid-tag');
});
});

it('initializes the option to 3', function() {
describe('max-length option', function() {
it('initializes the option to undefined', function() {
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.minLength).toBe(3);
expect(isolateScope.options.maxLength).toBeUndefined();
});
});

describe('max-length option', function() {
it('sets the maxlength attribute of the input field to max-length option', function() {
// Arrange/Act
compile('max-length="10"');
it('adds a new tag if the input length is less than the max-length option', function() {
// Arrange
compile('max-length="5"');

// Act
newTag('foo');

// Assert
expect(getInput().attr('maxlength')).toBe('10');
expect($scope.tags).toEqual([{ text: 'foo' }]);
expect(getInput()).not.toHaveClass('invalid-tag');
});

it('initializes the option to empty', function() {
// Arrange/Act
it('adds a new tag of any length if the max-length option has no value', function() {
// Arrange
compile();

// Act
newTag('foooooooooooooooooooooooooooooooooooooooooooooooooooooooobar');

// Assert
expect(getInput().attr('maxlength')).toBe('');
expect($scope.tags).toEqual([{ text: 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooobar' }]);
});

it('does not add a new tag if the input length is greater than the max-length option', function() {
// Arrange
compile('max-length="5"');

// Act
newTag('foobar');

// Assert
expect($scope.tags).toEqual([]);
expect(getInput()).toHaveClass('invalid-tag');
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/test-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</style>
</head>
<body ng-controller="Ctrl">
<tags-input style="display: none" class="foo" ng-model="tags" limit-tags="5"
<tags-input class="foo" ng-model="tags" limit-tags="5"
replace-spaces-with-dashes="true"
enable-editing-last-tag="true"
placeholder="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Expand All @@ -53,7 +53,7 @@
max-results-to-show="10">
</auto-complete>
</tags-input>
<tags-input ng-init="tags2=['item1', 'item2', 'item3']" ng-model="tags2" display-property="label" add-on-blur="false" placeholder="Add a superhero">
<tags-input ng-init="tags2=['item1', 'item2', 'item3']" ng-model="tags2" display-property="label" add-on-blur="false" placeholder="Add a superhero" max-length="5">
<auto-complete source="loadItems2($query)"
debounce-delay="0"
min-length="1"
Expand Down

0 comments on commit 1458ba6

Please sign in to comment.