Skip to content

Commit

Permalink
Improves handling of "zero" key press
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Q authored and youknowriad committed Mar 27, 2020
1 parent 5f43e25 commit c449aad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/block-editor/src/components/line-height-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { ZERO } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand All @@ -24,6 +25,20 @@ export default function LineHeightControl( { value: lineHeight, onChange } ) {
return null;
}

const handleOnKeyDown = ( event ) => {
const { keyCode } = event;

if ( keyCode === ZERO && ! isDefined ) {
/**
* Prevents the onChange callback from firing, which prevents
* the logic from assuming the change was triggered from
* an input arrow CLICK.
*/
event.preventDefault();
onChange( '0' );
}
};

const handleOnChange = ( nextValue ) => {
// Set the next value without modification if lineHeight has been defined
if ( isDefined ) {
Expand All @@ -32,6 +47,11 @@ export default function LineHeightControl( { value: lineHeight, onChange } ) {
}

// Otherwise...
/**
* The following logic handles the initial up/down arrow CLICK of the
* input element. This is so that the next values (from an undefined value state)
* are more better suited for line-height rendering.
*/
let adjustedNextValue = nextValue;

switch ( nextValue ) {
Expand All @@ -54,6 +74,7 @@ export default function LineHeightControl( { value: lineHeight, onChange } ) {
<div className="block-editor-line-height-control">
<TextControl
autoComplete="off"
onKeyDown={ handleOnKeyDown }
onChange={ handleOnChange }
label={ __( 'Line height' ) }
placeholder={ BASE_DEFAULT_VALUE }
Expand Down
4 changes: 4 additions & 0 deletions packages/keycodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ Keycode for TAB key.

Keycode for UP key.

<a name="ZERO" href="#ZERO">#</a> **ZERO**

Keycode for ZERO key.


<!-- END TOKEN(Autogenerated API docs) -->

Expand Down
4 changes: 4 additions & 0 deletions packages/keycodes/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export const COMMAND = 'meta';
* Keycode for SHIFT key.
*/
export const SHIFT = 'shift';
/**
* Keycode for ZERO key.
*/
export const ZERO = 48;

/**
* Object that contains functions that return the available modifier
Expand Down

0 comments on commit c449aad

Please sign in to comment.