Guest User

Untitled

a guest
Dec 18th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import { useEffect, useRef, forwardRef } from "react";
  2.  
  3. const RichTextEditorRHF = forwardRef(function RichTextEditorRHF(
  4. { value = "", height = "300px", rhfRegister, rhfValidationRules = "", rhfFieldName },
  5. ref
  6. ) {
  7. const textareaRef = useRef(null);
  8. const editorInstanceRef = useRef(null);
  9.  
  10. useEffect(() => {
  11. if (rhfValidationRules != "") rhfRegister(rhfFieldName, rhfValidationRules);
  12. else rhfRegister(rhfFieldName);
  13.  
  14. const textarea = textareaRef.current;
  15.  
  16. if (!textarea || editorInstanceRef.current) return;
  17.  
  18. const timeout = setTimeout(() => {
  19. const instance = window.sceditor.create(textarea, {
  20. format: "bbcode",
  21. toolbar:
  22. "bold,italic,underline,strike|left,center,right|size|bulletlist,orderedlist|link,unlink,image,youtube|quote|removeformat,maximize,source",
  23. style: "/sceditor/minified/themes/content/default.min.css",
  24. width: "100%",
  25. height: height,
  26. });
  27.  
  28. if (!instance) return;
  29.  
  30. editorInstanceRef.current = instance;
  31. }, 10);
  32.  
  33. return () => {
  34. clearTimeout(timeout);
  35. if (editorInstanceRef.current) {
  36. editorInstanceRef.current.destroy();
  37. editorInstanceRef.current = null;
  38. }
  39. };
  40. }, []);
  41.  
  42. return (
  43. <textarea
  44. ref={(node) => {
  45. textareaRef.current = node;
  46. if (typeof ref === "function") {
  47. ref(node);
  48. } else if (ref) {
  49. ref.current = node;
  50. }
  51. }}
  52. style={{ width: "100%", height }}
  53. defaultValue={value}
  54. />
  55. );
  56. });
  57.  
  58. export default RichTextEditorRHF;
Advertisement
Add Comment
Please, Sign In to add comment