Draft.js + React 16.6

A fiddle to repro issues in Draft.js

by Thibaud Colas

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.5/Draft.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.5/Draft.css">
<div id="react-root"></div>
<script>
  window.startContent = {
    "entityMap": {},
    "blocks": [{
        "key": "1fv8t",
        "text": "Unstyled",
        "type": "unstyled",
        "depth": 0,
        "inlineStyleRanges": [],
        "entityRanges": [],
        "data": {}
      },
      {
        "key": "f0898",
        "text": "List item",
        "type": "unordered-list-item",
        "depth": 0,
        "inlineStyleRanges": [],
        "entityRanges": [],
        "data": {}
      },
      {
        "key": "9qh91",
        "text": "Code block",
        "type": "code-block",
        "depth": 0,
        "inlineStyleRanges": [],
        "entityRanges": [],
        "data": {}
      }
    ]
  };

</script>

CSS

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

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

Babel + JSX

const {Editor, EditorState, RichUtils, convertFromRaw} = Draft;

class Container extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      editorState: EditorState.createWithContent(convertFromRaw(window.startContent))
    };
  }
  
  toggleBlock(type, e) {
    const { editorState } = this.state;
    this._handleChange(RichUtils.toggleBlockType(editorState, type));
  }
    
  render() {
    return (
      <div className="container-root">
        <button onClick={this.toggleBlock.bind(this, 'unstyled')}>P</button>
        <button onClick={this.toggleBlock.bind(this, 'code-block')}>Code</button>
        <button onClick={this.toggleBlock.bind(this, 'unordered-list-item')}>UL</button>
        <Editor 
          editorState={this.state.editorState}
          onChange={this._handleChange}
        />
      </div>
    );
  }
  _handleChange = (editorState) => {
    this.setState({editorState});
  }
}

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