diff --git a/src/components/inputs/formik-text-editor.js b/src/components/inputs/formik-text-editor.js index 5cc4d0dc9..346fb23cb 100644 --- a/src/components/inputs/formik-text-editor.js +++ b/src/components/inputs/formik-text-editor.js @@ -1,6 +1,7 @@ import React from "react"; import TextEditorV3 from "openstack-uicore-foundation/lib/components/inputs/editor-input-v3"; import { useFormikContext } from "formik"; +import normalizeJoditEmpty from "./utils/normalizeJoditEmpty"; const FormikTextEditor = ({ name, ...props }) => { const { values, errors, touched, setFieldValue, setFieldTouched } = @@ -11,7 +12,11 @@ const FormikTextEditor = ({ name, ...props }) => { name={name} id={name} value={values[name]} - onChange={(e) => setFieldValue(name, e.target.value)} + onChange={(e) => { + const stringValue = normalizeJoditEmpty(e.target.value); + + setFieldValue(name, stringValue); + }} onBlur={() => setFieldTouched(name, true)} error={touched?.[name] && errors?.[name] ? errors?.[name] : ""} license={process.env.JODIT_LICENSE_KEY} diff --git a/src/components/inputs/utils/__tests__/normalizeJoditEmpty.test.js b/src/components/inputs/utils/__tests__/normalizeJoditEmpty.test.js new file mode 100644 index 000000000..fe9e66ec6 --- /dev/null +++ b/src/components/inputs/utils/__tests__/normalizeJoditEmpty.test.js @@ -0,0 +1,14 @@ +import { expect, describe, it } from "@jest/globals"; +import normalizeJoditEmpty from "../normalizeJoditEmpty"; + +describe("normalizeJoditEmpty", () => { + it("normalizes an empty string that is surrounded by html tags", () => { + const inputString = "


"; + expect(normalizeJoditEmpty(inputString)).toBe(""); + }); + + it("normalizes a string with content is returned without modifications", () => { + const inputString = "

This is a content strign

"; + expect(normalizeJoditEmpty(inputString)).toBe(inputString); + }); +}); diff --git a/src/components/inputs/utils/normalizeJoditEmpty.js b/src/components/inputs/utils/normalizeJoditEmpty.js new file mode 100644 index 000000000..9816d54e5 --- /dev/null +++ b/src/components/inputs/utils/normalizeJoditEmpty.js @@ -0,0 +1,6 @@ +const normalizeJoditEmpty = (textInput) => { + const doc = new DOMParser().parseFromString(textInput, "text/html"); + return doc.body.textContent.length === 0 ? "" : textInput; +}; + +export default normalizeJoditEmpty;