File size: 1,312 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class Select extends Component {
static propTypes = {
cx: PropTypes.func.isRequired,
id: PropTypes.string,
onSelect: PropTypes.func.isRequired,
items: PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.string,
disabled: PropTypes.bool,
})
).isRequired,
selectedItem: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
};
onChange = (e) => {
this.props.onSelect(e.target.value);
};
render() {
const { cx, id, items, selectedItem } = this.props;
return (
<select
id={id}
className={cx('select')}
value={selectedItem}
onChange={this.onChange}
>
{items.map((item) => (
<option
className={cx('option')}
key={item.key === undefined ? item.value : item.key}
disabled={item.disabled}
value={item.value}
>
{item.label === undefined ? item.value : item.label}
</option>
))}
</select>
);
}
}
|