Skip to content

Commit

Permalink
Writing flow: Fix vertial focus trap on certain input types
Browse files Browse the repository at this point in the history
Add test

Fix e2e test

enable left/right native event on input number field

Remove changes

test

test
  • Loading branch information
t-hamano committed May 21, 2023
1 parent d42095c commit d7f6189
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
15 changes: 15 additions & 0 deletions packages/block-editor/src/components/writing-flow/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe( 'isNavigationCandidate', () => {
elements.inputCheckbox = document.createElement( 'input' );
elements.inputCheckbox.setAttribute( 'type', 'checkbox' );

elements.inputNumber = document.createElement( 'input' );
elements.inputNumber.setAttribute( 'type', 'number' );

elements.contentEditable = document.createElement( 'p' );
elements.contentEditable.contentEditable = true;
} );
Expand Down Expand Up @@ -47,6 +50,18 @@ describe( 'isNavigationCandidate', () => {
} );
} );

it( 'should return false if vertically navigating inputs with vertial support like number', () => {
[ UP, DOWN ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
elements.inputNumber,
keyCode,
false
);

expect( result ).toBe( false );
} );
} );

it( 'should return false if horizontally navigating input', () => {
[ LEFT, RIGHT ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
Expand Down
21 changes: 17 additions & 4 deletions packages/block-editor/src/components/writing-flow/use-arrow-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,40 @@ import { store as blockEditorStore } from '../../store';
*/
export function isNavigationCandidate( element, keyCode, hasModifier ) {
const isVertical = keyCode === UP || keyCode === DOWN;
const { tagName } = element;
const elementType = element.getAttribute( 'type' );

// Currently, all elements support unmodified vertical navigation.
// Native inputs should not navigate vertically, unless they are simple types that don't need up/down arrow keys.
if ( isVertical && ! hasModifier ) {
if ( tagName === 'INPUT' ) {
const vertiaclInputTypes = [
'date',
'datetime-local',
'month',
'number',
'range',
'time',
'week',
];
return ! vertiaclInputTypes.includes( elementType );
}
return true;
}

const { tagName } = element;

// Native inputs should not navigate horizontally, unless they are simple types that don't need left/right arrow keys.
if ( tagName === 'INPUT' ) {
const simpleInputTypes = [
'button',
'checkbox',
'number',
'color',
'file',
'image',
'radio',
'reset',
'submit',
];
return simpleInputTypes.includes( element.getAttribute( 'type' ) );
return simpleInputTypes.includes( elementType );
}

// Native textareas should not navigate horizontally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ test.describe( 'Toolbar roving tabindex', () => {
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/table' } );
await page.keyboard.press( 'ArrowLeft' );
await ToolbarRovingTabindexUtils.testBlockToolbarKeyboardNavigation(
'Block: Table',
'Table'
Expand Down

0 comments on commit d7f6189

Please sign in to comment.