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

Commit 97455f5

Browse files
Splaktarjelbourn
authored andcommittedJul 11, 2018
fix(chips): editing chips works again (#11364)
- don't override defaults for scope variables which aren't defined - properly mark scope variables as optional in chips directive - the default inputAriaLabel, deleteHint, etc should not be rendered - deleteHint in chip content broke the ability to edit chips - move deleteHint out of chip content and only aria-label on md-chip - handle edge case in editing when the user clears the content and then enters new content Fixes #11322. Relates to #11323.
1 parent eb10b56 commit 97455f5

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed
 

Diff for: ‎src/components/chips/js/chipController.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@ MdChipCtrl.prototype.getChipContent = function() {
8181

8282

8383
/**
84-
* @return {Object} first content element of the chips content element
84+
* When editing the chip, if the user modifies the existing contents, we'll get a span back and
85+
* need to ignore text elements as they only contain blank space.
86+
* `children()` ignores text elements.
87+
*
88+
* When editing the chip, if the user deletes the contents and then enters some new content
89+
* we'll only get a text element back.
90+
* @return {Object} jQuery object representing the content element of the chip
8591
*/
8692
MdChipCtrl.prototype.getContentElement = function() {
87-
return angular.element(this.getChipContent().contents()[0]);
93+
var contentElement = angular.element(this.getChipContent().children()[0]);
94+
if (!contentElement || contentElement.length === 0) {
95+
contentElement = angular.element(this.getChipContent().contents()[0]);
96+
}
97+
return contentElement;
8898
};
8999

90100

@@ -101,7 +111,9 @@ MdChipCtrl.prototype.getChipIndex = function() {
101111
* If the contents were updated to be empty, remove the chip and re-focus the input element.
102112
*/
103113
MdChipCtrl.prototype.goOutOfEditMode = function() {
104-
if (!this.isEditing) return;
114+
if (!this.isEditing) {
115+
return;
116+
}
105117

106118
this.isEditing = false;
107119
this.$element.removeClass('_md-chip-editing');
@@ -110,10 +122,7 @@ MdChipCtrl.prototype.goOutOfEditMode = function() {
110122

111123
var content = this.getContentElement().text();
112124
if (content) {
113-
this.parentController.updateChipContents(
114-
chipIndex,
115-
this.getContentElement().text()
116-
);
125+
this.parentController.updateChipContents(chipIndex, content);
117126

118127
this.$mdUtil.nextTick(function() {
119128
if (this.parentController.selectedChip === chipIndex) {
@@ -170,11 +179,10 @@ MdChipCtrl.prototype.goInEditMode = function() {
170179
MdChipCtrl.prototype.chipKeyDown = function(event) {
171180
if (!this.isEditing &&
172181
(event.keyCode === this.$mdConstant.KEY_CODE.ENTER ||
173-
event.keyCode === this.$mdConstant.KEY_CODE.SPACE)) {
182+
event.keyCode === this.$mdConstant.KEY_CODE.SPACE)) {
174183
event.preventDefault();
175184
this.goInEditMode();
176-
} else if (this.isEditing &&
177-
event.keyCode === this.$mdConstant.KEY_CODE.ENTER) {
185+
} else if (this.isEditing && event.keyCode === this.$mdConstant.KEY_CODE.ENTER) {
178186
event.preventDefault();
179187
this.goOutOfEditMode();
180188
}

Diff for: ‎src/components/chips/js/chipDirective.js

-12
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ angular
2121
*
2222
*/
2323

24-
// This hint text is visually hidden within a chip but used by screen readers to
25-
// inform the user how they can interact with a chip.
26-
var DELETE_HINT_TEMPLATE = '\
27-
<span ng-if="!$mdChipsCtrl.readonly" class="md-visually-hidden">\
28-
{{$mdChipsCtrl.deleteHint}}\
29-
</span>';
30-
3124
/**
3225
* MDChip Directive Definition
3326
*
@@ -38,8 +31,6 @@ var DELETE_HINT_TEMPLATE = '\
3831
* @ngInject
3932
*/
4033
function MdChip($mdTheming, $mdUtil, $compile, $timeout) {
41-
var deleteHintTemplate = $mdUtil.processTemplate(DELETE_HINT_TEMPLATE);
42-
4334
return {
4435
restrict: 'E',
4536
require: ['^?mdChips', 'mdChip'],
@@ -57,9 +48,6 @@ function MdChip($mdTheming, $mdUtil, $compile, $timeout) {
5748
if (chipsController) {
5849
chipController.init(chipsController);
5950

60-
// Append our delete hint to the div.md-chip-content (which does not exist at compile time)
61-
chipContentElement.append($compile(deleteHintTemplate)(scope));
62-
6351
// When a chip is blurred, make sure to unset (or reset) the selected chip so that tabbing
6452
// through elements works properly
6553
chipContentElement.on('blur', function() {

Diff for: ‎src/components/chips/js/chipsDirective.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
* @param {expression=} md-on-remove An expression which will be called when a chip has been
134134
* removed with `$chip`, `$index`, and `$event` available as parameters.
135135
* @param {expression=} md-on-select An expression which will be called when a chip is selected.
136-
* @param {boolean} md-require-match If true, and the chips template contains an autocomplete,
136+
* @param {boolean=} md-require-match If true, and the chips template contains an autocomplete,
137137
* only allow selection of pre-defined chips (i.e. you cannot add new ones).
138138
* @param {string=} input-aria-label A string read by screen readers to identify the input.
139139
* @param {string=} container-hint A string read by screen readers informing users of how to
@@ -200,7 +200,7 @@
200200
{{$mdChipsCtrl.containerHint}}\
201201
</span>\
202202
<md-chip ng-repeat="$chip in $mdChipsCtrl.items"\
203-
index="{{$index}}"\
203+
index="{{$index}}" aria-label="{{$mdChipsCtrl.deleteHint}}"\
204204
ng-class="{\'md-focused\': $mdChipsCtrl.selectedChip == $index, \'md-readonly\': !$mdChipsCtrl.ngModelCtrl || $mdChipsCtrl.readonly}">\
205205
<div class="md-chip-content"\
206206
tabindex="{{$mdChipsCtrl.ariaTabIndex == $index ? 0 : -1}}"\
@@ -271,24 +271,24 @@
271271
bindToController: true,
272272
compile: compile,
273273
scope: {
274-
readonly: '=readonly',
275-
removable: '=mdRemovable',
276-
placeholder: '@',
277-
secondaryPlaceholder: '@',
278-
maxChips: '@mdMaxChips',
274+
readonly: '=?readonly',
275+
removable: '=?mdRemovable',
276+
placeholder: '@?',
277+
secondaryPlaceholder: '@?',
278+
maxChips: '@?mdMaxChips',
279279
transformChip: '&mdTransformChip',
280-
onAppend: '&mdOnAppend',
281-
onAdd: '&mdOnAdd',
282-
onRemove: '&mdOnRemove',
283-
onSelect: '&mdOnSelect',
284-
inputAriaLabel: '@',
285-
containerHint: '@',
286-
deleteHint: '@',
287-
deleteButtonLabel: '@',
280+
onAppend: '&?mdOnAppend',
281+
onAdd: '&?mdOnAdd',
282+
onRemove: '&?mdOnRemove',
283+
onSelect: '&?mdOnSelect',
284+
inputAriaLabel: '@?',
285+
containerHint: '@?',
286+
deleteHint: '@?',
287+
deleteButtonLabel: '@?',
288288
separatorKeys: '=?mdSeparatorKeys',
289289
requireMatch: '=?mdRequireMatch',
290290
chipAppendDelayString: '@?mdChipAppendDelay',
291-
ngChange: '&'
291+
ngChange: '&?'
292292
}
293293
};
294294

Diff for: ‎src/core/util/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in
705705

706706
/**
707707
* Processes a template and replaces the start/end symbols if the application has
708-
* overriden them.
708+
* overridden them.
709709
*
710710
* @param template The template to process whose start/end tags may be replaced.
711711
* @returns {*}

0 commit comments

Comments
 (0)
This repository has been archived.