Created
June 25, 2014 13:50
-
-
Save anonymous/92d4f669b28ec717bb08 to your computer and use it in GitHub Desktop.
How to use Twitter Typeahead with react
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 states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', | |
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', | |
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', | |
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', | |
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', | |
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', | |
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', | |
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', | |
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' | |
]; | |
var TypeaheadInput = React.createClass({ | |
componentDidMount: function () { | |
$(this.refs.input.getDOMNode()).typeahead( | |
{ hint: true, highlight: true, minLength: 1 }, | |
{ name: 'states', displayKey: 'value', source: substringMatcher(this.props.options)} | |
) | |
.on("typeahead:autocompleted",this.props.onAutocompleted); | |
}, | |
render: function() { | |
return ( <input type="text" ref="input" /> ); | |
} | |
}); | |
var ExampleApplication = React.createClass({ | |
render: function() { | |
var bls = function () { console.log("bla"); } | |
return ( | |
<div> | |
<TypeaheadInput onAutocompleted={bls} options={states} /> | |
</div> | |
); | |
} | |
}); | |
React.renderComponent( <ExampleApplication />, document.getElementById('container')); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv='Content-type' content='text/html; charset=utf-8'> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Typeahead</title> | |
<script src="jquery-2.1.1.min.js"></script> | |
<script src="typeahead.jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="container"> | |
</div> | |
<script src="react-with-addons.min.js"></script> | |
<script src="JSXTransformer.js"></script> | |
<script type="text/jsx" src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment