Skip to content

Instantly share code, notes, and snippets.

@EdwinGuzman
Last active May 22, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdwinGuzman/0feb7d682e5f6cd4ecd9860a82a6291d to your computer and use it in GitHub Desktop.
Save EdwinGuzman/0feb7d682e5f6cd4ecd9860a82a6291d to your computer and use it in GitHub Desktop.
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