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

45 lines
1.6 KiB
TypeScript
Raw Normal View History

import React 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 = ({ id, name, light = false, label, description, validate, className, ...props }: Props) => (
2020-02-08 23:23:08 +00:00
<FormikField name={name} validate={validate}>
{
({ field, form: { errors, touched } }: FieldProps) => (
<React.Fragment>
{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])}
/>
{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
}
</React.Fragment>
)
}
2020-02-08 23:23:08 +00:00
</FormikField>
);
2020-02-08 23:23:08 +00:00
export default Field;