Last active
August 29, 2015 14:06
-
-
Save bultas/ea82ecc8925a37e3775e to your computer and use it in GitHub Desktop.
Simple React Input
This file contains hidden or 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
/** | |
* Copyright (c) 2015, Facebook, Inc. | |
* All rights reserved. | |
* | |
* This source code is licensed under the BSD-style license found in the | |
* LICENSE file in the root directory of this source tree. An additional grant | |
* of patent rights can be found in the PATENTS file in the same directory. | |
*/ | |
var React = require('react'); | |
var ReactPropTypes = React.PropTypes; | |
var ENTER_KEY_CODE = 13; | |
var TodoTextInput = React.createClass({ | |
propTypes: { | |
className: ReactPropTypes.string, | |
id: ReactPropTypes.string, | |
placeholder: ReactPropTypes.string, | |
onSave: ReactPropTypes.func.isRequired, | |
value: ReactPropTypes.string | |
}, | |
getInitialState: function() { | |
return { | |
value: this.props.value || '' | |
}; | |
}, | |
/** | |
* @return {object} | |
*/ | |
render: function() /*object*/ { | |
return ( | |
<input | |
className={this.props.className} | |
id={this.props.id} | |
placeholder={this.props.placeholder} | |
onBlur={this._save} | |
onChange={this._onChange} | |
onKeyDown={this._onKeyDown} | |
value={this.state.value} | |
autoFocus={true} | |
/> | |
); | |
}, | |
/** | |
* Invokes the callback passed in as onSave, allowing this component to be | |
* used in different ways. | |
*/ | |
_save: function() { | |
this.props.onSave(this.state.value); | |
this.setState({ | |
value: '' | |
}); | |
}, | |
/** | |
* @param {object} event | |
*/ | |
_onChange: function(/*object*/ event) { | |
this.setState({ | |
value: event.target.value | |
}); | |
}, | |
/** | |
* @param {object} event | |
*/ | |
_onKeyDown: function(event) { | |
if (event.keyCode === ENTER_KEY_CODE) { | |
this._save(); | |
} | |
} | |
}); | |
module.exports = TodoTextInput; |
This file contains hidden or 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 Input = React.createClass({ | |
getInitialState: function() { | |
return { | |
value: this.props.value || '' | |
}; | |
}, | |
componentWillReceiveProps: function(props) { | |
this.setState({ | |
value: props.value | |
}); | |
}, | |
render: function() { | |
return ( | |
<input value={this.state.value} placeholder={this.props.placeholder} onChange={this._change} /> | |
); | |
}, | |
_change: function(e) { | |
this.setState({ | |
value: e.target.value | |
}); | |
} | |
}); | |
module.exports = Input; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment