Skip to content

Instantly share code, notes, and snippets.

@vgoklani
Forked from andyl/minimal typeahead
Created December 27, 2015 04:20
Show Gist options
  • Save vgoklani/92ab0e67863bc42cf963 to your computer and use it in GitHub Desktop.
Save vgoklani/92ab0e67863bc42cf963 to your computer and use it in GitHub Desktop.
R = React.DOM
nations = ['britain', 'ireland', 'norway', 'sweden', 'denmark', 'germany',
'holland', 'belgium', 'france', 'spain', 'portugal', 'italy', 'switzerland']
Typeahead = React.createClass
getInitialState : -> {input: ""}
handleChange : -> @setState input: @refs.field.getDOMNode().value
handleClick : (nation)-> @setState input: nation
matches : (input)->
regex = new RegExp(input, "i")
_.select nations, (nation)-> nation.match(regex) && nation != input
render: ->
R.div {},
R.input {ref:'field', onChange: @handleChange, value: @state.input}
R.br {}
_.map @matches(@state.input), (nation)=>
Button {handleClick: @handleClick, name: nation}
Button = React.createClass
onClick: -> @props.handleClick(@props.name)
render: ->
classes = "btn btn-xs btn-default"
R.button {className: classes, onClick: @onClick}, @props.name
$(document).ready ->
React.render React.createFactory(Typeahead)(), $('#typetest')[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment