Skip to content

Commit 2934984

Browse files
author
Ashish manhas
authored
fix:safe HTML rendering for radio label (calcom#22645)
1 parent 28cb2cf commit 2934984

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

packages/features/form-builder/FormBuilderField.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Controller, useFormContext } from "react-hook-form";
44
import type { z } from "zod";
55

66
import { useLocale } from "@calcom/lib/hooks/useLocale";
7+
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
78
import classNames from "@calcom/ui/classNames";
89
import { InfoBadge } from "@calcom/ui/components/badge";
910
import { Label } from "@calcom/ui/components/form";
@@ -19,6 +20,19 @@ import {
1920
} from "./useShouldBeDisabledDueToPrefill";
2021
import { getTranslatedConfig as getTranslatedVariantsConfig } from "./utils/variantsConfig";
2122

23+
// helper to render markdown label safely
24+
const renderLabel = (field: Partial<RhfFormField>) => {
25+
if (field.labelAsSafeHtml) {
26+
return (
27+
<span
28+
// eslint-disable-next-line react/no-danger
29+
dangerouslySetInnerHTML={{ __html: markdownToSafeHTML(field.labelAsSafeHtml) }}
30+
/>
31+
);
32+
}
33+
return <span>{field.label}</span>;
34+
};
35+
2236
type RhfForm = {
2337
fields: z.infer<typeof fieldsSchema>;
2438
};
@@ -155,7 +169,7 @@ const WithLabel = ({
155169
field.label && (
156170
<div className="mb-2 flex items-center">
157171
<Label className="!mb-0 flex items-center" htmlFor={htmlFor}>
158-
<span>{field.label}</span>
172+
{renderLabel(field)}
159173
<span className="text-emphasis -mb-1 ml-1 text-sm font-medium leading-none">
160174
{!readOnly && field.required ? "*" : ""}
161175
</span>

packages/features/form-builder/fieldsThatSupportLabelAsSafeHtml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import type { FieldType } from "./schema";
44
* Once a component supports `labelAsSafeHtml`, add it's field's type here
55
* A whitelist is needed because unless we explicitly use dangerouslySetInnerHTML, React will escape the HTML
66
*/
7-
export const fieldsThatSupportLabelAsSafeHtml: FieldType[] = ["boolean"];
7+
export const fieldsThatSupportLabelAsSafeHtml: FieldType[] = ["boolean", "radio"];

0 commit comments

Comments
 (0)