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

Commit 55aa537

Browse files
committed
fix(chips): editable chip gets removed after editing (#11323)
use jQuery's `contents()` instead of `children()` since the chip content elements are text nodes and `children()` doesn't find text nodes improve double click detection when editable chips are enabled add/improve JSDoc and types rename `MdChipCtrl.isEditting` to `MdChipCtrl.isEditing` Fixes #11298. Fixes #10392. Fixes #10532. Fixes #10664. Fixes #10879.
1 parent 9d9c0b9 commit 55aa537

File tree

4 files changed

+90
-60
lines changed

4 files changed

+90
-60
lines changed

src/components/chips/js/chipController.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ angular
44

55
/**
66
* Controller for the MdChip component. Responsible for handling keyboard
7-
* events and editting the chip if needed.
7+
* events and editing the chip if needed.
88
*
99
* @param $scope
1010
* @param $element
@@ -42,7 +42,7 @@ function MdChipCtrl ($scope, $element, $mdConstant, $timeout, $mdUtil) {
4242
/**
4343
* @type {boolean}
4444
*/
45-
this.isEditting = false;
45+
this.isEditing = false;
4646

4747
/**
4848
* @type {MdChipsCtrl}
@@ -65,14 +65,14 @@ MdChipCtrl.prototype.init = function(controller) {
6565

6666
if (this.enableChipEdit) {
6767
this.$element.on('keydown', this.chipKeyDown.bind(this));
68-
this.$element.on('mousedown', this.chipMouseDown.bind(this));
68+
this.$element.on('dblclick', this.chipMouseDoubleClick.bind(this));
6969
this.getChipContent().addClass('_md-chip-content-edit-is-enabled');
7070
}
7171
};
7272

7373

7474
/**
75-
* @return {Object}
75+
* @return {Object} first element with the md-chip-content class
7676
*/
7777
MdChipCtrl.prototype.getChipContent = function() {
7878
var chipContents = this.$element[0].getElementsByClassName('md-chip-content');
@@ -81,28 +81,29 @@ MdChipCtrl.prototype.getChipContent = function() {
8181

8282

8383
/**
84-
* @return {Object}
84+
* @return {Object} first content element of the chips content element
8585
*/
8686
MdChipCtrl.prototype.getContentElement = function() {
87-
return angular.element(this.getChipContent().children()[0]);
87+
return angular.element(this.getChipContent().contents()[0]);
8888
};
8989

9090

9191
/**
92-
* @return {number}
92+
* @return {number} index of this chip
9393
*/
9494
MdChipCtrl.prototype.getChipIndex = function() {
9595
return parseInt(this.$element.attr('index'));
9696
};
9797

9898

9999
/**
100-
* Presents an input element to edit the contents of the chip.
100+
* Update the chip's contents, focus the chip if it's selected, and exit edit mode.
101+
* If the contents were updated to be empty, remove the chip and re-focus the input element.
101102
*/
102103
MdChipCtrl.prototype.goOutOfEditMode = function() {
103-
if (!this.isEditting) return;
104+
if (!this.isEditing) return;
104105

105-
this.isEditting = false;
106+
this.isEditing = false;
106107
this.$element.removeClass('_md-chip-editing');
107108
this.getChipContent()[0].contentEditable = 'false';
108109
var chipIndex = this.getChipIndex();
@@ -127,7 +128,7 @@ MdChipCtrl.prototype.goOutOfEditMode = function() {
127128

128129
/**
129130
* Given an HTML element. Selects contents of it.
130-
* @param node
131+
* @param {Element} node
131132
*/
132133
MdChipCtrl.prototype.selectNodeContents = function(node) {
133134
var range, selection;
@@ -149,7 +150,7 @@ MdChipCtrl.prototype.selectNodeContents = function(node) {
149150
* Presents an input element to edit the contents of the chip.
150151
*/
151152
MdChipCtrl.prototype.goInEditMode = function() {
152-
this.isEditting = true;
153+
this.isEditing = true;
153154
this.$element.addClass('_md-chip-editing');
154155
this.getChipContent()[0].contentEditable = 'true';
155156
this.getChipContent().on('blur', function() {
@@ -164,15 +165,15 @@ MdChipCtrl.prototype.goInEditMode = function() {
164165
* Handles the keydown event on the chip element. If enable-chip-edit attribute is
165166
* set to true, space or enter keys can trigger going into edit mode. Enter can also
166167
* trigger submitting if the chip is already being edited.
167-
* @param event
168+
* @param {KeyboardEvent} event
168169
*/
169170
MdChipCtrl.prototype.chipKeyDown = function(event) {
170-
if (!this.isEditting &&
171+
if (!this.isEditing &&
171172
(event.keyCode === this.$mdConstant.KEY_CODE.ENTER ||
172173
event.keyCode === this.$mdConstant.KEY_CODE.SPACE)) {
173174
event.preventDefault();
174175
this.goInEditMode();
175-
} else if (this.isEditting &&
176+
} else if (this.isEditing &&
176177
event.keyCode === this.$mdConstant.KEY_CODE.ENTER) {
177178
event.preventDefault();
178179
this.goOutOfEditMode();
@@ -181,12 +182,10 @@ MdChipCtrl.prototype.chipKeyDown = function(event) {
181182

182183

183184
/**
184-
* Handles the double click event
185+
* Enter edit mode if we're not already editing and the enable-chip-edit attribute is enabled.
185186
*/
186-
MdChipCtrl.prototype.chipMouseDown = function() {
187-
if(this.getChipIndex() == this.parentController.selectedChip &&
188-
this.enableChipEdit &&
189-
!this.isEditting) {
187+
MdChipCtrl.prototype.chipMouseDoubleClick = function() {
188+
if (this.enableChipEdit && !this.isEditing) {
190189
this.goInEditMode();
191190
}
192191
};

src/components/chips/js/chipDirective.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ angular
88
* @module material.components.chips
99
*
1010
* @description
11-
* `<md-chip>` is a component used within `<md-chips>` and is responsible for rendering individual
12-
* chips.
11+
* `<md-chip>` is a component used within `<md-chips>`. It is responsible for rendering an
12+
* individual chip.
1313
*
1414
*
1515
* @usage
1616
* <hljs lang="html">
17-
* <md-chip>{{$chip}}</md-chip>
17+
* <md-chips>
18+
* <md-chip>{{$chip}}</md-chip>
19+
* </md-chips>
1820
* </hljs>
1921
*
2022
*/
2123

22-
// This hint text is hidden within a chip but used by screen readers to
24+
// This hint text is visually hidden within a chip but used by screen readers to
2325
// inform the user how they can interact with a chip.
24-
2526
var DELETE_HINT_TEMPLATE = '\
2627
<span ng-if="!$mdChipsCtrl.readonly" class="md-visually-hidden">\
2728
{{$mdChipsCtrl.deleteHint}}\
@@ -32,6 +33,8 @@ var DELETE_HINT_TEMPLATE = '\
3233
*
3334
* @param $mdTheming
3435
* @param $mdUtil
36+
* @param $compile
37+
* @param $timeout
3538
* @ngInject
3639
*/
3740
function MdChip($mdTheming, $mdUtil, $compile, $timeout) {

0 commit comments

Comments
 (0)