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