Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/components/elements/Field.tsx
7461 views
1
import React, { forwardRef } from 'react';
2
import { Field as FormikField, FieldProps } from 'formik';
3
import Input from '@/components/elements/Input';
4
import Label from '@/components/elements/Label';
5
6
interface OwnProps {
7
name: string;
8
light?: boolean;
9
label?: string;
10
description?: string;
11
validate?: (value: any) => undefined | string | Promise<any>;
12
}
13
14
type Props = OwnProps & Omit<React.InputHTMLAttributes<HTMLInputElement>, 'name'>;
15
16
const Field = forwardRef<HTMLInputElement, Props>(
17
({ id, name, light = false, label, description, validate, ...props }, ref) => (
18
<FormikField innerRef={ref} name={name} validate={validate}>
19
{({ field, form: { errors, touched } }: FieldProps) => (
20
<div>
21
{label && (
22
<Label htmlFor={id} isLight={light}>
23
{label}
24
</Label>
25
)}
26
<Input
27
id={id}
28
{...field}
29
{...props}
30
isLight={light}
31
hasError={!!(touched[field.name] && errors[field.name])}
32
/>
33
{touched[field.name] && errors[field.name] ? (
34
<p className={'input-help error'}>
35
{(errors[field.name] as string).charAt(0).toUpperCase() +
36
(errors[field.name] as string).slice(1)}
37
</p>
38
) : description ? (
39
<p className={'input-help'}>{description}</p>
40
) : null}
41
</div>
42
)}
43
</FormikField>
44
)
45
);
46
Field.displayName = 'Field';
47
48
export default Field;
49
50