Skip to content

Instantly share code, notes, and snippets.

@maxkoretskyi
Created September 24, 2018 08:18
Show Gist options
  • Save maxkoretskyi/fb8c630b86a772622bf8ac55dd623b76 to your computer and use it in GitHub Desktop.
Save maxkoretskyi/fb8c630b86a772622bf8ac55dd623b76 to your computer and use it in GitHub Desktop.
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