Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #265 from ckeditor/t/153
Browse files Browse the repository at this point in the history
Fix: List items should handle Enter and Space key press when focused. Closes #153.
  • Loading branch information
oskarwrobel authored Jul 6, 2017
2 parents 07e1502 + 4bbe0b1 commit 403b64a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/list/listitemview.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import View from '../view';
import Template from '../template';
import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';

/**
* The list item view class.
Expand All @@ -31,6 +32,14 @@ export default class ListItemView extends View {
*/
this.set( 'tabindex', -1 );

/**
* Instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*
* @readonly
* @member {module:utils/keystrokehandler~KeystrokeHandler}
*/
this.keystrokes = new KeystrokeHandler();

const bind = this.bindTemplate;

this.template = new Template( {
Expand Down Expand Up @@ -92,6 +101,22 @@ export default class ListItemView extends View {
*/
}

/**
* @inheritDoc
*/
init() {
const onKeystrokePress = ( data, cancel ) => {
this.fire( 'execute' );
cancel();
};

this.keystrokes.listenTo( this.element );

// Execute on Enter and Space key press.
this.keystrokes.set( 'Enter', onKeystrokePress );
this.keystrokes.set( 'Space', onKeystrokePress );
}

/**
* Focuses the list item.
*/
Expand Down
45 changes: 44 additions & 1 deletion tests/list/listitemview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* For licensing, see LICENSE.md.
*/

/* globals Event */
/* global Event */

import ListItemView from '../../src/list/listitemview';
import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';

describe( 'ListItemView', () => {
let view;
Expand All @@ -24,6 +25,48 @@ describe( 'ListItemView', () => {
it( 'creates element from template', () => {
expect( view.element.classList.contains( 'ck-list__item' ) ).to.be.true;
} );

it( 'should create #keystrokes instance', () => {
expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
} );
} );

describe( 'init()', () => {
it( 'starts listening for #keystrokes coming from #element', () => {
const spy = sinon.spy( view.keystrokes, 'listenTo' );

view.init();
sinon.assert.calledOnce( spy );
sinon.assert.calledWithExactly( spy, view.element );
} );

// https://github.com/ckeditor/ckeditor5-ui/issues/153
it( 'triggers view#execute event when Enter or Space key is pressed', () => {
const spy = sinon.spy();
const evtData = {
keyCode: 10,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
};

view.on( 'execute', spy );
view.keystrokes.press( evtData );

sinon.assert.notCalled( spy );
sinon.assert.notCalled( evtData.preventDefault );

evtData.keyCode = 13;
view.keystrokes.press( evtData );

sinon.assert.calledOnce( spy );
sinon.assert.calledOnce( evtData.preventDefault );

evtData.keyCode = 32;
view.keystrokes.press( evtData );

sinon.assert.calledTwice( spy );
sinon.assert.calledTwice( evtData.preventDefault );
} );
} );

describe( 'DOM bindings', () => {
Expand Down

0 comments on commit 403b64a

Please sign in to comment.