Skip to content

Commit

Permalink
Mobile - AztecView - Adds code values to add support to the recent ch…
Browse files Browse the repository at this point in the history
…anges on the web editor using KeyboardEvent.code since codeKey is now deprecated. (#43521)
  • Loading branch information
Gerardo Pacheco authored Aug 24, 2022
1 parent e3e79e1 commit 1b21f86
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/react-native-aztec/src/AztecView.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ import {
* WordPress dependencies
*/
import { Component, createRef } from '@wordpress/element';
import { ENTER, BACKSPACE } from '@wordpress/keycodes';
import {
BACKSPACE,
DELETE,
DOWN,
ENTER,
ESCAPE,
LEFT,
RIGHT,
SPACE,
UP,
} from '@wordpress/keycodes';

/**
* Internal dependencies
Expand All @@ -21,6 +31,19 @@ import * as AztecInputState from './AztecInputState';

const AztecManager = UIManager.getViewManagerConfig( 'RCTAztecView' );

// Used to match KeyboardEvent.code values (from the Web API) with native keycodes.
const KEYCODES = {
[ BACKSPACE ]: 'Backspace',
[ DELETE ]: 'Delete',
[ DOWN ]: 'ArrowDown',
[ ENTER ]: 'Enter',
[ ESCAPE ]: 'Escape',
[ LEFT ]: 'ArrowLeft',
[ RIGHT ]: 'ArrowRight',
[ SPACE ]: 'Space',
[ UP ]: 'ArrowUp',
};

class AztecView extends Component {
constructor() {
super( ...arguments );
Expand Down Expand Up @@ -75,7 +98,7 @@ class AztecView extends Component {

const { onKeyDown } = this.props;

const newEvent = { ...event, keyCode: ENTER };
const newEvent = { ...event, keyCode: ENTER, code: KEYCODES[ ENTER ] };
onKeyDown( newEvent );
}

Expand All @@ -89,6 +112,7 @@ class AztecView extends Component {
const newEvent = {
...event,
keyCode: BACKSPACE,
code: KEYCODES[ BACKSPACE ],
preventDefault: () => {},
};
onKeyDown( newEvent );
Expand All @@ -100,9 +124,13 @@ class AztecView extends Component {
}

const { onKeyDown } = this.props;
const { keyCode } = event.nativeEvent;
const newEvent = {
...event,
keyCode: event.nativeEvent.keyCode,
keyCode,
...( KEYCODES[ keyCode ] && {
code: KEYCODES[ keyCode ],
} ),
preventDefault: () => {},
};
onKeyDown( newEvent );
Expand Down

0 comments on commit 1b21f86

Please sign in to comment.