Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { useControlField, useField } from "@carbon/form";
import { FormControl, FormErrorMessage } from "@carbon/react";
import { RichText as RichTextBase, useRichText } from "@carbon/react/RichText";
import type { ComponentProps } from "react";
import { useEffect } from "react";

type RichTextProps = Omit<ComponentProps<typeof RichTextBase>, "editor"> & {
  name: string;
  output?: "html" | "json" | "text";
};

const RichText = ({ name, output = "html", ...props }: RichTextProps) => {
  const { getInputProps, error, defaultValue } = useField(name);
  const [value, setValue] = useControlField<string>(name);
  const richText = useRichText(defaultValue);

  useEffect(() => {
    if (!value) {
      richText?.commands.clearContent(true);
    }
  }, [value, richText]);

  useEffect(() => {
    if (richText) {
      richText.on("update", () => {
        switch (output) {
          case "html":
            setValue(richText.getHTML());
            break;
          case "json":
            setValue(JSON.stringify(richText.getJSON()));
            break;
          case "text":
            setValue(richText.getText());
            break;
          default:
            setValue(richText.getHTML());
            break;
        }
      });
    }

    return () => {
      if (richText) {
        richText.off("update");
      }
    };
  }, [richText, output, setValue]);

  return (
    <FormControl isInvalid={!!error}>
      <RichTextBase {...props} editor={richText} />
      <input
        {...getInputProps({
          id: name
        })}
        value={value}
        type="hidden"
      />
      {error && <FormErrorMessage>{error}</FormErrorMessage>}
    </FormControl>
  );
};

export default RichText;