Created
July 18, 2015 00:54
-
-
Save muratsu/25588ed0a056b7b8a129 to your computer and use it in GitHub Desktop.
react-searchbar
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
var Bacon = require('baconjs').Bacon; | |
var SearchBar = React.createClass({ | |
propTypes: { | |
// initialValue: React.PropTypes.string.isRequired, | |
placeHolderText: React.PropTypes.string.isRequired, | |
onUserInput: React.PropTypes.func.isRequired | |
}, | |
// getInitialState: function() { | |
// return { textValue: this.props.initialValue } | |
// }, | |
componentDidMount: function() { | |
var self = this, | |
delay = 200, // in ms | |
inputElem = React.findDOMNode(this.refs.filterTextInput); | |
// Convert keydown events to stream | |
var text = Bacon.fromEvent(inputElem, 'keydown') | |
.debounce(delay) | |
.map(function(event) { | |
return event.target.value; | |
}) | |
.skipDuplicates(); | |
var textObservable = text.flatMapLatest(function(query) { | |
return Bacon.once(query); | |
}); | |
textObservable.onValue(function(val) { | |
self.props.onUserInput(val); | |
}); | |
}, | |
render: function() { | |
return ( | |
<div className="col-xs-offset-2 col-xs-8"> | |
<div id="searchwrapper"> | |
<input | |
className="searchBox" | |
type="search" | |
autoComplete="off" | |
placeholder={this.props.placeHolderText} | |
value="this" | |
ref="filterTextInput" | |
/> | |
</div> | |
</div> | |
); | |
} | |
}); | |
module.exports = SearchBar; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment