Last active
May 22, 2018 17:07
-
-
Save EdwinGuzman/0feb7d682e5f6cd4ecd9860a82a6291d to your computer and use it in GitHub Desktop.
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'; | |
class Select extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
selected: '', | |
}; | |
this.onChange = this.onChange.bind(this); | |
this.getOptions = this.getOptions.bind(this); | |
} | |
onChange(e) { | |
const selected = e.target.value; | |
this.setState({ selected }); | |
} | |
// Takes in an array of option objects { value: '', label: ''} | |
getOptions(options) { | |
return options.map(option => | |
<option value={option.value} key={option.value}>{option.label}</option>); | |
} | |
render() { | |
const { id, options, selectLabel } = this.props; | |
const { selected } = this.state; | |
return ( | |
<fieldset class="nypl-select-field"> | |
<label for={id}>{selectLabel}</label> | |
<select id={id} name={id} value={this.state.selected} onChange={this.onChange}> | |
{this.getOptions(options)} | |
</select> | |
</fieldset> | |
); | |
} | |
} | |
Select.propTypes = { | |
id: PropTypes.string, | |
selectLabel: PropTypes.string, | |
array: PropTypes.shape({ | |
value: PropTypes.string, | |
label: PropTypes.string, | |
}), | |
}; | |
export default Select; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment