Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(chips): do not trim the input model.
Browse files Browse the repository at this point in the history
* The chips input should not trim the text, because otherwise the buffer will be always falsey, even when there are spaces in the input, and this would cause the backspace not to work.

Fixes #7243

Closes #7748
  • Loading branch information
devversion authored and ThomasBurleson committed Mar 30, 2016
1 parent 9c85915 commit 2d020e1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,59 @@ describe('<md-chips>', function() {
expect(enterEvent.preventDefault).toHaveBeenCalled();
}));

it('should trim the buffer when a chip will be added', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var input = element.find('input');

// This string contains a lot of spaces, which should be trimmed.
input.val(' Test ');
input.triggerHandler('input');

expect(ctrl.chipBuffer).toBeTruthy();

var enterEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.ENTER,
which: $mdConstant.KEY_CODE.ENTER
};

input.triggerHandler(enterEvent);

expect(scope.items).toEqual(['Apple', 'Banana', 'Orange', 'Test']);
}));

it('should not trim the input text of the input', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var input = element.find('input');

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

expect(ctrl.chipBuffer).toBeTruthy();

var enterEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.BACKSPACE,
which: $mdConstant.KEY_CODE.BACKSPACE,
preventDefault: jasmine.createSpy('preventDefault')
};

input.triggerHandler(enterEvent);

expect(enterEvent.preventDefault).not.toHaveBeenCalled();

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

input.triggerHandler(enterEvent);

expect(enterEvent.preventDefault).toHaveBeenCalledTimes(1);
}));
});


it('focuses/blurs the component when focusing/blurring the input', inject(function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
Expand Down
2 changes: 1 addition & 1 deletion src/components/chips/js/chipsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ MdChipsCtrl.prototype.inputKeydown = function(event) {
// Only append the chip and reset the chip buffer if the max chips limit isn't reached.
if (this.hasMaxChipsReached()) return;

this.appendChip(chipBuffer);
this.appendChip(chipBuffer.trim());
this.resetChipBuffer();
}
};
Expand Down
1 change: 1 addition & 0 deletions src/components/chips/js/chipsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
ng-model="$mdChipsCtrl.chipBuffer"\
ng-focus="$mdChipsCtrl.onInputFocus()"\
ng-blur="$mdChipsCtrl.onInputBlur()"\
ng-trim="false"\
ng-keydown="$mdChipsCtrl.inputKeydown($event)">';

var CHIP_DEFAULT_TEMPLATE = '\
Expand Down

0 comments on commit 2d020e1

Please sign in to comment.