Last active
March 27, 2018 21:10
-
-
Save milnomada/0f025a7eabf23122b5512b495a39e486 to your computer and use it in GitHub Desktop.
React Component for react-select module
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
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import Select from 'react-select'; | |
const defaultValues = [ | |
{ label: 'Chocolate', value: 'chocolate' }, | |
{ label: 'Vanilla', value: 'vanilla' }, | |
{ label: 'Strawberry', value: 'strawberry' }, | |
{ label: 'Caramel', value: 'caramel' }, | |
{ label: 'Cookies and Cream', value: 'cookiescream' }, | |
{ label: 'Peppermint', value: 'peppermint' }, | |
]; | |
class AutoSelect extends React.Component { | |
static propTypes = { | |
label: PropTypes.string, | |
} | |
constructor(props) { | |
super(props); | |
this.state = { | |
removeSelected: true, | |
disabled: false, | |
stayOpen: false, | |
value: [], | |
rtl: false | |
} | |
} | |
handleSelectChange (value) { | |
console.log('You\'ve selected:', value); | |
this.setState({ value }); | |
} | |
toggleCheckbox (e) { | |
this.setState({ | |
[e.target.name]: e.target.checked, | |
}); | |
} | |
toggleRtl(e) { | |
let rtl = e.target.checked; | |
this.setState({ rtl }); | |
} | |
render() { | |
const { disabled, stayOpen, value } = this.state; | |
const options = defaultValues; | |
return ( | |
<div className="section"> | |
<Select | |
closeOnSelect={!stayOpen} | |
disabled={disabled} | |
multi | |
onChange={(v)=>this.handleSelectChange(v)} | |
options={options} | |
placeholder="Select your favourite(s)" | |
removeSelected={this.state.removeSelected} | |
rtl={this.state.rtl} | |
simpleValue | |
value={value} | |
/> | |
<div className="checkbox-list"> | |
<label className="checkbox"> | |
<input type="checkbox" className="checkbox-control" name="removeSelected" checked={this.state.removeSelected} onChange={(e)=>this.toggleCheckbox(e)} /> | |
<span className="checkbox-label">Remove selected options</span> | |
</label> | |
<label className="checkbox"> | |
<input type="checkbox" className="checkbox-control" name="disabled" checked={this.state.disabled} onChange={(e)=>this.toggleCheckbox(e)} /> | |
<span className="checkbox-label">Disable the control</span> | |
</label> | |
<label className="checkbox"> | |
<input type="checkbox" className="checkbox-control" name="stayOpen" checked={stayOpen} onChange={(e)=>this.toggleCheckbox(e)}/> | |
<span className="checkbox-label">Stay open when an Option is selected</span> | |
</label> | |
<label className="checkbox"> | |
<input type="checkbox" className="checkbox-control" name="rtl" checked={this.state.rtl} onChange={(e)=>this.toggleRtl(e)} /> | |
<span className="checkbox-label">rtl</span> | |
</label> | |
</div> | |
</div> | |
); | |
} | |
} | |
export default AutoSelect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adapt to ES6 Component, exports, props and methods