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

49 lines
1.7 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';
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'>;
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) => (
<div>
{label && (
<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])}
/>
{touched[field.name] && errors[field.name] ? (
2019-12-16 00:41:20 +00:00
<p className={'input-help error'}>
{(errors[field.name] as string).charAt(0).toUpperCase() +
(errors[field.name] as string).slice(1)}
</p>
) : description ? (
<p className={'input-help'}>{description}</p>
) : null}
</div>
)}
</FormikField>
)
);
2020-07-04 22:19:46 +00:00
Field.displayName = 'Field';
2020-02-08 23:23:08 +00:00
export default Field;