I am trying to render dynamic html content coming from server side, in react-draft-wysiwyg which has <table>
tag too.
I tried so many things, here is my code, if you could help me please.
I tried some other text editors too, but that to behaving same for example react-quill.
expected output:
Actual Output (Everything is rendering in a single line, not in table format)
import React, { Component } from 'react';
import {
EditorState,
convertToRaw,
ContentState,
convertFromHTML,
} from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
export default class EditorConvertToHTML extends Component {
constructor(props) {
super(props);
const html = ` <div> some html content here...
<table style=\"height: 74px; width: 644px;\" border=\"0\">
<tbody>
<tr>
<td><strong>Customer</strong></td>
<td><strong>Customer PO</strong></td>
</tr>
<tr>
<td><span>Customer name</span></td>
<td><span>po number</span></td></tr> <tr>
<td><strong>Customer Complaint Number</strong></td>
<td><strong>Invoice Number</strong></td>
<td> </td>
</tr> <tr>
<td><span></span></td>
<td><span>other value</span></td>
<td> </td>
</tr>
</tbody>
</table> </div>
some more html content ... `;
//tried this
const contentBlocks = convertFromHTML(html);
// and this too
// const contentBlocks = htmlToDraft(html);
if (contentBlocks) {
const contentState = ContentState.createFromBlockArray(
contentBlocks.contentBlocks
);
const editorState = EditorState.createWithContent(contentState);
this.state = {
editorState,
};
}
}
onEditorStateChange: Function = (editorState) => {
this.setState({
editorState,
});
};
render() {
const { editorState } = this.state;
return (
<div>
<Editor
editorState={editorState}
wrapperClassName="demo-wrapper"
editorClassName="demo-editor"
onEditorStateChange={this.onEditorStateChange}
/>
<textarea
disabled
value={draftToHtml(convertToRaw(editorState.getCurrentContent()))}
/>
</div>
);
}
}