import { ErrorMessageProps, Field as FormikField, FieldInputProps, useFormikContext } from "formik"; import React, { useMemo } from "react"; import { InputProps, LabelProps } from "reactstrap"; import Input from "./Input"; import SimpleField from "./SimpleField"; type FieldProps = Partial> & InputProps & { errorMessageProps?: ErrorMessageProps; labelProps?: LabelProps; labelText?: string; withLoading?: boolean; withTemplate?: boolean; }; const Field: React.FC = ({ disabled, errorMessageProps, labelProps, labelText, withLoading, withTemplate, ...props }) => { const { isSubmitting } = useFormikContext(); const isDisabled = useMemo(() => { if (disabled) { return true; } return isSubmitting && withLoading; }, [isSubmitting, withLoading, disabled]); const inputId = props.id ?? props.name; const label = labelText ?? props.name?.replace(/([A-Z])/g, ' $1').trim() ?? ''; const correctedLabelProps: LabelProps = { for: inputId, ...labelProps, }; props = { ...props, disabled: isDisabled, 'aria-disabled': isDisabled }; return ( <> {withTemplate ? ( ) : ( )} ); }; Field.defaultProps = { disabled: false, withTemplate: true, withLoading: true, }; export default Field;