浏览代码

FocusCycler#focusNext/Previous should focus first/last focusable when no view is currently focused.

Aleksander Nowodzinski 8 年之前
父节点
当前提交
533023f389
共有 2 个文件被更改,包括 12 次插入6 次删除
  1. 8 2
      packages/ckeditor5-ui/src/focuscycler.js
  2. 4 4
      packages/ckeditor5-ui/tests/focuscycler.js

+ 8 - 2
packages/ckeditor5-ui/src/focuscycler.js

@@ -241,13 +241,19 @@ export default class FocusCycler {
 	 */
 	 */
 	_getFocusableItem( step ) {
 	_getFocusableItem( step ) {
 		// Cache for speed.
 		// Cache for speed.
-		const current = this.current;
+		let current = this.current;
 		const collectionLength = this.focusables.length;
 		const collectionLength = this.focusables.length;
 
 
-		if ( !collectionLength || current === null ) {
+		if ( !collectionLength ) {
 			return null;
 			return null;
 		}
 		}
 
 
+		// Start from the beginning if no view is focused.
+		// https://github.com/ckeditor/ckeditor5-ui/issues/206
+		if ( current === null ) {
+			return this[ step === 1 ? 'first' : 'last' ];
+		}
+
 		// Cycle in both directions.
 		// Cycle in both directions.
 		let index = ( current + collectionLength + step ) % collectionLength;
 		let index = ( current + collectionLength + step ) % collectionLength;
 
 

+ 4 - 4
packages/ckeditor5-ui/tests/focuscycler.js

@@ -112,10 +112,10 @@ describe( 'FocusCycler', () => {
 			expect( cycler.next ).to.equal( focusables.get( 2 ) );
 			expect( cycler.next ).to.equal( focusables.get( 2 ) );
 		} );
 		} );
 
 
-		it( 'returns null when no view is focused', () => {
+		it( 'focuses the first focusable view when no view is focused', () => {
 			focusTracker.focusedElement = null;
 			focusTracker.focusedElement = null;
 
 
-			expect( cycler.next ).to.be.null;
+			expect( cycler.next ).to.equal( focusables.get( 1 ) );
 		} );
 		} );
 
 
 		it( 'returns null when no items', () => {
 		it( 'returns null when no items', () => {
@@ -162,10 +162,10 @@ describe( 'FocusCycler', () => {
 			expect( cycler.previous ).to.equal( focusables.get( 2 ) );
 			expect( cycler.previous ).to.equal( focusables.get( 2 ) );
 		} );
 		} );
 
 
-		it( 'returns null when no view is focused', () => {
+		it( 'focuses the last focusable view when no view is focused', () => {
 			focusTracker.focusedElement = null;
 			focusTracker.focusedElement = null;
 
 
-			expect( cycler.previous ).to.be.null;
+			expect( cycler.previous ).to.equal( focusables.get( 3 ) );
 		} );
 		} );
 
 
 		it( 'returns null when no items', () => {
 		it( 'returns null when no items', () => {