Skip to content

Commit 27f972a

Browse files
committed
fix: Bug with generating React Code when using WYSIWYG Editor
1 parent 7f60509 commit 27f972a

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎src/components/form-builder/form-components/form-text.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
cn,
33
generateTWClassesForAllViewports,
4-
escapeHtml,
4+
replaceBrTags,
5+
replaceClassWithClassName,
6+
replaceStyleStringWithObject,
57
} from "@/lib/utils";
68
import { DesignPropertiesViews } from "@/types/form-builder.types";
79
import { FormComponentModel } from "@/models/FormComponent";
@@ -31,13 +33,24 @@ type ReactCode = {
3133
};
3234

3335
export function getReactCode(component: FormComponentModel): ReactCode {
36+
37+
let content = replaceStyleStringWithObject(component.content || "");
38+
content = replaceBrTags(content);
39+
content = replaceClassWithClassName(content);
40+
const colSpanClasses = generateTWClassesForAllViewports(component, "colSpan");
41+
const colStartClasses = generateTWClassesForAllViewports(
42+
component,
43+
"colStart"
44+
);
45+
const customClasses = component.getField("attributes.class") || "";
46+
3447
return {
3548
code: `
3649
<div
3750
key="${component.id}"
3851
id="${component.getField("attributes.id")}"
39-
className="${component.getField("attributes.class")}">
40-
${component.content || ""}
52+
className="${customClasses} ${colSpanClasses} ${colStartClasses}">
53+
${content}
4154
</div>
4255
`,
4356
dependencies: {

‎src/lib/utils.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,44 @@ export const escapeHtml = (text: string, whitelist?: EscapeHtmlWhitelist[]): str
313313

314314
};
315315

316+
export const replaceBrTags = (text: string): string => {
317+
return text.replace(/<br\s*\/?>/g, '<br />');
318+
};
319+
320+
export const replaceClassWithClassName = (text: string): string => {
321+
return text.replace(/class="([^"]+)"/g, (match, p1) => {
322+
return `className="${p1}"`;
323+
});
324+
};
325+
326+
export const convertStyleToStyleObject = (style: string): Record<string, string> => {
327+
if (!style) return {};
328+
329+
// Split the style string by semicolons and filter out empty strings
330+
const stylePairs = style.split(';').filter(Boolean);
331+
332+
return stylePairs.reduce((acc, pair) => {
333+
// Split each pair by colon and trim whitespace
334+
const [property, value] = pair.split(':').map(str => str.trim());
335+
336+
if (property && value) {
337+
// Convert CSS property to camelCase (e.g., text-align -> textAlign)
338+
const camelCaseProperty = property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
339+
acc[camelCaseProperty] = value;
340+
}
341+
342+
return acc;
343+
}, {} as Record<string, string>);
344+
};
345+
346+
export const replaceStyleStringWithObject = (style: string): string => {
347+
return style.replace(/style="([^"]+)"/g, (match, p1) => {
348+
return `style={${JSON.stringify(convertStyleToStyleObject(p1))}}`;
349+
});
350+
351+
}
352+
353+
316354
export const getGridRows = (
317355
items: FormComponentModel[],
318356
viewport: Viewports

0 commit comments

Comments
 (0)