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
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 Jun 17, 2018
1 parent 74d2445 commit ebd5db1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 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 + '.');
};
}
})();
18 changes: 10 additions & 8 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.
*/
MdChipsCtrl.prototype.getAdjacentChipIndex = function(index) {
Expand All @@ -462,7 +461,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 @@ -716,7 +715,9 @@ MdChipsCtrl.prototype.selectAndFocusChip = function(index) {
* Call `focus()` on the chip at `index`
*/
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 @@ -857,8 +858,7 @@ MdChipsCtrl.prototype.configureAutocomplete = function(ctrl) {
};

/**
* Whether the current chip buffer should be added on input blur or not.
* @returns {boolean}
* @returns {boolean} Whether the current chip buffer should be added on input blur or not.
*/
MdChipsCtrl.prototype.shouldAddOnBlur = function() {

Expand All @@ -869,14 +869,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;
};

MdChipsCtrl.prototype.hasFocus = function () {
Expand Down

0 comments on commit ebd5db1

Please sign in to comment.