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

DimensionsPanel: Fix unexpected value decoding/encoding #52661

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -207,18 +207,24 @@ export default function DimensionsPanel( {
// in global styles but not in block inspector.
includeLayoutControls = false,
} ) {
const { dimensions, spacing } = settings;

const decodeValue = ( rawValue ) => {
if ( rawValue && typeof rawValue === 'object' ) {
return Object.keys( rawValue ).reduce( ( acc, key ) => {
acc[ key ] = getValueFromVariable(
{ settings },
{ settings: { dimensions, spacing } },
'',
rawValue[ key ]
);
return acc;
}, {} );
}
return getValueFromVariable( { settings }, '', rawValue );
return getValueFromVariable(
{ settings: { dimensions, spacing } },
'',
rawValue
);
};

const showSpacingPresetsControl = useHasSpacingPresets( settings );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { LABELS, ICONS, hasAxisSupport } from '../utils';
import {
LABELS,
ICONS,
getPresetValueFromCustomValue,
hasAxisSupport,
} from '../utils';

const groupedSides = [ 'vertical', 'horizontal' ];

Expand All @@ -20,7 +25,17 @@ export default function AxialInputControls( {
if ( ! onChange ) {
return;
}
const nextValues = { ...values };

// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
spacingSizes
);
return acc;
}, {} ),
};

if ( side === 'vertical' ) {
nextValues.top = next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { ALL_SIDES, LABELS, ICONS } from '../utils';
import {
ALL_SIDES,
LABELS,
ICONS,
getPresetValueFromCustomValue,
} from '../utils';

export default function SeparatedInputControls( {
minimumCustomValue,
Expand All @@ -20,7 +25,17 @@ export default function SeparatedInputControls( {
: ALL_SIDES;

const createHandleOnChange = ( side ) => ( next ) => {
const nextValues = { ...values };
// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
spacingSizes
);
return acc;
}, {} ),
};

nextValues[ side ] = next;

onChange( nextValues );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { LABELS } from '../utils';
import { LABELS, getPresetValueFromCustomValue } from '../utils';

export default function SingleInputControl( {
minimumCustomValue,
Expand All @@ -16,7 +16,17 @@ export default function SingleInputControl( {
values,
} ) {
const createHandleOnChange = ( currentSide ) => ( next ) => {
const nextValues = { ...values };
// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
spacingSizes
);
return acc;
}, {} ),
};

nextValues[ currentSide ] = next;

onChange( nextValues );
Expand Down