Skip to content

Instantly share code, notes, and snippets.

@gor181
Created January 15, 2016 21:16
Show Gist options
  • Save gor181/2e86ef01e6aa790658e2 to your computer and use it in GitHub Desktop.
Save gor181/2e86ef01e6aa790658e2 to your computer and use it in GitHub Desktop.
Simple select for React.
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