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

Commit 403b64a

Browse files
authored
Merge pull request #265 from ckeditor/t/153
Fix: List items should handle Enter and Space key press when focused. Closes #153.
2 parents 07e1502 + 4bbe0b1 commit 403b64a

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/list/listitemview.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import View from '../view';
1111
import Template from '../template';
12+
import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
1213

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

35+
/**
36+
* Instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
37+
*
38+
* @readonly
39+
* @member {module:utils/keystrokehandler~KeystrokeHandler}
40+
*/
41+
this.keystrokes = new KeystrokeHandler();
42+
3443
const bind = this.bindTemplate;
3544

3645
this.template = new Template( {
@@ -92,6 +101,22 @@ export default class ListItemView extends View {
92101
*/
93102
}
94103

104+
/**
105+
* @inheritDoc
106+
*/
107+
init() {
108+
const onKeystrokePress = ( data, cancel ) => {
109+
this.fire( 'execute' );
110+
cancel();
111+
};
112+
113+
this.keystrokes.listenTo( this.element );
114+
115+
// Execute on Enter and Space key press.
116+
this.keystrokes.set( 'Enter', onKeystrokePress );
117+
this.keystrokes.set( 'Space', onKeystrokePress );
118+
}
119+
95120
/**
96121
* Focuses the list item.
97122
*/

tests/list/listitemview.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* For licensing, see LICENSE.md.
44
*/
55

6-
/* globals Event */
6+
/* global Event */
77

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

1011
describe( 'ListItemView', () => {
1112
let view;
@@ -24,6 +25,48 @@ describe( 'ListItemView', () => {
2425
it( 'creates element from template', () => {
2526
expect( view.element.classList.contains( 'ck-list__item' ) ).to.be.true;
2627
} );
28+
29+
it( 'should create #keystrokes instance', () => {
30+
expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
31+
} );
32+
} );
33+
34+
describe( 'init()', () => {
35+
it( 'starts listening for #keystrokes coming from #element', () => {
36+
const spy = sinon.spy( view.keystrokes, 'listenTo' );
37+
38+
view.init();
39+
sinon.assert.calledOnce( spy );
40+
sinon.assert.calledWithExactly( spy, view.element );
41+
} );
42+
43+
// https://github.com/ckeditor/ckeditor5-ui/issues/153
44+
it( 'triggers view#execute event when Enter or Space key is pressed', () => {
45+
const spy = sinon.spy();
46+
const evtData = {
47+
keyCode: 10,
48+
preventDefault: sinon.spy(),
49+
stopPropagation: sinon.spy()
50+
};
51+
52+
view.on( 'execute', spy );
53+
view.keystrokes.press( evtData );
54+
55+
sinon.assert.notCalled( spy );
56+
sinon.assert.notCalled( evtData.preventDefault );
57+
58+
evtData.keyCode = 13;
59+
view.keystrokes.press( evtData );
60+
61+
sinon.assert.calledOnce( spy );
62+
sinon.assert.calledOnce( evtData.preventDefault );
63+
64+
evtData.keyCode = 32;
65+
view.keystrokes.press( evtData );
66+
67+
sinon.assert.calledTwice( spy );
68+
sinon.assert.calledTwice( evtData.preventDefault );
69+
} );
2770
} );
2871

2972
describe( 'DOM bindings', () => {

0 commit comments

Comments
 (0)