2020-02-23 04:07:56 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Field, FieldProps } from 'formik';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import InputError from '@/components/elements/InputError';
|
2020-07-04 22:19:46 +00:00
|
|
|
import Label from '@/components/elements/Label';
|
|
|
|
import tw from 'twin.macro';
|
2020-02-23 04:07:56 +00:00
|
|
|
|
|
|
|
interface Props {
|
2020-03-23 01:15:38 +00:00
|
|
|
id?: string;
|
2020-02-23 04:07:56 +00:00
|
|
|
name: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
label?: string;
|
|
|
|
description?: string;
|
|
|
|
validate?: (value: any) => undefined | string | Promise<any>;
|
|
|
|
}
|
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
const FormikFieldWrapper = ({ id, name, label, className, description, validate, children }: Props) => (
|
2020-02-23 04:07:56 +00:00
|
|
|
<Field name={name} validate={validate}>
|
|
|
|
{
|
|
|
|
({ field, form: { errors, touched } }: FieldProps) => (
|
|
|
|
<div className={classNames(className, { 'has-error': touched[field.name] && errors[field.name] })}>
|
2020-07-04 22:19:46 +00:00
|
|
|
{label && <Label htmlFor={id}>{label}</Label>}
|
2020-02-23 04:07:56 +00:00
|
|
|
{children}
|
|
|
|
<InputError errors={errors} touched={touched} name={field.name}>
|
2020-07-04 22:19:46 +00:00
|
|
|
{description || null}
|
2020-02-23 04:07:56 +00:00
|
|
|
</InputError>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Field>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default FormikFieldWrapper;
|