misc_pterodactyl-panel/resources/scripts/components/elements/Field.tsx

43 lines
1.5 KiB
TypeScript
Raw Normal View History

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';
import Input 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';
2019-12-16 00:41:20 +00:00
interface OwnProps {
name: string;
light?: boolean;
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}>
{
({ field, form: { errors, touched } }: FieldProps) => (
<div>
{label &&
2020-07-04 19:39:55 +00:00
<Label htmlFor={id} isLight={light}>{label}</Label>
}
<Input
id={id}
{...field}
2019-12-16 00:41:20 +00:00
{...props}
isLight={light}
hasError={!!(touched[field.name] && errors[field.name])}
/>
2021-07-25 22:41:54 +00:00
<InputError errors={errors} touched={touched} name={field.name}>
{description || null}
</InputError>
</div>
)
}
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;