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

fix: Handles button binding during buildingblock pasting #33674

Merged
merged 6 commits into from
May 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { updateWidgetPositions } from "layoutSystems/autolayout/utils/positionUt
import type { FlexLayer } from "layoutSystems/autolayout/utils/types";
import {
getNewPositions,
handleButtonDynamicTriggerPathList,
handleImageWidgetWhenPasting,
handleJSONFormPropertiesListedInDynamicBindingPath,
handleJSONFormWidgetWhenPasting,
Expand Down Expand Up @@ -844,6 +845,9 @@ function handleOtherWidgetReferencesWhilePastingBuildingBlockWidget(
case "IMAGE_WIDGET":
handleImageWidgetWhenPasting(widgetNameMap, widget);
break;
case "BUTTON_WIDGET":
handleButtonDynamicTriggerPathList(widgetNameMap, widget);
break;
default:
widgets = handleSpecificCasesWhilePasting(
widget,
Expand Down
26 changes: 26 additions & 0 deletions app/client/src/sagas/PasteWidgetUtils/PasteWidgetUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
handleJSONFormWidgetWhenPasting,
handleTextWidgetWhenPasting,
handleJSONFormPropertiesListedInDynamicBindingPath,
handleButtonDynamicTriggerPathList,
} from ".";
import {
widget,
Expand All @@ -17,6 +18,7 @@ import {
const widgetNameMap = {
table1: "table1Copy",
};
import { klona } from "klona";

function testIndividualWidgetPasting(
widgetNameMap: Record<string, string>,
Expand Down Expand Up @@ -229,3 +231,27 @@ describe("handleJSONFormPropertiesListedInDynamicBindingPath", () => {
]);
});
});

describe("handleButtonDynamicTriggerPathList", () => {
const widget = {
dynamicTriggerPathList: [{ key: "onClick" }],
onClick: "{{oldName.val}}",
} as any as FlattenedWidgetProps;
it("1. should replace old widget names with new widget names in dynamic trigger paths", () => {
const widgetNameMap = {
oldName: "newName",
};
const button = klona(widget);
handleButtonDynamicTriggerPathList(widgetNameMap, button);
expect(button.onClick).toEqual("{{newName.val}}");
});

it("2. should do nothing if the widgetNameMap does not contain names in dynamic trigger paths", () => {
const widgetNameMap = {
oldWidget1: "newWidget1",
};
const button = klona(widget);
handleButtonDynamicTriggerPathList(widgetNameMap, button);
expect(button.onClick).toEqual("{{oldName.val}}");
});
});
11 changes: 11 additions & 0 deletions app/client/src/sagas/PasteWidgetUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,14 @@ export function accessNestedObjectValue(
),
);
}

export function handleButtonDynamicTriggerPathList(
widgetNameMap: Record<string, string>,
widget: FlattenedWidgetProps,
) {
Object.entries(widgetNameMap).forEach(([oldWidgetName, newWidgetName]) => {
widget.dynamicTriggerPathList?.forEach((path: { key: string }) => {
accessNestedObjectValue(widget, path.key, oldWidgetName, newWidgetName);
});
});
}
Loading