Created
November 27, 2015 07:22
-
-
Save satoruk/cd944205c648471e9b9b to your computer and use it in GitHub Desktop.
React Text Field 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
import React, { Component, PropTypes } from 'react'; | |
const ENTER = 13; | |
export default class TextField extends Component { | |
static get displayName() { | |
return 'TextField'; | |
} | |
static get propTypes() { | |
return { | |
onEnterKeyUpWithoutIME: PropTypes.func | |
}; | |
} | |
get value() { | |
return this.refs.input.value; | |
} | |
set value(v) { | |
this.refs.input.value = v; | |
} | |
handleKeyDown(event) { | |
const { | |
onKeyDown = () => {} | |
} = this.props; | |
onKeyDown(event); | |
this.setState({keyDownCode: event.keyCode}); | |
} | |
handleKeyUp(event) { | |
const { | |
onEnterKeyUpWithoutIME = () => {}, | |
onKeyUp = () => {} | |
} = this.props; | |
onKeyUp(event); | |
if (event.keyCode === ENTER && this.state.keyDownCode === ENTER ) { | |
onEnterKeyUpWithoutIME(event); | |
} | |
} | |
render() { | |
const props = Object.assign({}, this.props, { | |
ref: 'input', | |
type:'text', | |
onKeyUp: this.handleKeyUp.bind(this), | |
onKeyDown: this.handleKeyDown.bind(this) | |
}); | |
delete props.onEnterKeyUpWithoutIME; | |
return <input {...props}/>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment