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.4 KiB
71 lines
2.4 KiB
import React, { FC, FocusEventHandler } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import classNames from 'classnames'
|
|
import noop from 'lodash/noop'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { IconDefinition } from '@fortawesome/fontawesome-common-types'
|
|
|
|
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 Props {
|
|
name: string
|
|
label: string
|
|
type?: 'text' | 'email'
|
|
placeholder?: string
|
|
help?: string
|
|
icon?: IconDefinition
|
|
onBlur?: FocusEventHandler<HTMLInputElement>
|
|
}
|
|
|
|
const TextField: FC<Props> = ({
|
|
name,
|
|
label,
|
|
type = 'text',
|
|
placeholder,
|
|
help,
|
|
icon,
|
|
onBlur = noop,
|
|
}) => {
|
|
const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
|
|
const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, name))
|
|
|
|
const dispatch = useDispatch()
|
|
|
|
const controlClassDictionary = { control: true, 'has-icons-left': !!icon }
|
|
const helpClassDictionary: ClassDictionary = { help: true }
|
|
const inputClassDictionary: ClassDictionary = { input: true }
|
|
|
|
if (notification) {
|
|
const ncn = notificationTypeToClassName(notification.type)
|
|
|
|
helpClassDictionary[ncn] = true
|
|
inputClassDictionary[ncn] = true
|
|
}
|
|
|
|
return (
|
|
<div className="field">
|
|
<label className="label">{label}</label>
|
|
<div className={classNames(controlClassDictionary)}>
|
|
<input
|
|
className={classNames(inputClassDictionary)}
|
|
type={type}
|
|
placeholder={placeholder}
|
|
value={value}
|
|
onChange={e => dispatch(setFieldValue(name, e.target.value))}
|
|
onBlur={onBlur} />
|
|
{icon &&
|
|
<span className="icon is-small is-left">
|
|
<FontAwesomeIcon icon={icon} />
|
|
</span>
|
|
}
|
|
</div>
|
|
{(!notification && help) && <p className="help">{help}</p>}
|
|
{notification && <p className={classNames(helpClassDictionary)}>{notification.message}</p>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default TextField
|