Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1009 from dwaltz/master
Browse files Browse the repository at this point in the history
Placard keyDown Accept/Cancel
  • Loading branch information
Stephen Williams committed Jan 27, 2015
2 parents 16ce486 + 518a04b commit d07b42e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions js/placard.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
this.options.revertOnCancel = (this.$accept.length>0) ? true : false;
}

this.isInput = this.$field.is('input');

this.$field.on('focus.fu.placard', $.proxy(this.show, this));
this.$field.on('keydown.fu.placard', $.proxy(this.keyComplete, this));
this.$accept.on('click.fu.placard', $.proxy(this.complete, this, 'accept'));
this.$cancel.on('click.fu.placard', function(e){ e.preventDefault(); self.complete('cancel'); });

Expand All @@ -72,6 +75,16 @@
}
},

keyComplete: function(e){
if(this.isInput && e.keyCode === 13){
this.complete('accept');
this.$field.blur();
} else if (e.keyCode === 27) {
this.complete('cancel');
this.$field.blur();
}
},

destroy: function() {
this.$element.remove();
// remove any external bindings
Expand Down
20 changes: 20 additions & 0 deletions test/placard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ define(function(require){
$placard.find('.placard-cancel').click();
});

test('Enter and exit keys should trigger appropriate response', function(){
var $placard = $(html).find('#placard1');
var $input = $placard.find('input');
var e = $.Event("keydown");

$placard.placard({
onAccept: function(){
ok(1===1, 'onAccept function called when enter keypress');
},
onCancel: function(){
ok(1===1, 'onCancel function called when exit keypress');
}
});

e.keyCode = 13;
$input.trigger(e);
e.keyCode = 27;
$input.trigger(e);
});

test('externalClickAction option should work as expected', function(){
var $placard = $(html).find('#placard1');

Expand Down

0 comments on commit d07b42e

Please sign in to comment.