2019-06-23 06:25:38 +00:00
|
|
|
import React from 'react';
|
2020-02-08 23:23:08 +00:00
|
|
|
import { Field as FormikField, FieldProps } from 'formik';
|
2019-06-23 06:25:38 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2019-12-16 00:41:20 +00:00
|
|
|
interface OwnProps {
|
2019-06-23 06:25:38 +00:00
|
|
|
name: string;
|
|
|
|
label?: string;
|
|
|
|
description?: string;
|
|
|
|
validate?: (value: any) => undefined | string | Promise<any>;
|
|
|
|
}
|
|
|
|
|
2019-12-16 00:41:20 +00:00
|
|
|
type Props = OwnProps & Omit<React.InputHTMLAttributes<HTMLInputElement>, 'name'>;
|
|
|
|
|
2020-02-08 23:23:08 +00:00
|
|
|
const Field = ({ id, name, label, description, validate, className, ...props }: Props) => (
|
|
|
|
<FormikField name={name} validate={validate}>
|
2019-06-23 06:25:38 +00:00
|
|
|
{
|
|
|
|
({ field, form: { errors, touched } }: FieldProps) => (
|
|
|
|
<React.Fragment>
|
|
|
|
{label &&
|
|
|
|
<label htmlFor={id} className={'input-dark-label'}>{label}</label>
|
|
|
|
}
|
|
|
|
<input
|
|
|
|
id={id}
|
|
|
|
{...field}
|
2019-12-16 00:41:20 +00:00
|
|
|
{...props}
|
|
|
|
className={classNames((className || 'input-dark'), {
|
2019-06-23 06:25:38 +00:00
|
|
|
error: touched[field.name] && errors[field.name],
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
{touched[field.name] && errors[field.name] ?
|
2019-12-16 00:41:20 +00:00
|
|
|
<p className={'input-help error'}>
|
2019-06-23 06:25:38 +00:00
|
|
|
{(errors[field.name] as string).charAt(0).toUpperCase() + (errors[field.name] as string).slice(1)}
|
|
|
|
</p>
|
|
|
|
:
|
|
|
|
description ? <p className={'input-help'}>{description}</p> : null
|
|
|
|
}
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
2020-02-08 23:23:08 +00:00
|
|
|
</FormikField>
|
2019-06-23 06:25:38 +00:00
|
|
|
);
|
2020-02-08 23:23:08 +00:00
|
|
|
|
|
|
|
export default Field;
|