Skip to content

Instantly share code, notes, and snippets.

@JulienMelissas
Forked from keeth/Select.js
Last active September 12, 2017 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulienMelissas/f383d64243acba7c675935180ccc29f0 to your computer and use it in GitHub Desktop.
Save JulienMelissas/f383d64243acba7c675935180ccc29f0 to your computer and use it in GitHub Desktop.
A formsy-react wrapper around React Select (ES6)
import React from 'react';
import Formsy from 'formsy-react';
import ReactSelect from 'react-select';
// Dont forget to import your styles somewhere
const Select = React.createClass({
mixins: [Formsy.Mixin],
propTypes: {
name: React.PropTypes.string.isRequired,
placeholder: React.PropTypes.string,
multiple: React.PropTypes.bool,
options: React.PropTypes.array.isRequired
},
changeValue(value, selectedOptions) {
if (this.props.multiple) {
this.setValue(selectedOptions.map(option => option.value));
} else {
this.setValue(value);
}
},
render() {
var className = this.showRequired() ? 'required' : this.showError() ? 'error' : null;
var errorMessage = this.getErrorMessage();
return (
<div className={'form-group' + (className ? ' ' + className : '') }>
<label htmlFor={this.props.name}>{this.props.title}</label>
<ReactSelect
{...this.props}
ref="select"
onChange={this.changeValue}
value={this.getValue()}
/>
{errorMessage ? <span>{errorMessage}</span> : null}
</div>
);
}
});
export default Select;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment