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

Add paragraph text element block #58

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/@episerver/forms-react/src/components/FormBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const FormBody = (props: FormBodyProps) => {
if (isFormFinalized.current) {
formCache.remove(submissionStorageKey)
localFormCache.remove(submissionStorageKey)
formCache.remove(FormConstants.FormCurrentStep + form.key)
}
}).catch((e: ProblemDetail) => {
if(e.status === 401) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { FormCache, FormContainer, FormStorage, ParagraphText, Url } from "@episerver/forms-sdk"
import React, { useMemo } from "react";
import ElementWrapper from "./shared/ElementWrapper";
import { useElement } from "../../hooks/useElement";
import { ElementCaption, ValidationMessage } from "./shared";
import { useForms } from "../../context/store";

export interface ParagraphTextElementBlockProps {
element: ParagraphText
}

export const ParagraphTextElementBlock = (props: ParagraphTextElementBlockProps) => {
const { element } = props;
const { elementContext, handleChange, handleBlur } = useElement(element);
const { isVisible, validationResults, value, validatorClasses } = elementContext;
const formContext = useForms();
const form = formContext?.formContainer as FormContainer
const formKey = form.key;

const formStorage = new FormStorage(form);
const data = formStorage.loadFormDataFromStorage();

const doReplaceText = element.properties.disablePlaceholdersReplacement ?? true

function extractTextsWithFormat(inputString: string) {
const regex = /::(.*?)::/g;
const matches = inputString.match(regex);
if (matches) {
return matches.map(match => match.slice(2, -2));
} else {
return [];
}
}

let text = element.properties.paragraphText
const placeHolders = extractTextsWithFormat(element.properties.paragraphText)

if (doReplaceText) {
data.forEach(element => {
const key = element.elementKey
const value = element.value as string
const friendlyName = form.formElements.find(fe => fe.key === key)?.displayName
if (friendlyName && placeHolders.indexOf(friendlyName) !== -1) {
text = text.replace("::" + friendlyName + "::", value)
}
});
}

console.log(text)

return useMemo(() => (
<ElementWrapper className={`FormParagraphText Form__Element--NonData ${validatorClasses}`} validationResults={validationResults} isVisible={isVisible}>
<ElementCaption element={element} />
<>
<div id={formKey} >
<div dangerouslySetInnerHTML={{
__html: text
}}/>
</div>
<div id={formKey + "__OriginalText"} className="Form__Original__ParagraphText">

</div>
</>
</ElementWrapper >
), [isVisible, validationResults, value]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ImageChoiceElementBlock } from "./ImageChoiceElementBlock";
import { FileUploadElementBlock } from "./FileUploadElementBlock";
import { ResetButtonElementBlock } from "./ResetButtonElementBlock";
import { SubmitButtonElementBlock } from "./SubmitButtonElementBlock";
import { ParagraphTextElementBlock } from "./ParagraphTextElementBlock";

export const components: Record<string, any> = {
TextboxElementBlock,
Expand All @@ -25,5 +26,6 @@ export const components: Record<string, any> = {
ImageChoiceElementBlock,
FileUploadElementBlock,
ResetButtonElementBlock,
SubmitButtonElementBlock
SubmitButtonElementBlock,
ParagraphTextElementBlock
};
7 changes: 4 additions & 3 deletions src/@episerver/forms-sdk/src/form-submit/formSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ export class FormSubmitter {
return new Promise<FormSubmitResult>((resolve, reject)=>{
let formStorage = new FormStorage(this._form);

//save data to storage of browser
formStorage.saveFormDataToStorage(model.submissionData);
// Save data to storage of browser
let currentData = formStorage.loadFormDataFromStorage()
formStorage.saveFormDataToStorage(currentData.concat(model.submissionData));

//post data to API
// Post data to API
let formData = new FormData();
formData.append("formKey", model.formKey);
formData.append("locale", model.locale);
Expand Down