import { useMemo } from 'react'; import { Flex, FormControl, FormLabel, FormErrorMessage } from '@chakra-ui/react'; import { useFormContext } from 'react-hook-form'; import { If, Then } from 'react-if'; import { useColorValue } from '~/context'; import { useBooleanValue } from '~/hooks'; import type { FieldError } from 'react-hook-form'; import type { FormData } from '~/types'; import type { TField } from './types'; export const FormField = (props: TField): JSX.Element => { const { name, label, children, labelAddOn, fieldAddOn, hiddenLabels = false, ...rest } = props; const labelColor = useColorValue('blackAlpha.700', 'whiteAlpha.700'); const errorColor = useColorValue('red.500', 'red.300'); const opacity = useBooleanValue(hiddenLabels, 0, undefined); const { formState } = useFormContext(); const error = useMemo(() => { if (name in formState.errors) { console.group(`Error on field '${label}'`); console.warn(formState.errors[name as keyof FormData]); console.groupEnd(); return formState.errors[name as keyof FormData] as FieldError; } return null; }, [formState, label, name]); return ( {label} {labelAddOn} {children} {fieldAddOn} {error?.message} ); };