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';
|
2021-09-18 01:56:44 +00:00
|
|
|
import Input, { Textarea } from '@/components/elements/Input';
|
2020-07-04 19:39:55 +00:00
|
|
|
import Label from '@/components/elements/Label';
|
2021-07-25 22:41:54 +00:00
|
|
|
import InputError from '@/components/elements/InputError';
|
2021-09-15 21:37:17 +00:00
|
|
|
import tw from 'twin.macro';
|
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) => (
|
2021-05-01 17:44:40 +00:00
|
|
|
<div>
|
2019-06-23 06:25:38 +00:00
|
|
|
{label &&
|
2021-09-15 21:37:17 +00:00
|
|
|
<div css={tw`flex flex-row`} title={description}>
|
|
|
|
<Label htmlFor={id} isLight={light}>{label}</Label>
|
|
|
|
{/*{description && <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" css={tw`h-4 w-4 ml-1 cursor-pointer`}><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>}*/}
|
|
|
|
</div>
|
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
|
|
|
/>
|
2021-07-25 22:41:54 +00:00
|
|
|
<InputError errors={errors} touched={touched} name={field.name}>
|
|
|
|
{description || null}
|
|
|
|
</InputError>
|
2021-05-01 17:44:40 +00:00
|
|
|
</div>
|
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
|
|
|
|
2021-09-18 01:56:44 +00:00
|
|
|
type Props2 = OwnProps & Omit<React.TextareaHTMLAttributes<HTMLTextAreaElement>, 'name'>;
|
|
|
|
|
|
|
|
export const TextareaField = forwardRef<HTMLTextAreaElement, Props2>(
|
|
|
|
function TextareaField ({ id, name, light = false, label, description, validate, ...props }, ref) {
|
|
|
|
return (
|
|
|
|
<FormikField innerRef={ref} name={name} validate={validate}>
|
|
|
|
{
|
|
|
|
({ field, form: { errors, touched } }: FieldProps) => (
|
|
|
|
<div>
|
|
|
|
{label && <Label htmlFor={id} isLight={light}>{label}</Label>}
|
|
|
|
<Textarea
|
|
|
|
id={id}
|
|
|
|
{...field}
|
|
|
|
{...props}
|
|
|
|
isLight={light}
|
|
|
|
hasError={!!(touched[field.name] && errors[field.name])}
|
|
|
|
/>
|
|
|
|
<InputError errors={errors} touched={touched} name={field.name}>
|
|
|
|
{description || null}
|
|
|
|
</InputError>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</FormikField>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
TextareaField.displayName = 'TextareaField';
|
|
|
|
|
2020-02-08 23:23:08 +00:00
|
|
|
export default Field;
|