Skip to content
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 @@ -7,6 +7,8 @@ import {
hasMixedValues,
hasDefinedValues,
mode,
getPresetValueFromCustomValue,
getPresetValueFromControlValue,
} from '../utils';

const defaultUnitSelections = {
Expand Down Expand Up @@ -180,3 +182,72 @@ describe( 'mode', () => {
expect( mode( values ) ).toBe( 'a' );
} );
} );

describe( 'getPresetValueFromCustomValue', () => {
const presets = [
{ name: 'None', slug: '0', size: 0 },
{ name: 'Small', slug: 'sm', size: '4px' },
{ name: 'Medium', slug: 'md', size: 'clamp(2px, 1vw, 8px)' },
];

it( 'should return "0" if value is "0"', () => {
expect( getPresetValueFromCustomValue( '0', presets ) ).toBe( '0' );
} );

it( 'should return preset reference if value matches a preset', () => {
expect( getPresetValueFromCustomValue( '4px', presets ) ).toBe(
'var:preset|border-radius|sm'
);
expect(
getPresetValueFromCustomValue( 'clamp(2px, 1vw, 8px)', presets )
).toBe( 'var:preset|border-radius|md' );
} );

it( 'should return value as-is if no matching preset', () => {
expect( getPresetValueFromCustomValue( '7px', presets ) ).toBe( '7px' );
} );

it( 'should return value as-is if already a preset reference', () => {
expect(
getPresetValueFromCustomValue(
'var:preset|border-radius|md',
presets
)
).toBe( 'var:preset|border-radius|md' );
} );

it( 'should return undefined if value is undefined', () => {
expect(
getPresetValueFromCustomValue( undefined, presets )
).toBeUndefined();
} );
} );

describe( 'getPresetValueFromControlValue', () => {
const presets = [
{ name: 'None', slug: '0', size: 0 },
{ name: 'Small', slug: 'sm', size: '4px' },
{ name: 'Medium', slug: 'md', size: 'clamp(2px, 1vw, 8px)' },
];

it( 'should return "0" if control value is 0 and not selectList', () => {
expect( getPresetValueFromControlValue( 0, 'slider', presets ) ).toBe(
'0'
);
} );

it( 'should return undefined if control value is 0 and controlType is selectList', () => {
expect(
getPresetValueFromControlValue( 0, 'selectList', presets )
).toBeUndefined();
} );

it( 'should return preset reference for other values', () => {
expect( getPresetValueFromControlValue( 1, 'slider', presets ) ).toBe(
'var:preset|border-radius|sm'
);
expect( getPresetValueFromControlValue( 2, 'slider', presets ) ).toBe(
'var:preset|border-radius|md'
);
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export function getPresetValueFromControlValue(
} else if ( size === 0 ) {
return '0';
}

return `var:preset|border-radius|${ presets[ controlValue ]?.slug }`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ export default function BorderPanel( {
const showBorderRadius = useHasBorderRadiusControl( settings );
const borderRadiusValues = useMemo( () => {
if ( typeof border?.radius !== 'object' ) {
return decodeValue( border?.radius );
return border?.radius;
}

return {
topLeft: decodeValue( border?.radius?.topLeft ),
topRight: decodeValue( border?.radius?.topRight ),
bottomLeft: decodeValue( border?.radius?.bottomLeft ),
bottomRight: decodeValue( border?.radius?.bottomRight ),
topLeft: border?.radius?.topLeft,
topRight: border?.radius?.topRight,
bottomLeft: border?.radius?.bottomLeft,
bottomRight: border?.radius?.bottomRight,
};
}, [ border?.radius, decodeValue ] );
}, [ border?.radius ] );
const setBorderRadius = ( newBorderRadius ) =>
setBorder( { ...border, radius: newBorderRadius } );
const hasBorderRadius = () => {
Expand Down
Loading