Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/components/elements/FormikFieldWrapper.tsx
7461 views
1
import React from 'react';
2
import { Field, FieldProps } from 'formik';
3
import InputError from '@/components/elements/InputError';
4
import Label from '@/components/elements/Label';
5
6
interface Props {
7
id?: string;
8
name: string;
9
children: React.ReactNode;
10
className?: string;
11
label?: string;
12
description?: string;
13
validate?: (value: any) => undefined | string | Promise<any>;
14
}
15
16
const FormikFieldWrapper = ({ id, name, label, className, description, validate, children }: Props) => (
17
<Field name={name} validate={validate}>
18
{({ field, form: { errors, touched } }: FieldProps) => (
19
<div className={`${className} ${touched[field.name] && errors[field.name] ? 'has-error' : undefined}`}>
20
{label && <Label htmlFor={id}>{label}</Label>}
21
{children}
22
<InputError errors={errors} touched={touched} name={field.name}>
23
{description || null}
24
</InputError>
25
</div>
26
)}
27
</Field>
28
);
29
30
export default FormikFieldWrapper;
31
32