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

Commit

Permalink
fix(chips): editing chips works again (#11364)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Splaktar authored and jelbourn committed Jul 11, 2018
1 parent eb10b56 commit 97455f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
28 changes: 18 additions & 10 deletions src/components/chips/js/chipController.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,20 @@ MdChipCtrl.prototype.getChipContent = function() {


/**
* @return {Object} first content element of the chips content element
* When editing the chip, if the user modifies the existing contents, we'll get a span back and
* need to ignore text elements as they only contain blank space.
* `children()` ignores text elements.
*
* When editing the chip, if the user deletes the contents and then enters some new content
* we'll only get a text element back.
* @return {Object} jQuery object representing the content element of the chip
*/
MdChipCtrl.prototype.getContentElement = function() {
return angular.element(this.getChipContent().contents()[0]);
var contentElement = angular.element(this.getChipContent().children()[0]);
if (!contentElement || contentElement.length === 0) {
contentElement = angular.element(this.getChipContent().contents()[0]);
}
return contentElement;
};


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

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

var content = this.getContentElement().text();
if (content) {
this.parentController.updateChipContents(
chipIndex,
this.getContentElement().text()
);
this.parentController.updateChipContents(chipIndex, content);

this.$mdUtil.nextTick(function() {
if (this.parentController.selectedChip === chipIndex) {
Expand Down Expand Up @@ -170,11 +179,10 @@ MdChipCtrl.prototype.goInEditMode = function() {
MdChipCtrl.prototype.chipKeyDown = function(event) {
if (!this.isEditing &&
(event.keyCode === this.$mdConstant.KEY_CODE.ENTER ||
event.keyCode === this.$mdConstant.KEY_CODE.SPACE)) {
event.keyCode === this.$mdConstant.KEY_CODE.SPACE)) {
event.preventDefault();
this.goInEditMode();
} else if (this.isEditing &&
event.keyCode === this.$mdConstant.KEY_CODE.ENTER) {
} else if (this.isEditing && event.keyCode === this.$mdConstant.KEY_CODE.ENTER) {
event.preventDefault();
this.goOutOfEditMode();
}
Expand Down
12 changes: 0 additions & 12 deletions src/components/chips/js/chipDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ angular
*
*/

// This hint text is visually hidden within a chip but used by screen readers to
// inform the user how they can interact with a chip.
var DELETE_HINT_TEMPLATE = '\
<span ng-if="!$mdChipsCtrl.readonly" class="md-visually-hidden">\
{{$mdChipsCtrl.deleteHint}}\
</span>';

/**
* MDChip Directive Definition
*
Expand All @@ -38,8 +31,6 @@ var DELETE_HINT_TEMPLATE = '\
* @ngInject
*/
function MdChip($mdTheming, $mdUtil, $compile, $timeout) {
var deleteHintTemplate = $mdUtil.processTemplate(DELETE_HINT_TEMPLATE);

return {
restrict: 'E',
require: ['^?mdChips', 'mdChip'],
Expand All @@ -57,9 +48,6 @@ function MdChip($mdTheming, $mdUtil, $compile, $timeout) {
if (chipsController) {
chipController.init(chipsController);

// Append our delete hint to the div.md-chip-content (which does not exist at compile time)
chipContentElement.append($compile(deleteHintTemplate)(scope));

// When a chip is blurred, make sure to unset (or reset) the selected chip so that tabbing
// through elements works properly
chipContentElement.on('blur', function() {
Expand Down
32 changes: 16 additions & 16 deletions src/components/chips/js/chipsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
* @param {expression=} md-on-remove An expression which will be called when a chip has been
* removed with `$chip`, `$index`, and `$event` available as parameters.
* @param {expression=} md-on-select An expression which will be called when a chip is selected.
* @param {boolean} md-require-match If true, and the chips template contains an autocomplete,
* @param {boolean=} md-require-match If true, and the chips template contains an autocomplete,
* only allow selection of pre-defined chips (i.e. you cannot add new ones).
* @param {string=} input-aria-label A string read by screen readers to identify the input.
* @param {string=} container-hint A string read by screen readers informing users of how to
Expand Down Expand Up @@ -200,7 +200,7 @@
{{$mdChipsCtrl.containerHint}}\
</span>\
<md-chip ng-repeat="$chip in $mdChipsCtrl.items"\
index="{{$index}}"\
index="{{$index}}" aria-label="{{$mdChipsCtrl.deleteHint}}"\
ng-class="{\'md-focused\': $mdChipsCtrl.selectedChip == $index, \'md-readonly\': !$mdChipsCtrl.ngModelCtrl || $mdChipsCtrl.readonly}">\
<div class="md-chip-content"\
tabindex="{{$mdChipsCtrl.ariaTabIndex == $index ? 0 : -1}}"\
Expand Down Expand Up @@ -271,24 +271,24 @@
bindToController: true,
compile: compile,
scope: {
readonly: '=readonly',
removable: '=mdRemovable',
placeholder: '@',
secondaryPlaceholder: '@',
maxChips: '@mdMaxChips',
readonly: '=?readonly',
removable: '=?mdRemovable',
placeholder: '@?',
secondaryPlaceholder: '@?',
maxChips: '@?mdMaxChips',
transformChip: '&mdTransformChip',
onAppend: '&mdOnAppend',
onAdd: '&mdOnAdd',
onRemove: '&mdOnRemove',
onSelect: '&mdOnSelect',
inputAriaLabel: '@',
containerHint: '@',
deleteHint: '@',
deleteButtonLabel: '@',
onAppend: '&?mdOnAppend',
onAdd: '&?mdOnAdd',
onRemove: '&?mdOnRemove',
onSelect: '&?mdOnSelect',
inputAriaLabel: '@?',
containerHint: '@?',
deleteHint: '@?',
deleteButtonLabel: '@?',
separatorKeys: '=?mdSeparatorKeys',
requireMatch: '=?mdRequireMatch',
chipAppendDelayString: '@?mdChipAppendDelay',
ngChange: '&'
ngChange: '&?'
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in

/**
* Processes a template and replaces the start/end symbols if the application has
* overriden them.
* overridden them.
*
* @param template The template to process whose start/end tags may be replaced.
* @returns {*}
Expand Down

0 comments on commit 97455f5

Please sign in to comment.