/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { FC, useState, ReactNode, FocusEvent, CSSProperties, ComponentType, ElementType, memo } from 'react'; import cx from 'clsx'; import { useField, ErrorMessage } from 'formik'; import omit from 'lodash.omit'; export interface IFormikUiLabelProps { name: string; label?: ReactNode; hint?: ReactNode; placeholder?: string; type?: string; disabled?: boolean; required?: boolean; style?: CSSProperties; format?: string; onAnimationStart?: (e: AnimationEvent) => void; onFocus?: (event: FocusEvent) => void; onBlur?: (event: FocusEvent) => void; } export const WithLabel = (component: string = 'input') => (WrappedComponent: ComponentType): ElementType => { const Label: FC = (props) => { const { name, label, hint, placeholder, type, disabled, required, style, format, onFocus, onBlur } = props; const [hideLabel, setHide] = useState(false); const [field, { error }] = useField(name); const hidden = type && type === 'hidden'; const moveLabel = component === 'phoneInput' && format !== 'NATIONAL'; const passableProps = omit(props, ['hint', 'label', 'onFocus', 'onBlur', 'required']); const handleAutoFill = (e: AnimationEvent): void => { setHide(e.animationName === 'onAutoFillStart'); }; const handleFocus = (event: FocusEvent): void => { if (onFocus) { onFocus(event); } setHide(true); }; const handleBlur = (event: FocusEvent): void => { if (onBlur) { onBlur(event); } field.onBlur(name); setHide(false); }; const hide = hideLabel || !!field.value || !!placeholder || !!(disabled && field.value); return ( ); }; return memo(Label); }; export default WithLabel;