[ABANDONED] React/Redux front end for the Flexor social network.
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.
 
 

69 lines
2.0 KiB

import React, { FC } from 'react'
import classNames from 'classnames'
import noop from 'lodash/noop'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
import { notificationTypeToClassName } from 'src/utils'
import { FormNotification, ClassDictionary } from 'src/types'
interface SelectOptions {
[value: string]: string
}
export interface Props {
name: string
label: string
options: SelectOptions
value?: string
notification?: FormNotification
icon?: IconDefinition
setValue?: (value: string) => void
}
const SelectField: FC<Props> = ({
label,
options,
value,
notification,
icon,
setValue = noop,
}) => {
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 => setValue(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