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

Commit

Permalink
fix(chips): unwanted re-focus of last chip after blur (#11305)
Browse files Browse the repository at this point in the history
this solves a regression introduced in 1.1.2 by #9650
change `ng-change` demo to use `$log` instead of `alert`
fix some lint line length warnings
JSDoc clean up

Fixes #10758
  • Loading branch information
Splaktar committed Jul 31, 2018
1 parent e303503 commit c2c7566
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
15 changes: 8 additions & 7 deletions src/components/chips/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ <h2 class="md-title">Use a custom chip template.</h2>
<br/>
<h2 class="md-title">Use the default chip template.</h2>

<md-chips ng-model="ctrl.fruitNames" readonly="ctrl.readonly" md-removable="ctrl.removable"></md-chips>
<md-chips ng-model="ctrl.fruitNames" readonly="ctrl.readonly" md-removable="ctrl.removable">
</md-chips>

<br/>
<h2 class="md-title">Use ng-change</h2>
<h2 class="md-title">Use ng-change and add-on-blur</h2>

<md-chips ng-model="ctrl.ngChangeFruitNames" ng-change="ctrl.onModelChange(ctrl.ngChangeFruitNames)"
md-removable="ctrl.removable"></md-chips>
<md-chips ng-model="ctrl.ngChangeFruitNames" md-add-on-blur="true"
ng-change="ctrl.onModelChange(ctrl.ngChangeFruitNames)"
md-removable="ctrl.removable"></md-chips>

<br/>
<h2 class="md-title">Make chips editable.</h2>

<md-chips ng-model="ctrl.editableFruitNames" readonly="ctrl.readonly" md-removable="ctrl.removable"
md-enable-chip-edit="true"></md-chips>
<md-chips ng-model="ctrl.editableFruitNames" readonly="ctrl.readonly"
md-removable="ctrl.removable" md-enable-chip-edit="true"></md-chips>

<br/>

<h2 class="md-title">Use Placeholders and override hint texts.</h2>

<md-chips
Expand Down
3 changes: 1 addition & 2 deletions src/components/chips/demoBasicUsage/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<code>md-chips</code> component. In order to achieve this, the behavior has changed to also select
and highlight the newly appended chip for <code>300ms</code> before re-focusing the text input.
</p>

<p>
Please see the <a href="api/directive/mdChips">documentation</a> for more information and for
the new <code>md-chip-append-delay</code> option which allows you to customize this delay.
</p>
</p>
4 changes: 2 additions & 2 deletions src/components/chips/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}])
.controller('BasicDemoCtrl', DemoCtrl);

function DemoCtrl ($timeout, $q) {
function DemoCtrl ($timeout, $q, $log) {
var self = this;

self.readonly = false;
Expand Down Expand Up @@ -42,7 +42,7 @@
};

self.onModelChange = function(newModel) {
alert('The model has changed');
$log.log('The model has changed to ' + newModel + '.');
};
}
})();
15 changes: 9 additions & 6 deletions src/components/chips/js/chipsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ MdChipsCtrl.prototype.resetSelectedChip = function() {
* determined as the next chip in the list, unless the target chip is the
* last in the list, then it is the chip immediately preceding the target. If
* there is only one item in the list, -1 is returned (select none).
* The number returned is the index to select AFTER the target has been
* removed.
* The number returned is the index to select AFTER the target has been removed.
* If the current chip is not selected, then -1 is returned to select none.
* @param {number} index
* @returns {number}
Expand All @@ -464,7 +463,7 @@ MdChipsCtrl.prototype.getAdjacentChipIndex = function(index) {
* @param {string} newChip chip buffer contents that will be used to create the new chip
*/
MdChipsCtrl.prototype.appendChip = function(newChip) {
this.shouldFocusLastChip = true;
this.shouldFocusLastChip = !this.addOnBlur;
if (this.useTransformChip && this.transformChip) {
var transformedChip = this.transformChip({'$chip': newChip});

Expand Down Expand Up @@ -733,7 +732,9 @@ MdChipsCtrl.prototype.selectAndFocusChip = function(index) {
* @param {number} index location of chip to focus
*/
MdChipsCtrl.prototype.focusChip = function(index) {
var chipContent = this.$element[0].querySelector('md-chip[index="' + index + '"] .md-chip-content');
var chipContent = this.$element[0].querySelector(
'md-chip[index="' + index + '"] .md-chip-content'
);

this.ariaTabIndex = index;

Expand Down Expand Up @@ -888,14 +889,16 @@ MdChipsCtrl.prototype.shouldAddOnBlur = function() {
// If the model value is empty and required is set on the element, then the model will be invalid.
// In that case, we still want to allow adding the chip. The main (but not only) case we want
// to disallow is adding a chip on blur when md-max-chips validation fails.
var isModelValid = this.ngModelCtrl.$isEmpty(this.ngModelCtrl.$modelValue) || this.ngModelCtrl.$valid;
var isModelValid = this.ngModelCtrl.$isEmpty(this.ngModelCtrl.$modelValue) ||
this.ngModelCtrl.$valid;
var isAutocompleteShowing = this.autocompleteCtrl && !this.autocompleteCtrl.hidden;

if (this.userInputNgModelCtrl) {
isModelValid = isModelValid && this.userInputNgModelCtrl.$valid;
}

return this.addOnBlur && !this.requireMatch && chipBuffer && isModelValid && !isAutocompleteShowing;
return this.addOnBlur && !this.requireMatch && chipBuffer && isModelValid &&
!isAutocompleteShowing;
};

/**
Expand Down

0 comments on commit c2c7566

Please sign in to comment.