diff --git a/src/components/mui/formik-inputs/mui-formik-text-editor.js b/src/components/mui/formik-inputs/mui-formik-text-editor.js new file mode 100644 index 00000000..96607782 --- /dev/null +++ b/src/components/mui/formik-inputs/mui-formik-text-editor.js @@ -0,0 +1,34 @@ +import React from "react"; +import { useField } from "formik"; +import { InputLabel } from "@mui/material"; +import TextEditorV3 from "../../inputs/editor-input-v3"; +import { normalizeHtmlString } from "../../../utils/methods"; + +const FormikTextEditor = ({ name, label = "", options = {}, licence, ...props }) => { + const [field, meta, helpers] = useField(name); + const mergedOptions = { tabIndex: 0, allowTabNavigation: true, ...options }; + + return ( + <> + {label && ( + + {label} + + )} + { + const stringValue = normalizeHtmlString(e.target.value); + helpers.setValue(stringValue); + }} + error={meta.touched && meta.error} + license={licence} + {...props} + /> + + ); +}; + +export default FormikTextEditor; diff --git a/src/utils/methods.js b/src/utils/methods.js index e318325e..f685dac5 100644 --- a/src/utils/methods.js +++ b/src/utils/methods.js @@ -311,3 +311,9 @@ export const empty = (value) => { }; export const isSentryInitialized = () => typeof window !== "undefined" && !!window.SENTRY_DSN; + +export const normalizeHtmlString = (textInput) => { + if (!textInput) return ""; + const doc = new DOMParser().parseFromString(textInput, "text/html"); + return doc.body.textContent.trim().length === 0 ? "" : textInput; +}; \ No newline at end of file