2022-12-16 02:06:14 +00:00
|
|
|
import type { FieldProps } from 'formik';
|
|
|
|
import { Field as FormikField } from 'formik';
|
|
|
|
import type { InputHTMLAttributes, TextareaHTMLAttributes } from 'react';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { forwardRef } from 'react';
|
2022-12-16 02:06:14 +00:00
|
|
|
import tw, { styled } from 'twin.macro';
|
|
|
|
|
2020-07-04 19:39:55 +00:00
|
|
|
import Label from '@/components/elements/Label';
|
2022-12-16 02:06:14 +00:00
|
|
|
import Input, { Textarea } from '@/components/elements/Input';
|
|
|
|
import InputError from '@/components/elements/InputError';
|
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>;
|
|
|
|
}
|
|
|
|
|
2022-12-16 02:06:14 +00:00
|
|
|
type Props = OwnProps & Omit<InputHTMLAttributes<HTMLInputElement>, 'name'>;
|
2019-12-16 00:41:20 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const Field = forwardRef<HTMLInputElement, Props>(
|
|
|
|
({ id, name, light = false, label, description, validate, ...props }, ref) => (
|
|
|
|
<FormikField innerRef={ref} name={name} validate={validate}>
|
|
|
|
{({ field, form: { errors, touched } }: FieldProps) => (
|
2021-05-01 17:44:40 +00:00
|
|
|
<div>
|
2022-06-26 19:13:52 +00:00
|
|
|
{label && (
|
|
|
|
<Label htmlFor={id} isLight={light}>
|
|
|
|
{label}
|
|
|
|
</Label>
|
|
|
|
)}
|
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
|
|
|
/>
|
2022-06-26 19:13:52 +00:00
|
|
|
{touched[field.name] && errors[field.name] ? (
|
2019-12-16 00:41:20 +00:00
|
|
|
<p className={'input-help error'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{(errors[field.name] as string).charAt(0).toUpperCase() +
|
|
|
|
(errors[field.name] as string).slice(1)}
|
2019-06-23 06:25:38 +00:00
|
|
|
</p>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : description ? (
|
|
|
|
<p className={'input-help'}>{description}</p>
|
|
|
|
) : null}
|
2021-05-01 17:44:40 +00:00
|
|
|
</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
|
|
|
</FormikField>
|
2022-11-25 20:25:03 +00:00
|
|
|
),
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2020-07-04 22:19:46 +00:00
|
|
|
Field.displayName = 'Field';
|
2020-02-08 23:23:08 +00:00
|
|
|
|
|
|
|
export default Field;
|
2022-12-16 02:06:14 +00:00
|
|
|
|
|
|
|
type TextareaProps = OwnProps & Omit<TextareaHTMLAttributes<HTMLTextAreaElement>, 'name'>;
|
|
|
|
|
|
|
|
export const TextareaField = forwardRef<HTMLTextAreaElement, TextareaProps>(function TextareaField(
|
|
|
|
{ id, name, light = false, label, description, validate, className, ...props },
|
|
|
|
ref,
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<FormikField innerRef={ref} name={name} validate={validate}>
|
|
|
|
{({ field, form: { errors, touched } }: FieldProps) => (
|
|
|
|
<div className={className}>
|
|
|
|
{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';
|
|
|
|
|
|
|
|
export const FieldRow = styled.div`
|
|
|
|
${tw`grid grid-cols-1 sm:grid-cols-2 gap-x-6 gap-y-6 mb-6`};
|
|
|
|
& > div {
|
|
|
|
${tw`sm:w-full sm:flex sm:flex-col`};
|
|
|
|
}
|
|
|
|
`;
|