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

Commit

Permalink
feat(chips): adds keyboard shortcuts to chips component
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Messerle committed Apr 15, 2015
1 parent ab26480 commit c62d4df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
54 changes: 46 additions & 8 deletions src/components/chips/js/chipsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
* @param $element
* @constructor
*/
function MdChipsCtrl ($scope, $mdConstant, $log, $element) {
function MdChipsCtrl ($scope, $mdConstant, $log, $element, $timeout) {
/** @type {$timeout} **/
this.$timeout = $timeout;

/** @type {Object} */
this.$mdConstant = $mdConstant;

Expand Down Expand Up @@ -100,6 +103,37 @@
}
};

/**
* Handles the keydown event on the chip elements: backspace removes the selected chip, arrow
* keys switch which chips is active
* @param event
*/
MdChipsCtrl.prototype.chipKeydown = function (event) {
switch (event.keyCode) {
case this.$mdConstant.KEY_CODE.BACKSPACE:
case this.$mdConstant.KEY_CODE.DELETE:
if (this.selectedChip < 0) return;
event.preventDefault();
this.removeAndSelectAdjacentChip(this.selectedChip);
break;
case this.$mdConstant.KEY_CODE.LEFT_ARROW:
if (this.selectedChip < 0) this.selectedChip = this.items.length;
event.preventDefault();
if (this.items.length) this.selectAndFocusChipSafe(this.selectedChip - 1);
break;
case this.$mdConstant.KEY_CODE.RIGHT_ARROW:
event.preventDefault();
this.selectAndFocusChipSafe(this.selectedChip + 1);
break;
case this.$mdConstant.KEY_CODE.ESCAPE:
case this.$mdConstant.KEY_CODE.TAB:
if (this.selectedChip < 0) return;
event.preventDefault();
this.selectAndFocusChipSafe(this.selectedChip);
break;
}
};

/**
* Get the input's placeholder - uses `placeholder` when list is empty and `secondary-placeholder`
* when the list is non-empty. If `secondary-placeholder` is not provided, `placeholder` is used
Expand All @@ -119,7 +153,7 @@
MdChipsCtrl.prototype.removeAndSelectAdjacentChip = function(index) {
var selIndex = this.getAdjacentChipIndex(index);
this.removeChip(index);
this.selectAndFocusChip(selIndex);
this.$timeout(function () { this.selectAndFocusChipSafe(selIndex); }.bind(this));
};

/**
Expand Down Expand Up @@ -206,12 +240,17 @@
this.items.splice(index, 1);
};

MdChipsCtrl.prototype.removeChipAndFocusInput = function (index) {
this.removeChip(index);
this.onFocus();
};
/**
* Selects the chip at `index`,
* @param index
*/
MdChipsCtrl.prototype.selectChipSafe = function(index) {
MdChipsCtrl.prototype.selectAndFocusChipSafe = function(index) {
if (!this.items.length) return this.selectChip(-1);
if (index === this.items.length) return this.onFocus();
index = Math.max(index, 0);
index = Math.min(index, this.items.length - 1);
this.selectChip(index);
Expand Down Expand Up @@ -266,6 +305,7 @@
MdChipsCtrl.prototype.onFocus = function () {
var input = this.$element[0].querySelectorAll('input')[0];
input && input.focus();
this.resetSelectedChip();
};

/**
Expand All @@ -285,11 +325,9 @@
// Bind to keydown and focus events of input
var scope = this.$scope;
var ctrl = this;
inputElement.on('keydown', function(event) {
scope.$apply(function() {
ctrl.inputKeydown(event);
});
});
inputElement
.on('keydown', function(event) { scope.$apply(function() { ctrl.inputKeydown(event); }); })
.on('focus', function () { scope.$apply(function () { ctrl.selectedChip = null; }); });

This comment has been minimized.

Copy link
@gkalpak

gkalpak Apr 21, 2015

Member

Why not set selectedChip to -1 ? Is there a special meaning to null ?

This comment has been minimized.

Copy link
@robertmesserle

robertmesserle via email Apr 21, 2015

Contributor
};

MdChipsCtrl.prototype.configureAutocomplete = function(ctrl) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/chips/js/chipsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<md-chips-wrap\
ng-if="!$mdChipsCtrl.readonly || $mdChipsCtrl.items.length > 0"\
ng-focus="$mdChipsCtrl.onFocus()"\
ng-keydown="$mdChipsCtrl.chipKeydown($event)"\
tabindex="0"\
class="md-chips">\
<md-chip ng-repeat="$chip in $mdChipsCtrl.items"\
Expand Down Expand Up @@ -126,7 +127,7 @@
<button\
class="md-chip-remove"\
ng-if="!$mdChipsCtrl.readonly"\
ng-click="$mdChipsCtrl.removeChip($$replacedScope.$index)"\
ng-click="$mdChipsCtrl.removeChipAndFocusInput($$replacedScope.$index)"\
tabindex="-1">\
<md-icon md-svg-icon="close"></md-icon>\
<span class="md-visually-hidden">\
Expand Down

0 comments on commit c62d4df

Please sign in to comment.