Skip to content

Instantly share code, notes, and snippets.

@MoOx
Created July 29, 2014 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoOx/f97b11b95cb727921db0 to your computer and use it in GitHub Desktop.
Save MoOx/f97b11b95cb727921db0 to your computer and use it in GitHub Desktop.
React clearable input
/**
* <InputClearable />
*
* @es6
* @jsx React.DOM
*/
module React from "react"
import IconSvg from "../iconsvg"
export default React.createClass({
getInitialState() {
return {
value: this.props.value || ""
}
},
render() {
return (
<span className="InputClearable">
{this.transferPropsTo(<input ref="input" className={"InputClearable-input"} value={this.state.value} onChange={this.handleChange}/>)}
<IconSvg key="InputClearable-clearButton" className={"InputClearable-clearButton"} hidden={!this.state.value} onClick={this.clearValue} />
</span>
)
},
clearValue(e) {
this.setState({value: ""}, function () {
// call onChange event
// this.props.onChange(e) // INFINITE LOOP DUDE, OBVIOUSLY
// give focus back
this.refs.input.getDOMNode().focus()
})
},
handleChange(e) {
this.setState({value: e.currentTarget.value}, function() {
console.log("on change dude")
this.props.onChange(e)
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment