Skip to content

Commit

Permalink
Cover block: stop using UnitControl's deprecated unit prop (Wor…
Browse files Browse the repository at this point in the history
…dPress#39522)

* Cover block: stop using `UnitControl` s deprecated `unit` prop

* Add `unit` to the list of `computedValue` memo dependencies

* Remove `temporaryValue`
  • Loading branch information
ciampo authored and jostnes committed Mar 23, 2022
1 parent 4fa4b1c commit 10ab550
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import namesPlugin from 'colord/plugins/names';
/**
* WordPress dependencies
*/
import { Fragment, useEffect, useRef, useState } from '@wordpress/element';
import {
Fragment,
useEffect,
useRef,
useState,
useMemo,
} from '@wordpress/element';
import {
BaseControl,
Button,
Expand All @@ -26,6 +32,7 @@ import {
__experimentalBoxControl as BoxControl,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalUnitControl as UnitControl,
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
} from '@wordpress/components';
import { compose, useInstanceId } from '@wordpress/compose';
import {
Expand Down Expand Up @@ -96,8 +103,6 @@ function CoverHeightInput( {
unit = 'px',
value = '',
} ) {
const [ temporaryInput, setTemporaryInput ] = useState( null );

const instanceId = useInstanceId( UnitControl );
const inputId = `block-cover-height-input-${ instanceId }`;
const isPx = unit === 'px';
Expand All @@ -120,20 +125,16 @@ function CoverHeightInput( {
: undefined;

if ( isNaN( inputValue ) && inputValue !== undefined ) {
setTemporaryInput( unprocessedValue );
return;
}
setTemporaryInput( null );
onChange( inputValue );
};

const handleOnBlur = () => {
if ( temporaryInput !== null ) {
setTemporaryInput( null );
}
};
const computedValue = useMemo( () => {
const [ parsedQuantity ] = parseQuantityAndUnitFromRawValue( value );
return [ parsedQuantity, unit ].join( '' );
}, [ unit, value ] );

const inputValue = temporaryInput !== null ? temporaryInput : value;
const min = isPx ? COVER_MIN_HEIGHT : 0;

return (
Expand All @@ -142,13 +143,11 @@ function CoverHeightInput( {
id={ inputId }
isResetValueOnUnitChange
min={ min }
onBlur={ handleOnBlur }
onChange={ handleOnChange }
onUnitChange={ onUnitChange }
style={ { maxWidth: 80 } }
unit={ unit }
units={ units }
value={ inputValue }
value={ computedValue }
/>
</BaseControl>
);
Expand Down

0 comments on commit 10ab550

Please sign in to comment.