Adding Event Handlers
Last updated
class App extends React.Component {
state = {
value: initialValue,
}
onChange = ({ value }) => {
this.setState({ value })
}
// Define a new handler which prints the key that was pressed.
onKeyDown = (event, editor, next) => {
console.log(event.key)
return next()
}
render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
)
}
}class App extends React.Component {
state = {
value: initialValue,
}
onChange = ({ value }) => {
this.setState({ value })
}
onKeyDown = (event, editor, next) => {
// Return with no changes if the keypress is not '&'
if (event.key !== '&') return next()
// Prevent the ampersand character from being inserted.
event.preventDefault()
// Change the value by inserting 'and' at the cursor's position.
editor.insertText('and')
}
render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
)
}
}