Draft.js editor initiate with unordered list items
Draft.js editor initiate with unordered list items (for stackoverflow answer)
by Mikhail Shabrikov
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.7.0/Draft.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.0/Draft.js"></script>
<!-- Created for this stackoverflow answers - https://stackoverflow.com/a/47030806/5806646 , https://stackoverflow.com/a/46983796/5806646 -->
<div id="react-root"></div>
CSS
body {
font-family: Helvetica, sans-serif;
}
.container-root {
border: 1px solid black;
padding: 5px;
margin: 5px;
}
Babel + JSX
const {Editor, CharacterMetadata, RichUtils, ContentBlock, genKey, ContentState, EditorState} = Draft;
const { List, Repeat } = Immutable;
class Container extends React.Component {
constructor(props) {
super(props);
const input = ['foo', 'bar', 'baz'];
const contentBlocksArray = input.map(word => {
return new ContentBlock({
key: genKey(),
type: 'unordered-list-item',
characterList: new List(Repeat(CharacterMetadata.create(), word.length)),
text: word
});
});
this.state = {
editorState: EditorState.createWithContent(ContentState.createFromBlockArray(contentBlocksArray))
};
}
_handleChange = (editorState) => {
this.setState({ editorState });
}
render() {
return (
<div className="container-root">
<Editor
placeholder="Type"
editorState={this.state.editorState}
onChange={this._handleChange}
/>
</div>
);
}
}
ReactDOM.render(<Container />, document.getElementById('react-root'))