From 5160b8be102a65287d4f02c1e2c340a129124674 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 6 Dec 2024 13:56:20 +0100 Subject: [PATCH] Simplify merging logic --- packages/components/src/box-control/index.tsx | 4 +- .../src/box-control/input-control.tsx | 12 +-- packages/components/src/box-control/utils.ts | 87 ++++++------------- 3 files changed, 34 insertions(+), 69 deletions(-) diff --git a/packages/components/src/box-control/index.tsx b/packages/components/src/box-control/index.tsx index 1c453913da3583..279dfa55eafe38 100644 --- a/packages/components/src/box-control/index.tsx +++ b/packages/components/src/box-control/index.tsx @@ -21,7 +21,7 @@ import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils'; import { DEFAULT_VALUES, getInitialSide, - isValuesMixed, + isValueMixed, isValuesDefined, getAllowedSides, } from './utils'; @@ -95,7 +95,7 @@ function BoxControl( { const [ isDirty, setIsDirty ] = useState( hasInitialValue ); const [ isLinked, setIsLinked ] = useState( - ! hasInitialValue || ! isValuesMixed( inputValues ) || hasOneSide + ! hasInitialValue || ! isValueMixed( inputValues ) || hasOneSide ); const [ side, setSide ] = useState< BoxControlIconProps[ 'side' ] >( diff --git a/packages/components/src/box-control/input-control.tsx b/packages/components/src/box-control/input-control.tsx index 58d441209c1b7c..992459a5587155 100644 --- a/packages/components/src/box-control/input-control.tsx +++ b/packages/components/src/box-control/input-control.tsx @@ -12,9 +12,9 @@ import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils'; import { CUSTOM_VALUE_SETTINGS, getAllowedSides, - getAllValue, + getMergedValue, + isValueMixed, isValuesDefined, - isValuesMixed, LABELS, } from './utils'; import { @@ -125,16 +125,12 @@ export default function BoxInputControl( { setSelectedUnits( newUnits ); }; - const mergedValue = getAllValue( - values, - selectedUnits, - defaultValuesToModify - ); + const mergedValue = getMergedValue( values, defaultValuesToModify ); const hasValues = isValuesDefined( values ); const isMixed = hasValues && defaultValuesToModify.length > 1 && - isValuesMixed( values, selectedUnits, defaultValuesToModify ); + isValueMixed( values, defaultValuesToModify ); const mixedPlaceholder = isMixed ? __( 'Mixed' ) : undefined; const [ parsedQuantity, parsedUnit ] = parseQuantityAndUnitFromRawValue( mergedValue ); diff --git a/packages/components/src/box-control/utils.ts b/packages/components/src/box-control/utils.ts index 2d064d38f53b41..c37ea4cc126300 100644 --- a/packages/components/src/box-control/utils.ts +++ b/packages/components/src/box-control/utils.ts @@ -6,7 +6,6 @@ import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils'; import type { BoxControlInputControlProps, BoxControlProps, @@ -83,56 +82,46 @@ function mode< T >( arr: T[] ) { } /** - * Gets the 'all' input value and unit from values data. + * Gets the merged input value and unit from values data. * * @param values Box values. - * @param selectedUnits Box units. * @param availableSides Available box sides to evaluate. * * @return A value + unit for the 'all' input. */ -export function getAllValue( +export function getMergedValue( values: BoxControlValue = {}, - selectedUnits?: BoxControlValue, availableSides: BoxControlProps[ 'sides' ] = ALL_SIDES ) { const sides = normalizeSides( availableSides ); - const parsedQuantitiesAndUnits = sides.map( ( side ) => - parseQuantityAndUnitFromRawValue( values[ side ] ) - ); - const allParsedQuantities = parsedQuantitiesAndUnits.map( - ( value ) => value[ 0 ] ?? '' - ); - const allParsedUnits = parsedQuantitiesAndUnits.map( - ( value ) => value[ 1 ] - ); - - const commonQuantity = allParsedQuantities.every( - ( v ) => v === allParsedQuantities[ 0 ] - ) - ? allParsedQuantities[ 0 ] - : ''; - - /** - * The typeof === 'number' check is important. On reset actions, the incoming value - * may be null or an empty string. - * - * Also, the value may also be zero (0), which is considered a valid unit value. - * - * typeof === 'number' is more specific for these cases, rather than relying on a - * simple truthy check. - */ - let commonUnit; - if ( typeof commonQuantity === 'number' ) { - 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 ); + if ( + sides.every( + ( side: keyof BoxControlValue ) => + values[ side ] === values[ sides[ 0 ] ] + ) + ) { + return values[ sides[ 0 ] ]; } - return [ commonQuantity, commonUnit ].join( '' ); + return undefined; +} + +/** + * Checks if the values are mixed. + * + * @param values Box values. + * @param availableSides Available box sides to evaluate. + * @return Whether the values are mixed. + */ +export function isValueMixed( + values: BoxControlValue = {}, + availableSides: BoxControlProps[ 'sides' ] = ALL_SIDES +) { + const sides = normalizeSides( availableSides ); + return sides.some( + ( side: keyof BoxControlValue ) => + values[ side ] !== values[ sides[ 0 ] ] + ); } /** @@ -151,26 +140,6 @@ export function getAllUnitFallback( selectedUnits?: BoxControlValue ) { return mode( filteredUnits ); } -/** - * Checks to determine if values are mixed. - * - * @param values Box values. - * @param selectedUnits Box units. - * @param sides Available box sides to evaluate. - * - * @return Whether values are mixed. - */ -export function isValuesMixed( - values: BoxControlValue = {}, - selectedUnits?: BoxControlValue, - sides: BoxControlProps[ 'sides' ] = ALL_SIDES -) { - const allValue = getAllValue( values, selectedUnits, sides ); - const isMixed = isNaN( parseFloat( allValue ) ); - - return isMixed; -} - /** * Checks to determine if values are defined. *