| 1 |
import React, { useMemo } from "react"; |
| 2 |
import ReactQuill from "react-quill"; |
| 3 |
import "react-quill/dist/quill.snow.css"; |
| 4 |
|
| 5 |
export interface RichTextEditorProps { |
| 6 |
value: string; |
| 7 |
onChange: (value: string) => void; |
| 8 |
label?: string; |
| 9 |
placeholder?: string; |
| 10 |
helperText?: string; |
| 11 |
disabled?: boolean; |
| 12 |
minHeight?: number; |
| 13 |
maxHeight?: number; |
| 14 |
} |
| 15 |
|
| 16 |
export const RichTextEditor: React.FC<RichTextEditorProps> = ({ |
| 17 |
value, |
| 18 |
onChange, |
| 19 |
label, |
| 20 |
placeholder, |
| 21 |
helperText, |
| 22 |
disabled = false, |
| 23 |
minHeight = 280, |
| 24 |
maxHeight = 600, |
| 25 |
}) => { |
| 26 |
const modules = useMemo( |
| 27 |
() => ({ |
| 28 |
toolbar: [ |
| 29 |
[{ header: [1, 2, 3, false] }], |
| 30 |
["bold", "italic", "underline", "strike"], |
| 31 |
[{ list: "ordered" }, { list: "bullet" }], |
| 32 |
[{ align: [] }], |
| 33 |
["link"], |
| 34 |
["clean"], |
| 35 |
], |
| 36 |
}), |
| 37 |
[], |
| 38 |
); |
| 39 |
|
| 40 |
const formats = [ |
| 41 |
"header", |
| 42 |
"bold", |
| 43 |
"italic", |
| 44 |
"underline", |
| 45 |
"strike", |
| 46 |
"list", |
| 47 |
"bullet", |
| 48 |
"align", |
| 49 |
"link", |
| 50 |
]; |
| 51 |
|
| 52 |
const handleChange = (content: string) => { |
| 53 |
// Normalize empty content |
| 54 |
const normalized = content === "<p><br></p>" ? "" : content; |
| 55 |
onChange(normalized); |
| 56 |
}; |
| 57 |
|
| 58 |
return ( |
| 59 |
<div className="w-full"> |
| 60 |
{label && ( |
| 61 |
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1.5"> |
| 62 |
{label} |
| 63 |
</label> |
| 64 |
)} |
| 65 |
|
| 66 |
<div |
| 67 |
className={`yatra-quill-editor ${disabled ? "opacity-60 cursor-not-allowed" : ""}`} |
| 68 |
> |
| 69 |
<ReactQuill |
| 70 |
theme="snow" |
| 71 |
value={value || ""} |
| 72 |
onChange={handleChange} |
| 73 |
modules={modules} |
| 74 |
formats={formats} |
| 75 |
placeholder={placeholder} |
| 76 |
readOnly={disabled} |
| 77 |
style={{ |
| 78 |
minHeight: `${minHeight}px`, |
| 79 |
maxHeight: `${maxHeight}px`, |
| 80 |
}} |
| 81 |
/> |
| 82 |
</div> |
| 83 |
|
| 84 |
{helperText && ( |
| 85 |
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400"> |
| 86 |
{helperText} |
| 87 |
</p> |
| 88 |
)} |
| 89 |
</div> |
| 90 |
); |
| 91 |
}; |
| 92 |
|
| 93 |
export default RichTextEditor; |
| 94 |
|