Draft.js editor initiate with custom component

Draft.js editor i initiate with custom component (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>
<div id="react-root"></div>

CSS

body {
  font-family: Helvetica, sans-serif;
}

.container-root {
  border: 1px solid black;
  padding: 5px;
  margin: 5px;
}

.my-custom-block {
  background-color: cadetblue;
  margin: 15px 0;
  font-size: 16px;
  position: relative;
  padding-left: 4px;
  border-left: 9px solid lightblue;
}

Babel + JSX

const {Editor, CharacterMetadata, DefaultDraftBlockRenderMap, ContentBlock, EditorBlock, genKey, ContentState, EditorState} = Draft;
const { List, Map, Repeat } = Immutable;

class MyCustomBlock extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className='my-custom-block'>
        {/* here, this.props.children contains a <section> container, as that was the matching element */}
        <EditorBlock {...this.props} />
      </div>
    );
  }
}

function blockRendererFn(contentBlock) {
  const type = contentBlock.getType();
  if (type === 'MyCustomBlock') {
    return {
      component: MyCustomBlock,
      props: {},
    };
  }
}

const RenderMap = new Map({
  MyCustomBlock: {
    element: 'div',
  }
}).merge(DefaultDraftBlockRenderMap);

const extendedBlockRenderMap = Draft.DefaultDraftBlockRenderMap.merge(RenderMap);

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: 'MyCustomBlock',
        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"
          blockRenderMap={extendedBlockRenderMap}
          blockRendererFn={blockRendererFn}
          editorState={this.state.editorState}
          onChange={this._handleChange}
        />
      </div>
    );
  }
}

ReactDOM.render(<Container />, document.getElementById('react-root'))