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.
61 lines
1.9 KiB
61 lines
1.9 KiB
import React, { FC, FocusEventHandler } from 'react'
|
|
import classNames from 'classnames'
|
|
import noop from 'lodash/noop'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { IconDefinition } from '@fortawesome/fontawesome-common-types'
|
|
|
|
import { notificationTypeToClassName } from 'src/utils'
|
|
import { FormNotification, ClassDictionary } from 'src/types'
|
|
|
|
export interface Props {
|
|
name: string
|
|
label: string
|
|
type?: 'text' | 'email'
|
|
placeholder?: string
|
|
icon?: IconDefinition
|
|
value?: string
|
|
notification?: FormNotification
|
|
setValue?: (value: string) => void
|
|
onBlur?: FocusEventHandler
|
|
}
|
|
|
|
const TextField: FC<Props> = ({
|
|
label,
|
|
type = 'text',
|
|
placeholder,
|
|
icon,
|
|
value = '',
|
|
notification,
|
|
setValue = noop,
|
|
onBlur = noop,
|
|
}) => {
|
|
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) => setValue(e.target.value)} onBlur={onBlur} />
|
|
{icon &&
|
|
<span className="icon is-small is-left">
|
|
<FontAwesomeIcon icon={icon} />
|
|
</span>
|
|
}
|
|
</div>
|
|
{notification &&
|
|
<p className={classNames(helpClassDictionary)}>{notification.message}</p>
|
|
}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default TextField
|