Skip to content

Commit

Permalink
UnitControl: Add support for unit step per unit type (#30376)
Browse files Browse the repository at this point in the history
* add support for unit step per unit type

* remove default step value from CSS_UNITS const

* add react native support

* missed step on native RangeCell
add comments

* allow props to override unit steps

* Update packages/components/src/unit-control/index.js

Co-authored-by: Miguel Fonseca <[email protected]>

* updates from review ->
remove setting default first unit
tidy code + comments

* Use capitals in comments

* Use block commments instead of single line

* Apply suggestions from code review

Co-authored-by: Miguel Fonseca <[email protected]>

Co-authored-by: Miguel Fonseca <[email protected]>
  • Loading branch information
rmorse and mcsf authored May 11, 2021
1 parent 7169054 commit 132fec1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/components/src/unit-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ function UnitControl(
/>
) : null;

let step = props.step;

/*
* If no step prop has been passed, lookup the active unit and
* try to get step from `units`, or default to a value of `1`
*/
if ( ! step && units ) {
const activeUnit = units.find( ( option ) => option.value === unit );
step = activeUnit?.step ?? 1;
}

return (
<Root className="components-unit-control-wrapper" style={ style }>
<ValueInput
Expand All @@ -177,6 +188,7 @@ function UnitControl(
size={ size }
suffix={ inputSuffix }
value={ value }
step={ step }
__unstableStateReducer={ composeStateReducers(
unitControlStateReducer,
stateReducer
Expand Down
13 changes: 13 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,17 @@ function UnitControl( {
);
}, [ pickerRef, units, onUnitChange, getAnchor ] );

let step = props.step;

/*
* If no step prop has been passed, lookup the active unit and
* try to get step from `units`, or default to a value of `1`
*/
if ( ! step && units ) {
const activeUnit = units.find( ( option ) => option.value === unit );
step = activeUnit?.step ?? 1;
}

return (
<>
{ unit !== '%' ? (
Expand All @@ -133,6 +144,7 @@ function UnitControl( {
onChange={ onChange }
separatorType={ separatorType }
value={ value }
step={ step }
defaultValue={ initialControlValue }
shouldDisplayTextInput
decimalNum={ unit === 'px' ? 0 : decimalNum }
Expand All @@ -149,6 +161,7 @@ function UnitControl( {
minimumValue={ min }
maximumValue={ max }
value={ value }
step={ step }
unit={ unit }
defaultValue={ initialControlValue }
separatorType={ separatorType }
Expand Down

0 comments on commit 132fec1

Please sign in to comment.