You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.3 KiB
71 lines
2.3 KiB
import React, { FC } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import classNames from 'classnames'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
|
|
|
|
import { setFieldValue } from 'src/actions/forms'
|
|
import { getFieldValue, getFieldNotification } from 'src/selectors/forms'
|
|
import { notificationTypeToClassName } from 'src/utils'
|
|
import { AppState, FormNotification, ClassDictionary } from 'src/types'
|
|
|
|
interface SelectOptions {
|
|
[value: string]: string
|
|
}
|
|
|
|
interface Props {
|
|
name: string
|
|
label: string
|
|
options: SelectOptions
|
|
icon?: IconDefinition
|
|
}
|
|
|
|
const SelectField: FC<Props> = ({
|
|
name,
|
|
label,
|
|
options,
|
|
icon,
|
|
}) => {
|
|
const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
|
|
const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, name))
|
|
|
|
const dispatch = useDispatch()
|
|
|
|
const opts = Object.entries(options)
|
|
const controlClassDictionary: ClassDictionary = { control: true }
|
|
const helpClassDictionary: ClassDictionary = { help: true }
|
|
|
|
if (notification) {
|
|
const ncn = notificationTypeToClassName(notification.type)
|
|
|
|
controlClassDictionary[ncn] = true
|
|
helpClassDictionary[ncn] = true
|
|
}
|
|
|
|
if (icon) {
|
|
controlClassDictionary['has-icons-left'] = true
|
|
}
|
|
|
|
return (
|
|
<div className="field">
|
|
<label className="label">{label}</label>
|
|
<div className={classNames(controlClassDictionary)}>
|
|
<div className="select">
|
|
<select value={value} onChange={e => dispatch(setFieldValue(name, e.target.value))}>
|
|
{opts.map(([key, value]) => <option key={key} value={key}>{value}</option>)}
|
|
</select>
|
|
</div>
|
|
{icon &&
|
|
<div className="icon is-small is-left">
|
|
<FontAwesomeIcon icon={icon} />
|
|
</div>
|
|
}
|
|
</div>
|
|
{notification &&
|
|
<p className={classNames(helpClassDictionary)}>{notification.message}</p>
|
|
}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default SelectField
|