2020-02-08 23:23:08 +00:00
|
|
|
import { Field as FormikField, FieldProps } from 'formik';
|
2021-10-01 17:07:48 +00:00
|
|
|
import React, { forwardRef } from 'react';
|
|
|
|
import tw, { styled } from 'twin.macro';
|
2021-09-18 01:56:44 +00:00
|
|
|
import Input, { Textarea } from '@/components/elements/Input';
|
2021-07-25 22:41:54 +00:00
|
|
|
import InputError from '@/components/elements/InputError';
|
2021-10-01 17:07:48 +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>;
|
2021-10-01 17:07:48 +00:00
|
|
|
|
|
|
|
className?: string;
|
2019-06-23 06:25:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 00:41:20 +00:00
|
|
|
type Props = OwnProps & Omit<React.InputHTMLAttributes<HTMLInputElement>, 'name'>;
|
|
|
|
|
2021-10-01 17:07:48 +00:00
|
|
|
const Field = forwardRef<HTMLInputElement, Props>(({ id, name, light = false, label, description, validate, className, ...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-10-01 17:07:48 +00:00
|
|
|
<div className={className}>
|
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-10-01 17:07:48 +00:00
|
|
|
export default Field;
|
|
|
|
|
|
|
|
type TextareaProps = OwnProps & Omit<React.TextareaHTMLAttributes<HTMLTextAreaElement>, 'name'>;
|
2021-09-18 01:56:44 +00:00
|
|
|
|
2021-10-01 17:07:48 +00:00
|
|
|
export const TextareaField = forwardRef<HTMLTextAreaElement, TextareaProps>(
|
|
|
|
function TextareaField ({ id, name, light = false, label, description, validate, className, ...props }, ref) {
|
2021-09-18 01:56:44 +00:00
|
|
|
return (
|
|
|
|
<FormikField innerRef={ref} name={name} validate={validate}>
|
|
|
|
{
|
|
|
|
({ field, form: { errors, touched } }: FieldProps) => (
|
2021-10-01 17:07:48 +00:00
|
|
|
<div className={className}>
|
2021-09-18 01:56:44 +00:00
|
|
|
{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';
|
|
|
|
|
2021-10-01 17:07:48 +00:00
|
|
|
export const FieldRow = styled.div`
|
|
|
|
${tw`mb-6 md:w-full md:flex md:flex-row`};
|
|
|
|
|
|
|
|
& > div {
|
|
|
|
${tw`md:w-full md:flex md:flex-col mb-6 md:mb-0`};
|
|
|
|
|
|
|
|
&:first-of-type {
|
|
|
|
${tw`md:mr-3`};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:not(:first-of-type):not(:last-of-type) {
|
|
|
|
${tw`md:mx-3`};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:last-of-type {
|
|
|
|
${tw`md:ml-3`};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|