gab-social/app/javascript/gabsocial/components/select.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
2020-03-07 04:53:28 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
2020-04-23 07:13:29 +01:00
import Icon from './icon'
2020-03-07 04:53:28 +00:00
2020-04-23 07:13:29 +01:00
/**
* Renders a select element with options
* @param {func} props.onChange - function to call on option selection
* @param {object} props.options - options for selection
* @param {string} [props.value] - value to set selected
*/
class Select extends ImmutablePureComponent {
2020-03-07 04:53:28 +00:00
2020-04-23 07:13:29 +01:00
updateOnProps = [
'options',
'value',
]
2020-03-07 04:53:28 +00:00
render() {
const {
value,
options,
2020-04-23 07:13:29 +01:00
onChange,
2020-03-07 04:53:28 +00:00
} = this.props
return (
<div className={_s._}>
2020-03-07 04:53:28 +00:00
<select
className={[_s._, _s.outlineNone, _s.text, _s.border1PX, _s.borderColorSecondary, _s.px15, _s.select, _s.fs14PX].join(' ')}
2020-03-07 04:53:28 +00:00
value={value}
onChange={onChange}
>
{
2020-04-23 07:13:29 +01:00
options.map((option) => (
2020-03-07 04:53:28 +00:00
<option key={`option-${option.value}`} value={option.value}>
{option.title}
</option>
))
}
</select>
2020-04-23 07:13:29 +01:00
<Icon
id='select'
size='14px'
className={[_s.cSecondary, _s.posAbs, _s.right0, _s.mr10, _s.bottom0, _s.mb15].join(' ')}
2020-04-23 07:13:29 +01:00
/>
2020-03-07 04:53:28 +00:00
</div>
)
}
}
Select.propTypes = {
onChange: PropTypes.func.isRequired,
options: PropTypes.oneOf([
ImmutablePropTypes.map,
PropTypes.object,
]).isRequired,
value: PropTypes.string,
}
export default Select