Skip to content

Commit

Permalink
link_pseudo: Normalize keyboard interaction #932
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Oct 30, 2014
1 parent 7a9136e commit 45265ea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
22 changes: 21 additions & 1 deletion common.blocks/link/_pseudo/link_pseudo.js
Expand Up @@ -2,17 +2,37 @@
* @module link
*/

modules.define('link', function(provide, Link) {
modules.define('link', ['keyboard__codes'], function(provide, keyCodes, Link) {

/**
* @exports
* @class link
* @bem
*/
provide(Link.decl({ modName : 'pseudo', modVal : true }, /** @lends link.prototype */{
onSetMod : {
'focused' : {
'true' : function() {
this.__base.apply(this, arguments);

this.bindTo('control', 'keydown', this._onKeyDown);
},
'' : function() {
this.__base.apply(this, arguments);

this.unbindFrom('control', 'keydown', this._onKeyDown);
}
}
},

_onPointerClick : function(e) {
e.preventDefault();

this.__base.apply(this, arguments);
},

_onKeyDown : function(e) {
e.keyCode === keyCodes.ENTER && this._onPointerClick(e);
}
}));

Expand Down
14 changes: 12 additions & 2 deletions common.blocks/link/_pseudo/link_pseudo.spec.js
@@ -1,7 +1,7 @@
modules.define(
'spec',
['link', 'i-bem__dom', 'jquery', 'BEMHTML'],
function(provide, Link, BEMDOM, $, BEMHTML) {
['link', 'i-bem__dom', 'jquery', 'BEMHTML', 'sinon', 'keyboard__codes'],
function(provide, Link, BEMDOM, $, BEMHTML, sinon, keyCodes) {

describe('link_pseudo', function() {
var link;
Expand All @@ -25,6 +25,16 @@ describe('link_pseudo', function() {
link.domElem.trigger(e);
e.isDefaultPrevented().should.be.true;
});

it('should emit click on "enter" key when focused', function() {
var spy = sinon.spy();

link.setMod('focused');
link.on('click', spy);
link.domElem.trigger($.Event('keydown', { keyCode : keyCodes.ENTER }));

spy.should.have.been.calledOnce;
});
});

provide();
Expand Down
1 change: 1 addition & 0 deletions common.blocks/link/link.deps.js
Expand Up @@ -5,6 +5,7 @@
shouldDeps : [
{ mod : 'disabled' },
{ block : 'jquery', elem : 'event', mods : { type : 'pointer' } },
{ block : 'keyboard', elem : 'codes' },
'control'
]
},
Expand Down

0 comments on commit 45265ea

Please sign in to comment.