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

Box Control: fix issue with negative values #60984

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
__experimentalView as View,
} from '@wordpress/components';
import { Icon, positionCenter, stretchWide } from '@wordpress/icons';
import { useCallback, Platform } from '@wordpress/element';
import { useCallback, useState, Platform } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -248,6 +248,10 @@ export default function DimensionsPanel( {
],
} );

//Minimum Margin Value
const minimumMargin = -Infinity;
const [ minMarginValue, setMinMarginValue ] = useState( minimumMargin );

// Content Size
const showContentSizeControl =
useHasContentSize( settings ) && includeLayoutControls;
Expand Down Expand Up @@ -435,6 +439,17 @@ export default function DimensionsPanel( {

const onMouseLeaveControls = () => onVisualize( false );

const inputProps = {
min: minMarginValue,
onDragStart: () => {
//Reset to 0 in case the value was negative.
setMinMarginValue( 0 );
},
onDragEnd: () => {
setMinMarginValue( minimumMargin );
},
};

return (
<Wrapper
resetAllFilter={ resetAllFilter }
Expand Down Expand Up @@ -561,7 +576,7 @@ export default function DimensionsPanel( {
<BoxControl
values={ marginValues }
onChange={ setMarginValues }
min={ -Infinity }
inputProps={ inputProps }
label={ __( 'Margin' ) }
sides={ marginSides }
units={ units }
Expand Down
1 change: 0 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
### Enhancements

- `InputControl`: Add a password visibility toggle story ([#60898](https://github.com/WordPress/gutenberg/pull/60898)).
- `BoxControl`: Allow negative values for margin controls ([#60347](https://github.com/WordPress/gutenberg/pull/60347)).
- `View`: Fix prop types ([#60919](https://github.com/WordPress/gutenberg/pull/60919)).

### Bug Fix
Expand Down
11 changes: 6 additions & 5 deletions packages/components/src/box-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import type {
BoxControlValue,
} from './types';

const defaultInputProps = {
min: 0,
};

const noop = () => {};

function useUniqueId( idProp?: string ) {
Expand Down Expand Up @@ -70,6 +74,7 @@ function useUniqueId( idProp?: string ) {
function BoxControl( {
__next40pxDefaultSize = false,
id: idProp,
inputProps = defaultInputProps,
onChange = noop,
label = __( 'Box Control' ),
values: valuesProp,
Expand All @@ -80,7 +85,6 @@ function BoxControl( {
resetValues = DEFAULT_VALUES,
onMouseOver,
onMouseOut,
...inputProps
}: BoxControlProps ) {
const [ values, setValues ] = useControlledState( valuesProp, {
fallback: DEFAULT_VALUES,
Expand Down Expand Up @@ -136,11 +140,8 @@ function BoxControl( {
setIsDirty( false );
};

const min = 'min' in inputProps ? inputProps.min : 0;
const newInputProps = { ...inputProps, min };

const inputControlProps = {
newInputProps,
...inputProps,
onChange: handleOnChange,
onFocus: handleOnFocus,
isLinked,
Expand Down
20 changes: 0 additions & 20 deletions packages/components/src/box-control/input-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
Expand All @@ -29,8 +28,6 @@ export default function BoxInputControls( {
sides,
...props
}: BoxControlInputControlProps ) {
const minimumCustomValue = props.min;
const [ minValue, setMinValue ] = useState( minimumCustomValue );
const generatedId = useInstanceId( BoxInputControls, 'box-control-input' );

const createHandleOnFocus =
Expand Down Expand Up @@ -103,9 +100,6 @@ export default function BoxInputControls( {
? parsedUnit
: selectedUnits[ side ];

const isNegativeValue =
parsedQuantity !== undefined && parsedQuantity < 0;

const inputId = [ generatedId, side ].join( '-' );

return (
Expand All @@ -121,7 +115,6 @@ export default function BoxInputControls( {
value={ [ parsedQuantity, computedUnit ].join(
''
) }
min={ minValue }
onChange={ ( nextValue, extra ) =>
handleOnValueChange(
side,
Expand All @@ -133,19 +126,6 @@ export default function BoxInputControls( {
side
) }
onFocus={ createHandleOnFocus( side ) }
onDragStart={ () => {
if ( isNegativeValue ) {
setMinValue( 0 );
}
} }
onDrag={ () => {
if ( isNegativeValue ) {
setMinValue( 0 );
}
} }
onDragEnd={ () => {
setMinValue( minimumCustomValue );
} }
label={ LABELS[ side ] }
hideLabelFromVision
/>
Expand Down
22 changes: 22 additions & 0 deletions packages/components/src/box-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ describe( 'BoxControl', () => {
expect( input ).toHaveValue( '50' );
expect( screen.getByRole( 'slider' ) ).toHaveValue( '50' );
} );

it( 'should render the number input with a default min value of 0', () => {
render( <BoxControl onChange={ () => {} } /> );

const input = screen.getByRole( 'textbox', { name: 'All sides' } );

expect( input ).toHaveAttribute( 'min', '0' );
} );

it( 'should pass down `inputProps` to the underlying number input', () => {
render(
<BoxControl
onChange={ () => {} }
inputProps={ { min: 10, max: 50 } }
/>
);

const input = screen.getByRole( 'textbox', { name: 'All sides' } );

expect( input ).toHaveAttribute( 'min', '10' );
expect( input ).toHaveAttribute( 'max', '50' );
} );
} );

describe( 'Reset', () => {
Expand Down
Loading