Created
September 24, 2018 08:18
-
-
Save maxkoretskyi/fb8c630b86a772622bf8ac55dd623b76 to your computer and use it in GitHub Desktop.
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
import React, { Component } from 'react'; | |
export class NumericCellEditor extends Component { | |
constructor(props) { | |
super(props); | |
this.textInput = React.createRef(); | |
} | |
onKeyPress(event) { | |
if (!isNumeric(event.nativeEvent)) { | |
event.preventDefault(); | |
} | |
function isNumeric(event) { | |
return /\d/.test(event.key); | |
} | |
} | |
onKeyDown(event) { | |
if (event.keyCode === 39 || event.keyCode === 37) { | |
event.stopPropagation(); | |
} | |
} | |
afterGuiAttached() { | |
if (this.textInput) this.textInput.current.focus(); | |
}; | |
getValue() { | |
return this.textInput.current.value; | |
}; | |
componentDidMount() { | |
this.textInput.current.addEventListener('keydown', this.onKeyDown); | |
} | |
render() { | |
return ( | |
<input onKeyPress={this.onKeyPress} ref={this.textInput} defaultValue={this.props.value}/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment