Created
April 26, 2018 20:12
-
-
Save WillSams/5479cd5edcc75c8b52e13acbd275f824 to your computer and use it in GitHub Desktop.
React-Wysiwyg via extend React.Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react') | |
var ReactDOM = require('react-dom') | |
var ContentEditable = require('react-wysiwyg') | |
class Example extends React.Component { | |
constructor(props){ | |
super(props) | |
this.state = { | |
text: '', | |
editing: false | |
} | |
} | |
render(){ | |
return ( | |
<div> | |
<ContentEditable | |
tagName='div' | |
className='BlogCommentBox' | |
onChange={this.onChange} | |
text={this.state.text} | |
placeholder='Your Name' | |
autofocus={true} | |
maxLength={200} | |
editing={this.state.editing} | |
/> | |
<button onClick={this.enableEditing}> | |
Enable Editing | |
</button> | |
</div> | |
) | |
} | |
onChange(text) { | |
// in order to render the updated text, | |
// you need to pass it as a prop to contentEditable. | |
// This gives you increased flexibility. | |
this.setState({ text: text }); | |
} | |
enableEditing(){ | |
// set your contenteditable field into editing mode. | |
this.setState({ editing: true }); | |
} | |
} | |
document.addEventListener('DOMContentLoaded', function() { | |
var elm = document.getElementById('myDiv') | |
if (elm) { ReactDOM.render(<Example />, elm) } | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment