2020-07-04 22:19:46 +00:00
|
|
|
import React, { forwardRef } from 'react';
|
2020-02-08 23:23:08 +00:00
|
|
|
import { Field as FormikField, FieldProps } from 'formik';
|
2020-07-04 16:13:41 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
2020-07-04 19:39:55 +00:00
|
|
|
import Label from '@/components/elements/Label';
|
2019-06-23 06:25:38 +00:00
|
|
|
|
2019-12-16 00:41:20 +00:00
|
|
|
interface OwnProps {
|
2019-06-23 06:25:38 +00:00
|
|
|
name: string;
|
2020-03-28 22:42:53 +00:00
|
|
|
light?: boolean;
|
2019-06-23 06:25:38 +00:00
|
|
|
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-07-05 01:46:09 +00:00
|
|
|
const Field = forwardRef<HTMLInputElement, Props>(({ id, name, light = false, label, description, validate, ...props }, ref) => (
|
2020-07-04 22:19:46 +00:00
|
|
|
<FormikField innerRef={ref} name={name} validate={validate}>
|
2019-06-23 06:25:38 +00:00
|
|
|
{
|
|
|
|
({ field, form: { errors, touched } }: FieldProps) => (
|
2020-07-05 01:46:09 +00:00
|
|
|
<>
|
2019-06-23 06:25:38 +00:00
|
|
|
{label &&
|
2020-07-04 19:39:55 +00:00
|
|
|
<Label htmlFor={id} isLight={light}>{label}</Label>
|
2019-06-23 06:25:38 +00:00
|
|
|
}
|
2020-07-04 16:13:41 +00:00
|
|
|
<Input
|
2019-06-23 06:25:38 +00:00
|
|
|
id={id}
|
|
|
|
{...field}
|
2019-12-16 00:41:20 +00:00
|
|
|
{...props}
|
2020-07-04 16:13:41 +00:00
|
|
|
isLight={light}
|
|
|
|
hasError={!!(touched[field.name] && errors[field.name])}
|
2019-06-23 06:25:38 +00:00
|
|
|
/>
|
|
|
|
{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
|
|
|
|
}
|
2020-07-05 01:46:09 +00:00
|
|
|
</>
|
2019-06-23 06:25:38 +00:00
|
|
|
)
|
|
|
|
}
|
2020-02-08 23:23:08 +00:00
|
|
|
</FormikField>
|
2020-07-04 22:19:46 +00:00
|
|
|
));
|
|
|
|
Field.displayName = 'Field';
|
2020-02-08 23:23:08 +00:00
|
|
|
|
|
|
|
export default Field;
|