Skip to content

Commit

Permalink
Handle min and max values for px
Browse files Browse the repository at this point in the history
  • Loading branch information
stacimc committed Nov 4, 2021
1 parent c6125f1 commit 0f81675
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/block-library/src/spacer/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function DimensionInput( {
onUnitChange,
unit = 'px',
value = '',
max,
min,
} ) {
const [ temporaryInput, setTemporaryInput ] = useState( null );

Expand All @@ -56,7 +58,7 @@ function DimensionInput( {
} );

const handleOnChange = ( unprocessedValue ) => {
const inputValue =
let inputValue =
unprocessedValue !== ''
? parseFloat( unprocessedValue )
: undefined;
Expand All @@ -66,6 +68,10 @@ function DimensionInput( {
return;
}
setTemporaryInput( null );

if ( isPx ) {
inputValue = Math.min( inputValue, max );
}
onChange( inputValue );
if ( inputValue === undefined ) {
onUnitChange();
Expand All @@ -79,14 +85,16 @@ function DimensionInput( {
};

const inputValue = temporaryInput !== null ? temporaryInput : value;
const min = isPx ? MIN_SPACER_HEIGHT : 0;
const minValue = isPx ? min : 0;
const maxValue = isPx ? max : undefined;

return (
<BaseControl label={ label } id={ inputId }>
<UnitControl
id={ inputId }
isResetValueOnUnitChange
min={ min }
min={ minValue }
max={ maxValue }
onBlur={ handleOnBlur }
onChange={ handleOnChange }
onUnitChange={ onUnitChange }
Expand All @@ -101,7 +109,7 @@ function DimensionInput( {

const ResizableSpacer = ( {
orientation,
maxValue,
max,
onResizeStart,
onResize,
onResizeStop,
Expand Down Expand Up @@ -135,7 +143,7 @@ const ResizableSpacer = ( {
}
} }
onResizeStop={ ( _event, _direction, elt ) => {
onResizeStop( Math.min( getNextVal( elt ), maxValue ) );
onResizeStop( Math.min( getNextVal( elt ), max ) );
setIsResizing( false );
} }
__experimentalShowTooltip={ true }
Expand Down Expand Up @@ -168,7 +176,10 @@ const SpacerEdit = ( {
const widthWithUnit = widthUnit ? `${ width }${ widthUnit }` : width;

const handleOnResizeStart = ( _event, _direction, elt ) => {
setAttributes( { heightUnit: 'px' } );
setAttributes( {
heightUnit: 'px',
widthUnit: 'px',
} );

onResizeStart( _event, _direction, elt );
};
Expand Down Expand Up @@ -213,7 +224,7 @@ const SpacerEdit = ( {
topLeft: false,
} }
orientation={ blockOrientation }
maxValue={ MAX_SPACER_WIDTH }
max={ MAX_SPACER_WIDTH }
onResizeStart={ handleOnResizeStart }
onResize={ setTemporaryWidth }
onResizeStop={ handleOnHorizontalResizeStop }
Expand All @@ -236,7 +247,7 @@ const SpacerEdit = ( {
topLeft: false,
} }
orientation={ blockOrientation }
maxValue={ MAX_SPACER_HEIGHT }
max={ MAX_SPACER_HEIGHT }
onResizeStart={ handleOnResizeStart }
onResize={ setTemporaryHeight }
onResizeStop={ handleOnVerticalResizeStop }
Expand Down Expand Up @@ -268,6 +279,8 @@ const SpacerEdit = ( {
<DimensionInput
label={ __( 'Width' ) }
value={ temporaryWidth || width }
max={ MAX_SPACER_WIDTH }
min={ MIN_SPACER_WIDTH }
unit={ widthUnit }
onChange={ ( nextWidth ) =>
setAttributes( { width: nextWidth } )
Expand All @@ -283,6 +296,8 @@ const SpacerEdit = ( {
<DimensionInput
label={ __( 'Height' ) }
value={ temporaryHeight || height }
max={ MAX_SPACER_HEIGHT }
min={ MIN_SPACER_HEIGHT }
unit={ heightUnit }
onChange={ ( nextHeight ) =>
setAttributes( { height: nextHeight } )
Expand Down

0 comments on commit 0f81675

Please sign in to comment.