Skip to content

Commit 3f487e6

Browse files
Render form question descriptions as Markdown and keep headings out of answers (#152)
1 parent bcf014a commit 3f487e6

5 files changed

Lines changed: 79 additions & 3 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@churchapps/apphelper": minor
3+
---
4+
Forms: question descriptions render as Markdown (bold, italic, lists, links), and Heading questions no longer create an empty answer or count as required when a form is submitted.

‎apphelper/src/forms/components/ConversationalForm.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import React from "react";
44
import { Box, Button, LinearProgress, Link, Typography } from "@mui/material";
55
import { ErrorMessages } from "./ErrorMessages";
6+
import { markdownDescriptionSx } from "./QuestionEdit";
7+
import { MarkdownPreviewLight } from "../../markdown/components/markdownEditor/MarkdownPreviewLight";
68
import { Locale } from "../helpers";
79
import { AnswerInterface, QuestionInterface } from "@churchapps/helpers";
810

@@ -92,7 +94,7 @@ export const ConversationalForm: React.FC<Props> = (props) => {
9294
{question.title}
9395
</Typography>
9496
{question.fieldType === "Heading" && question.description && (
95-
<Typography variant="body1" sx={{ mb: 2, textAlign: "center", color: "text.secondary", whiteSpace: "pre-wrap" }}>{question.description}</Typography>
97+
<Typography component="div" variant="body1" sx={{ mb: 2, textAlign: "center", color: "text.secondary", ...markdownDescriptionSx }}><MarkdownPreviewLight value={question.description} breaks /></Typography>
9698
)}
9799
{question.fieldType !== "Heading" && (
98100
<Box sx={{ "& .MuiInputLabel-root": { display: "none" }, "& fieldset legend": { display: "none" } }}>

‎apphelper/src/forms/components/FormSubmissionEdit.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const FormSubmissionEdit: React.FC<Props> = ({ showHeader = true, noBackg
6565
if (fs.questions !== null && fs.questions !== undefined) {
6666
fs.answers = fs.answers || [];
6767
for (let i = 0; i < fs.questions.length; i++) {
68+
if (fs.questions[i].fieldType === "Heading") continue;
6869
const answer: AnswerInterface = { questionId: fs.questions[i].id, value: "" };
6970
fs.answers.push(answer);
7071
}
@@ -117,7 +118,7 @@ export const FormSubmissionEdit: React.FC<Props> = ({ showHeader = true, noBackg
117118
for (let i = 0; i < formSubmission.questions.length; i++) {
118119
const q = formSubmission.questions[i];
119120
const answer = getAnswer(q.id || "");
120-
if (q.required) {
121+
if (q.required && q.fieldType !== "Heading") {
121122
if (answer === null || answer.value === null || answer.value === "") errors.push((q.title || "Field") + " " + Locale.label("formSubmissionEdit.isRequired"));
122123
}
123124
}

‎apphelper/src/forms/components/QuestionEdit.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import React from "react";
44
// import { MuiTelInput } from "mui-tel-input"; // Temporarily disabled due to MUI v7 compatibility
55
import { AnswerInterface, QuestionInterface } from "@churchapps/helpers";
66
import { Checkbox, Select, MenuItem, SelectChangeEvent, FormControl, InputLabel, TextField, FormLabel, FormGroup, FormControlLabel, Box, Typography } from "@mui/material";
7+
import { MarkdownPreviewLight } from "../../markdown/components/markdownEditor/MarkdownPreviewLight";
8+
9+
export const markdownDescriptionSx = { "& p": { my: 0 }, "& p + p, & ul, & ol": { mt: 1, mb: 0 }, "& ul, & ol": { pl: 3 } };
710

811
// Labels are bold, so the outlined border's notch (sized from a hidden copy of the label in its legend) must be bold too or the line runs through the label.
912
const boldLabelSx = { "& .MuiOutlinedInput-notchedOutline legend": { fontWeight: "bold" } };
@@ -36,7 +39,7 @@ export const QuestionEdit = React.forwardRef<any, Props>(({ noBackground = false
3639

3740
const q = props.question;
3841
const description = q.description
39-
? <Typography variant="body2" color="text.secondary" className="question-description" sx={{ mt: 0.5, whiteSpace: "pre-wrap" }}>{q.description}</Typography>
42+
? <Typography component="div" variant="body2" color="text.secondary" className="question-description" sx={{ mt: 0.5, ...markdownDescriptionSx }}><MarkdownPreviewLight value={q.description} breaks /></Typography>
4043
: null;
4144

4245
if (q.fieldType === "Heading") return <><h5>{q.title}</h5>{description}</>;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import React, { act } from "react";
3+
import { createRoot } from "react-dom/client";
4+
import { renderToStaticMarkup } from "react-dom/server";
5+
6+
const { get, post } = vi.hoisted(() => ({
7+
get: vi.fn(async () => [
8+
{ id: "H1", formId: "F1", title: "About you", fieldType: "Heading", required: true },
9+
{ id: "Q1", formId: "F1", title: "Name", fieldType: "Textbox" }
10+
]),
11+
post: vi.fn(async (_url: string, data: any) => data)
12+
}));
13+
14+
vi.mock("@churchapps/helpers", async (importOriginal) => {
15+
const actual: any = await importOriginal();
16+
return { ...actual, ApiHelper: { ...actual.ApiHelper, get, post } };
17+
});
18+
19+
import { QuestionEdit } from "../QuestionEdit";
20+
import { FormSubmissionEdit } from "../FormSubmissionEdit";
21+
22+
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
23+
24+
describe("Question descriptions", () => {
25+
it("render as Markdown", () => {
26+
const html = renderToStaticMarkup(
27+
<QuestionEdit
28+
question={{ id: "q1", title: "Name", fieldType: "Textbox", description: "**Bold** and [a link](https://example.com)" }}
29+
answer={{ questionId: "q1", value: "" }}
30+
changeFunction={() => { }}
31+
/>
32+
);
33+
expect(html).toContain("<strong>Bold</strong>");
34+
expect(html).toContain("href=\"https://example.com\"");
35+
});
36+
37+
it("strip unsafe links", () => {
38+
const html = renderToStaticMarkup(
39+
<QuestionEdit question={{ id: "q1", title: "Name", fieldType: "Textbox", description: "[x](javascript:alert(1))" }} answer={{ questionId: "q1", value: "" }} changeFunction={() => { }} />
40+
);
41+
expect(html).not.toContain("javascript:");
42+
});
43+
});
44+
45+
describe("Heading questions", () => {
46+
it("store no answer and are never required", async () => {
47+
const container = document.createElement("div");
48+
document.body.appendChild(container);
49+
const root = createRoot(container);
50+
51+
await act(async () => {
52+
root.render(<FormSubmissionEdit formSubmissionId="" addFormId="F1" contentType="form" contentId="F1" showHeader={false} updatedFunction={() => { }} />);
53+
});
54+
expect(container.textContent).toContain("About you");
55+
56+
const buttons = Array.from(container.querySelectorAll("button"));
57+
await act(async () => { buttons[buttons.length - 1].click(); });
58+
59+
expect(post).toHaveBeenCalledTimes(1);
60+
const submission = post.mock.calls[0][1][0];
61+
expect(submission.answers.map((a: any) => a.questionId)).toEqual(["Q1"]);
62+
63+
act(() => root.unmount());
64+
container.remove();
65+
});
66+
});

0 commit comments

Comments
 (0)