Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover block: stop using UnitControl's deprecated unit prop #39522

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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