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

UnitControl: Add support for unit step per unit type #30376

Merged
merged 13 commits into from
May 11, 2021
15 changes: 15 additions & 0 deletions packages/components/src/unit-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Root, ValueInput } from './styles/unit-control-styles';
import UnitSelectControl from './unit-select-control';
import { CSS_UNITS, getParsedValue, getValidParsedUnit } from './utils';
import { useControlledState } from '../utils/hooks';
// import { active } from '@wp-g2/styles/types';
Copy link
Contributor Author

@rmorse rmorse Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing my local build to fail, and it wasn't in use - so I've commented it for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need anything here?


function UnitControl(
{
Expand Down Expand Up @@ -158,6 +159,19 @@ function UnitControl(
/>
) : null;

let step = props.step;
// if no step prop has been passed
rmorse marked this conversation as resolved.
Show resolved Hide resolved
if ( ! step ) {
rmorse marked this conversation as resolved.
Show resolved Hide resolved
// Get current unit type
let activeUnit = units.find( ( option ) => option.value === unit );
// if unit wasn't found, choose the first option
if ( ! activeUnit && units.length > 0 ) {
activeUnit = units[ 0 ];
}
rmorse marked this conversation as resolved.
Show resolved Hide resolved
// Extract the step if it exists, or default to `1`
step = activeUnit?.step ? activeUnit.step : 1;
rmorse marked this conversation as resolved.
Show resolved Hide resolved
}

return (
<Root className="components-unit-control-wrapper" style={ style }>
<ValueInput
Expand All @@ -177,6 +191,7 @@ function UnitControl(
size={ size }
suffix={ inputSuffix }
value={ value }
step={ step }
__unstableStateReducer={ composeStateReducers(
unitControlStateReducer,
stateReducer
Expand Down
15 changes: 15 additions & 0 deletions packages/components/src/unit-control/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ function UnitControl( {
);
}, [ pickerRef, units, onUnitChange, getAnchor ] );

let step = props.step;
// If no step prop has been passed
if ( ! step ) {
rmorse marked this conversation as resolved.
Show resolved Hide resolved
// Get current unit type
let activeUnit = units.find( ( option ) => option.value === unit );
// if unit wasn't found, choose the first option
if ( ! activeUnit && units.length > 0 ) {
activeUnit = units[ 0 ];
}
// Extract the step if it exists, or default to `1`
step = activeUnit?.step ? activeUnit.step : 1;
}

return (
<>
{ unit !== '%' ? (
Expand All @@ -133,6 +146,7 @@ function UnitControl( {
onChange={ onChange }
separatorType={ separatorType }
value={ value }
step={ step }
defaultValue={ initialControlValue }
shouldDisplayTextInput
decimalNum={ unit === 'px' ? 0 : decimalNum }
Expand All @@ -149,6 +163,7 @@ function UnitControl( {
minimumValue={ min }
maximumValue={ max }
value={ value }
step={ unitStep }
rmorse marked this conversation as resolved.
Show resolved Hide resolved
unit={ unit }
defaultValue={ initialControlValue }
separatorType={ separatorType }
Expand Down