Created
January 15, 2016 21:16
-
-
Save gor181/2e86ef01e6aa790658e2 to your computer and use it in GitHub Desktop.
Simple select for React.
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
const React = require('react'); | |
const classnames = require('classnames'); | |
const { find, map } = require('lodash'); | |
module.exports = React.createClass({ | |
displayName: 'Select', | |
propTypes: { | |
types: React.PropTypes.arrayOf( | |
React.PropTypes.shape({ | |
label: React.PropTypes.string, | |
onClick: React.PropTypes.func | |
}).isRequired | |
).isRequired | |
}, | |
_onChange(evnt) { | |
const { types } = this.props; | |
const selectedType = find(types, { id: evnt.target.value }); | |
selectedType && selectedType.onClick(); | |
}, | |
render() { | |
const { types, className, label } = this.props; | |
return ( | |
<div className={classnames('form-group', className)}> | |
<label> | |
{label} | |
<select id='sortBy' name='sortBy' className='form-control' onChange={this._onChange}> | |
{ map(types, (type, index) => <option value={type.id} key={index}>{type.label}</option>) } | |
</select> | |
</label> | |
</div> | |
); | |
} | |
}); | |
//Usage: | |
React.createClass({ | |
render() { | |
return( | |
<Select | |
className='pull-right sortBy' | |
types={[ | |
{ id: 'lower', label: 'Price: lower first', onClick: () => console.log('lower') }, | |
{ id: 'higher', label: 'Price: higher first', onClick: () => console.log('higher') } | |
]} | |
/> | |
); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment