Skip to content
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
7 changes: 6 additions & 1 deletion src/components/inputs/formik-text-editor.js
Original file line number Diff line number Diff line change
@@ -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 } =
Expand All @@ -11,7 +12,11 @@ const FormikTextEditor = ({ name, ...props }) => {
name={name}
id={name}
value={values[name]}
onChange={(e) => setFieldValue(name, e.target.value)}
onChange={(e) => {
Copy link

@smarcet smarcet Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niko-exo seems that comment got truncated
let move this to an util method and lets take care of following corner cases
const normalizeJoditEmpty = (html) => { if (!html) return ""; const v = html.trim(); return (v === "<p><br></p>" || v === "<p><br/></p>" || v === "<p>&nbsp;</p>" || v === "<p></p>") ? "" : html; };

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}
Expand Down
14 changes: 14 additions & 0 deletions src/components/inputs/utils/__tests__/normalizeJoditEmpty.test.js
Original file line number Diff line number Diff line change
@@ -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 = "<p><br></p>";
expect(normalizeJoditEmpty(inputString)).toBe("");
});

it("normalizes a string with content is returned without modifications", () => {
const inputString = "<p>This is a content strign</p>";
expect(normalizeJoditEmpty(inputString)).toBe(inputString);
});
});
6 changes: 6 additions & 0 deletions src/components/inputs/utils/normalizeJoditEmpty.js
Original file line number Diff line number Diff line change
@@ -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;