Skip to content

Commit

Permalink
feat(chips): md-add-on-blur functionality
Browse files Browse the repository at this point in the history
* Allows developers to convert the remaining input text into a new chip on input blur.
  This is useful for example, when having chips for email addresses.

Closes angular#3364.
  • Loading branch information
devversion committed Jul 21, 2016
1 parent 0356bed commit 495b135
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
42 changes: 42 additions & 0 deletions src/components/chips/chips.spec.js
Expand Up @@ -202,6 +202,48 @@ describe('<md-chips>', function() {
expect(scope.selectChip).toHaveBeenCalled();
expect(scope.selectChip.calls.mostRecent().args[0]).toBe('Grape');
});

describe('when adding chips on blur', function() {

it('should append a new chip for the remaining text', function() {
var element = buildChips(
'<md-chips ng-model="items" md-add-on-blur="true">' +
'</md-chips>'
);

var input = element.find('input');

expect(scope.items.length).toBe(3);

input.val('Remaining');
input.triggerHandler('change');

// Trigger a blur event, to check if the text was converted properly.
input.triggerHandler('blur');

expect(scope.items.length).toBe(4);
});

it('should not append a new chip if the limit has reached', function() {
var element = buildChips(
'<md-chips ng-model="items" md-add-on-blur="true" md-max-chips="3">' +
'</md-chips>'
);

var input = element.find('input');

expect(scope.items.length).toBe(3);

input.val('Remaining');
input.triggerHandler('change');

// Trigger a blur event, to check if the text was converted properly.
input.triggerHandler('blur');

expect(scope.items.length).toBe(3);
});

});

describe('when removable', function() {

Expand Down
17 changes: 15 additions & 2 deletions src/components/chips/js/chipsController.js
Expand Up @@ -8,13 +8,15 @@ angular
* the models of various input components.
*
* @param $scope
* @param $attrs
* @param $mdConstant
* @param $log
* @param $element
* @param $timeout
* @param $mdUtil
* @constructor
*/
function MdChipsCtrl ($scope, $mdConstant, $log, $element, $timeout, $mdUtil) {
function MdChipsCtrl ($scope, $attrs, $mdConstant, $log, $element, $timeout, $mdUtil) {
/** @type {$timeout} **/
this.$timeout = $timeout;

Expand Down Expand Up @@ -52,7 +54,10 @@ function MdChipsCtrl ($scope, $mdConstant, $log, $element, $timeout, $mdUtil) {
this.hasAutocomplete = false;

/** @type {string} */
this.enableChipEdit = $mdUtil.parseAttributeBoolean(this.mdEnableChipEdit);
this.enableChipEdit = $mdUtil.parseAttributeBoolean($attrs.mdEnableChipEdit);

/** @type {string} */
this.addOnBlur = $mdUtil.parseAttributeBoolean($attrs.mdAddOnBlur);

/**
* Hidden hint text for how to delete a chip. Used to give context to screen readers.
Expand Down Expand Up @@ -503,6 +508,14 @@ MdChipsCtrl.prototype.onInputFocus = function () {

MdChipsCtrl.prototype.onInputBlur = function () {
this.inputHasFocus = false;

var chipBuffer = this.getChipBuffer().trim();

// Only append the chip and reset the chip buffer if the max chips limit isn't reached.
if (this.addOnBlur && chipBuffer && !this.hasMaxChipsReached()) {
this.appendChip(chipBuffer);
this.resetChipBuffer();
}
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/chips/js/chipsDirective.js
Expand Up @@ -98,6 +98,8 @@
* @param {number=} md-max-chips The maximum number of chips allowed to add through user input.
* <br/><br/>The validation property `md-max-chips` can be used when the max chips
* amount is reached.
* @param {boolean=} md-add-on-blur When set to true, remaining text inside of the input will
* be converted into a new chip on blur.
* @param {expression} md-transform-chip An expression of form `myFunction($chip)` that when called
* expects one of the following return values:
* - an object representing the `$chip` input string
Expand Down Expand Up @@ -224,7 +226,6 @@
readonly: '=readonly',
removable: '=mdRemovable',
placeholder: '@',
mdEnableChipEdit: '@',
secondaryPlaceholder: '@',
maxChips: '@mdMaxChips',
transformChip: '&mdTransformChip',
Expand Down

0 comments on commit 495b135

Please sign in to comment.