-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
BoxControl
: stop using UnitControl
's deprecated unit
prop
#39511
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils'; | ||
import UnitControl from './unit-control'; | ||
import { LABELS } from './utils'; | ||
import { Layout } from './styles/box-control-styles'; | ||
|
@@ -113,27 +114,37 @@ export default function AxialInputControls( { | |
align="top" | ||
className="component-box-control__vertical-horizontal-input-controls" | ||
> | ||
{ filteredSides.map( ( side ) => ( | ||
<UnitControl | ||
{ ...props } | ||
isFirst={ first === side } | ||
isLast={ last === side } | ||
isOnly={ only === side } | ||
value={ side === 'vertical' ? values.top : values.left } | ||
unit={ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For ease of review: the |
||
side === 'vertical' | ||
? selectedUnits.top | ||
: selectedUnits.left | ||
} | ||
onChange={ createHandleOnChange( side ) } | ||
onUnitChange={ createHandleOnUnitChange( side ) } | ||
onFocus={ createHandleOnFocus( side ) } | ||
onHoverOn={ createHandleOnHoverOn( side ) } | ||
onHoverOff={ createHandleOnHoverOff( side ) } | ||
label={ LABELS[ side ] } | ||
key={ side } | ||
/> | ||
) ) } | ||
{ filteredSides.map( ( side ) => { | ||
const [ | ||
parsedQuantity, | ||
parsedUnit, | ||
] = parseQuantityAndUnitFromRawValue( | ||
side === 'vertical' ? values.top : values.left | ||
); | ||
const selectedUnit = | ||
side === 'vertical' | ||
? selectedUnits.top | ||
: selectedUnits.left; | ||
return ( | ||
<UnitControl | ||
{ ...props } | ||
isFirst={ first === side } | ||
isLast={ last === side } | ||
isOnly={ only === side } | ||
value={ [ | ||
parsedQuantity, | ||
selectedUnit ?? parsedUnit, | ||
].join( '' ) } | ||
onChange={ createHandleOnChange( side ) } | ||
onUnitChange={ createHandleOnUnitChange( side ) } | ||
onFocus={ createHandleOnFocus( side ) } | ||
onHoverOn={ createHandleOnHoverOn( side ) } | ||
onHoverOff={ createHandleOnHoverOff( side ) } | ||
label={ LABELS[ side ] } | ||
key={ side } | ||
/> | ||
); | ||
} ) } | ||
</Layout> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { noop } from 'lodash'; | |
* Internal dependencies | ||
*/ | ||
import UnitControl from './unit-control'; | ||
import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils'; | ||
import { ALL_SIDES, LABELS } from './utils'; | ||
import { LayoutContainer, Layout } from './styles/box-control-styles'; | ||
|
||
|
@@ -91,25 +92,35 @@ export default function BoxInputControls( { | |
align="top" | ||
className="component-box-control__input-controls" | ||
> | ||
{ filteredSides.map( ( side ) => ( | ||
<UnitControl | ||
{ ...props } | ||
isFirst={ first === side } | ||
isLast={ last === side } | ||
isOnly={ only === side } | ||
value={ values[ side ] } | ||
unit={ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For ease of review: the |
||
values[ side ] ? undefined : selectedUnits[ side ] | ||
} | ||
onChange={ createHandleOnChange( side ) } | ||
onUnitChange={ createHandleOnUnitChange( side ) } | ||
onFocus={ createHandleOnFocus( side ) } | ||
onHoverOn={ createHandleOnHoverOn( side ) } | ||
onHoverOff={ createHandleOnHoverOff( side ) } | ||
label={ LABELS[ side ] } | ||
key={ `box-control-${ side }` } | ||
/> | ||
) ) } | ||
{ filteredSides.map( ( side ) => { | ||
const [ | ||
parsedQuantity, | ||
parsedUnit, | ||
] = parseQuantityAndUnitFromRawValue( values[ side ] ); | ||
|
||
const computedUnit = values[ side ] | ||
? parsedUnit | ||
: selectedUnits[ side ]; | ||
|
||
return ( | ||
<UnitControl | ||
{ ...props } | ||
isFirst={ first === side } | ||
isLast={ last === side } | ||
isOnly={ only === side } | ||
value={ [ parsedQuantity, computedUnit ].join( | ||
'' | ||
) } | ||
onChange={ createHandleOnChange( side ) } | ||
onUnitChange={ createHandleOnUnitChange( side ) } | ||
onFocus={ createHandleOnFocus( side ) } | ||
onHoverOn={ createHandleOnHoverOn( side ) } | ||
onHoverOff={ createHandleOnHoverOff( side ) } | ||
label={ LABELS[ side ] } | ||
key={ `box-control-${ side }` } | ||
/> | ||
); | ||
} ) } | ||
</Layout> | ||
</LayoutContainer> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,24 +61,32 @@ function mode( arr ) { | |
* Gets the 'all' input value and unit from values data. | ||
* | ||
* @param {Object} values Box values. | ||
* @param {Object} selectedUnits Box units. | ||
* @param {Array} availableSides Available box sides to evaluate. | ||
* | ||
* @return {string} A value + unit for the 'all' input. | ||
*/ | ||
export function getAllValue( values = {}, availableSides = ALL_SIDES ) { | ||
export function getAllValue( | ||
values = {}, | ||
selectedUnits, | ||
availableSides = ALL_SIDES | ||
) { | ||
const sides = normalizeSides( availableSides ); | ||
const parsedQuantitiesAndUnits = sides.map( ( side ) => | ||
parseQuantityAndUnitFromRawValue( values[ side ] ) | ||
); | ||
const allValues = parsedQuantitiesAndUnits.map( | ||
const allParsedQuantities = parsedQuantitiesAndUnits.map( | ||
( value ) => value[ 0 ] ?? '' | ||
); | ||
const allUnits = parsedQuantitiesAndUnits.map( ( value ) => value[ 1 ] ); | ||
const allParsedUnits = parsedQuantitiesAndUnits.map( | ||
Comment on lines
+78
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verbosity FTW |
||
( value ) => value[ 1 ] | ||
); | ||
|
||
const value = allValues.every( ( v ) => v === allValues[ 0 ] ) | ||
? allValues[ 0 ] | ||
const commonQuantity = allParsedQuantities.every( | ||
( v ) => v === allParsedQuantities[ 0 ] | ||
) | ||
? allParsedQuantities[ 0 ] | ||
: ''; | ||
const unit = mode( allUnits ); | ||
|
||
/** | ||
* The isNumber check is important. On reset actions, the incoming value | ||
|
@@ -89,9 +97,17 @@ export function getAllValue( values = {}, availableSides = ALL_SIDES ) { | |
* isNumber() is more specific for these cases, rather than relying on a | ||
* simple truthy check. | ||
*/ | ||
const allValue = isNumber( value ) ? `${ value }${ unit }` : undefined; | ||
let commonUnit; | ||
if ( isNumber( commonQuantity ) ) { | ||
commonUnit = mode( allParsedUnits ); | ||
} else { | ||
// Set meaningful unit selection if no commonQuantity and user has previously | ||
// selected units without assigning values while controls were unlinked. | ||
commonUnit = | ||
getAllUnitFallback( selectedUnits ) ?? mode( allParsedUnits ); | ||
} | ||
|
||
return allValue; | ||
return [ commonQuantity, commonUnit ].join( '' ); | ||
} | ||
|
||
/** | ||
|
@@ -113,13 +129,14 @@ export function getAllUnitFallback( selectedUnits ) { | |
/** | ||
* Checks to determine if values are mixed. | ||
* | ||
* @param {Object} values Box values. | ||
* @param {Array} sides Available box sides to evaluate. | ||
* @param {Object} values Box values. | ||
* @param {Object} selectedUnits Box units. | ||
* @param {Array} sides Available box sides to evaluate. | ||
* | ||
* @return {boolean} Whether values are mixed. | ||
*/ | ||
export function isValuesMixed( values = {}, sides = ALL_SIDES ) { | ||
const allValue = getAllValue( values, sides ); | ||
export function isValuesMixed( values = {}, selectedUnits, sides = ALL_SIDES ) { | ||
const allValue = getAllValue( values, selectedUnits, sides ); | ||
const isMixed = isNaN( parseFloat( allValue ) ); | ||
|
||
return isMixed; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ease of review: the
unit
prop below was removed. As a consequence, theallUnitFallback
variable was also removed. The logic used for computingallUnitFallback
has been moved and "embedded" in thegetAllValue
utility function