From 6dd725b7aa3d7a218a7426cfa3df9ec72c90b6a1 Mon Sep 17 00:00:00 2001 From: Giulia Ghisini Date: Tue, 16 Apr 2024 13:08:26 +0200 Subject: [PATCH] fix: fix field schema form sent_message and added customizable FormResult component --- src/components/FormResult.jsx | 63 +++++++++++++++++++++++++++++++++++ src/components/FormView.jsx | 57 ++++--------------------------- src/components/index.js | 5 +++ src/formSchema.js | 2 +- 4 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 src/components/FormResult.jsx diff --git a/src/components/FormResult.jsx b/src/components/FormResult.jsx new file mode 100644 index 0000000..779c3cf --- /dev/null +++ b/src/components/FormResult.jsx @@ -0,0 +1,63 @@ +import React from 'react'; +import { useIntl, defineMessages } from 'react-intl'; +import { Message, Button } from 'semantic-ui-react'; + +const messages = defineMessages({ + success: { + id: 'form_submit_success', + defaultMessage: 'Sent!', + }, + reset: { + id: 'form_reset', + defaultMessage: 'Clear', + }, +}); + +/* Function that replaces variables from the user customized message */ +const replaceMessage = (text, sent_data) => { + let i = 0; + while (i < sent_data.length) { + let idField = sent_data[i].label; + text = text.replaceAll('${' + idField + '}', sent_data[i].value ?? ''); + i++; + } + text = text.replaceAll('\n', '
'); + return text; +}; + +const FormResult = ({ formState, data, resetFormState }) => { + const intl = useIntl(); + return ( + + {/* Custom message */} + {data.send_message ? ( +

+ ) : ( + <> + {/* Default message */} + + {intl.formatMessage(messages.success)} + +

{formState.result}

+ + )} + {/* Back button */} + +
+ ); +}; +export default FormResult; diff --git a/src/components/FormView.jsx b/src/components/FormView.jsx index c9aa247..409192f 100644 --- a/src/components/FormView.jsx +++ b/src/components/FormView.jsx @@ -11,7 +11,7 @@ import { import { getFieldName } from 'volto-form-block/components/utils'; import Field from 'volto-form-block/components/Field'; import config from '@plone/volto/registry'; - +import { FormResult } from 'volto-form-block/components'; /* Style */ import 'volto-form-block/components/FormView.css'; @@ -28,18 +28,10 @@ const messages = defineMessages({ id: 'Error', defaultMessage: 'Error', }, - success: { - id: 'form_submit_success', - defaultMessage: 'Sent!', - }, form_errors: { id: 'form_errors_validation', defaultMessage: 'There are some errors in the form.', }, - reset: { - id: 'form_reset', - defaultMessage: 'Clear', - }, }); const FormView = ({ @@ -62,18 +54,6 @@ const FormView = ({ return formErrors?.filter((e) => e.field === field).length === 0; }; - /* Function that replaces variables from the user customized message */ - const replaceMessage = (text) => { - let i = 0; - const sent_data = formState.result.data; - while (i < sent_data.length) { - let idField = sent_data[i].label; - text = text.replaceAll('${' + idField + '}', sent_data[i].value ?? ''); - i++; - } - return text; - }; - const submit = (e) => { resetFormOnError(); onSubmit(e); @@ -89,36 +69,11 @@ const FormView = ({ )} {formState.result ? ( - - {/* Custom message */} - {data.send_message ? ( -

- ) : ( - <> - {/* Default message */} - - {intl.formatMessage(messages.success)} - -

{formState.result}

- - )} - {/* Back button */} - -
+ ) : (
/* webpackChunkName: "VoltoFormBlockView" */ 'volto-form-block/components/FormView' ), ); +export const FormResult = loadable(() => + import( + /* webpackChunkName: "VoltoFormBlockView" */ 'volto-form-block/components/FormResult' + ), +); export const Field = loadable(() => import( /* webpackChunkName: "VoltoFormBlockView" */ 'volto-form-block/components/Field' diff --git a/src/formSchema.js b/src/formSchema.js index 4edb803..950d9f7 100644 --- a/src/formSchema.js +++ b/src/formSchema.js @@ -164,7 +164,7 @@ const Schema = (data) => { }, send_message: { title: intl.formatMessage(messages.send_message), - type: 'textarea', + widget: 'textarea', description: intl.formatMessage(messages.send_message_helptext), }, },