Skip to content

Commit

Permalink
feat(tagsInput): add newTagText option
Browse files Browse the repository at this point in the history
Add an option for binding to the text in the input element.
This feature is important because it allows developers to know when
the input has changed as well the state of the input at the time of
the change. An example use case is doing custom validations that
regular expressions couldn't satisfy.

Closes mbenford#197.
  • Loading branch information
LoganBarnett committed Mar 14, 2015
1 parent b812a44 commit 4335880
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Renders an input box with tag editing support.
*
* @param {string} ngModel Assignable angular expression to data-bind to.
* @param {string} newTagText Assignable angular expression for data-binding to the underlying input for a new tag.
* @param {string=} [displayProperty=text] Property to be rendered as the tag label.
* @param {string=} [type=text] Type of the input element. Only 'text', 'email' and 'url' are supported values.
* @param {number=} tabindex Tab order of the control.
Expand Down Expand Up @@ -121,6 +122,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
require: 'ngModel',
scope: {
tags: '=ngModel',
newTagText: '=?',
onTagAdded: '&',
onInvalidTag: '&',
onTagRemoved: '&'
Expand Down Expand Up @@ -200,6 +202,10 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)

scope.newTag = { text: '', invalid: null };

scope.$watch('newTag.text', function(value) {
scope.newTagText = value;
});

scope.getDisplayText = function(tag) {
return safeToString(tag[options.displayProperty]);
};
Expand Down
15 changes: 15 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,21 @@ describe('tags-input directive', function() {
});
});

describe('new-tag-text option', function() {
it('updates as text is typed into the input field', function() {
// Arrange
compile('new-tag-text="innerModel"');

// Act
sendKeyPress('f'.charCodeAt(0));
sendKeyPress('o'.charCodeAt(0));
sendKeyPress('o'.charCodeAt(0));

// Assert
expect($scope.innerModel).toEqual('foo');
});
});

describe('tabindex option', function() {
it('sets the input field tab index', function() {
// Arrange/Act
Expand Down

0 comments on commit 4335880

Please sign in to comment.