Skip to content

Commit

Permalink
fix: resolve box-spacing advanced label issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaelahidev committed Oct 27, 2024
1 parent fcc4490 commit a5c879a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
8 changes: 7 additions & 1 deletion packages/controls/js/context/hooks/use-control-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,13 @@ export const useControlContext = (args?: ControlContextHookProps): Object => {
return reset(dataset);
}

const preparedValue = prepare(args?.path, dataset);
let { path } = args;

if (['RESET_TO_DEFAULT', 'RESET_ALL'].includes(args?.action)) {
path = args?.path.replace(args?.attribute + '.', '');
}

const preparedValue = prepare(path, dataset);

return reset(
calculatedValueBasedOnSavedValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ export const getStatesGraph = ({
return null;
}

if (isEquals(value, defaultValue)) {
if (
isEquals(value, defaultValue) ||
undefined === value
) {
return null;
}

Expand Down
9 changes: 2 additions & 7 deletions packages/editor/js/components/editor-advanced-label/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ export const EditorAdvancedLabelControl = ({
200
);

// Assume singularId is set and path include attribute so,
// attribute is object has nested props therefore we can exclude attribute from recieved path,
// so that we can prepare path to reset actions!
if (path && -1 !== path.indexOf(attribute) && singularId && isRepeater) {
path = path.replace(`${attribute}.`, '');
}

const isChangedValue =
(isChanged && isChangedOnCurrentState) ||
isChangedNormalStateOnBaseBreakpoint ||
Expand Down Expand Up @@ -304,6 +297,7 @@ export const EditorAdvancedLabelControl = ({
resetToDefault({
path,
onChange,
attribute,
isRepeater,
attributes,
repeaterItem,
Expand Down Expand Up @@ -361,6 +355,7 @@ export const EditorAdvancedLabelControl = ({
resetToDefault({
path,
onChange,
attribute,
isRepeater,
attributes,
repeaterItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,7 @@ describe('Box spacing label testing (Box Spacing Control)', () => {

// assert sides label
['top', 'right', 'bottom', 'left'].forEach((side) => {
// TODO - @reza - fix this - by resetting a type (margin or padding) all sides should be removed and value of side should be 20 (normal on desktop)
// cy.checkBoxSpacingLabelContent(
// type,
// side,
// '30'
// );
cy.checkBoxSpacingLabelContent(type, side, '20');

cy.checkBoxSpacingLabelClassName(
type,
Expand All @@ -1264,10 +1259,7 @@ describe('Box spacing label testing (Box Spacing Control)', () => {
cy.checkBoxSpacingLabelClassName(
type,
side,
[
'changed-in-other-state',
'changed-in-secondary-state',
],
['changed-in-secondary-state'],
'not-have'
);
});
Expand All @@ -1282,10 +1274,7 @@ describe('Box spacing label testing (Box Spacing Control)', () => {
cy.checkBoxSpacingLabelClassName(
type,
'',
[
'changed-in-other-state',
'changed-in-secondary-state',
],
['changed-in-secondary-state'],
'not-have'
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/js/hooks/use-advanced-label-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export const useAdvancedLabelProps = (
const preparedPath = path.substring(path.indexOf('.') + 1);

const _clonedDefaultValue =
prepare(preparedPath, clonedDefaultValue) ??
prepare(preparedPath, clonedDefaultValue) ||
clonedDefaultValue;

// Compare with rootValue
Expand Down
23 changes: 21 additions & 2 deletions packages/editor/js/hooks/use-attributes/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,12 @@ export const resetAllStates = (state: Object, action: Object): Object => {
const preparedPathValue = update(
dataset,
ref.path,
ref.defaultValue
-1 !== ref.path.indexOf(attributeId)
? prepare(
ref.path.replace(attributeId + '.', ''),
ref.defaultValue
)
: ref.defaultValue
);

stateBreakpoints[breakpointType] = mergeObject(
Expand Down Expand Up @@ -520,7 +525,9 @@ export const resetAllStates = (state: Object, action: Object): Object => {

return {
...state,
[attributeId]: newValue,
[attributeId]: {
value: newValue,
},
blockeraBlockStates: {
value: blockeraBlockStates,
},
Expand Down Expand Up @@ -737,6 +744,18 @@ export const resetCurrentState = (_state: Object, action: Object): Object => {
currentStateValue = state[attributeId];
}

if (ref?.path) {
const path = ref.path.replace(attributeId + '.', '');

args.deletedProps.push(path);

if (isObject(newValue) && !newValue.hasOwnProperty(attributeId)) {
if (isEquals(ref?.defaultValue, state[attributeId])) {
newValue[path] = undefined;
}
}
}

return mergeObject(
state,
{
Expand Down

0 comments on commit a5c879a

Please sign in to comment.