import { ElementType, useRef } from 'react'; import * as uuid from 'uuid'; import { CustomFieldInputProps } from './CustomFieldInputProps'; import { CustomFieldMetaProps } from './CustomFieldMetaProps'; import { RenderComponent, RenderComponentProps } from './Field'; import { InjectedFieldProps } from './InjectedFieldProps'; export type StandAloneInputProps< TValue, TRenderComponent extends ElementType > = { /** Component to render inside of the field. */ Component: RenderComponent; /** Value of the field. */ value: TValue; /** Function to change the value of the field. */ onChange: (arg: TValue) => void; } & Omit< RenderComponentProps, | keyof InjectedFieldProps | 'value' | 'onChange' | 'normalize' | 'validate' >; /** The stand alone version of ``. Can use a type of input component outside of a `
`. */ export default function StandAloneInput( props: StandAloneInputProps ) { const { Component, value, onChange, disabled, ...rest } = props; const handleChange = (arg: TValue) => { if (!disabled) { onChange(arg); } }; const Wrapped = Component as React.ComponentType>; const idRef = useRef(uuid.v4()); const input: CustomFieldInputProps = { id: idRef.current, name: idRef.current, value: value, onChange: handleChange, onBlur: () => {}, }; const meta: CustomFieldMetaProps = { warning: undefined, value: value, touched: false, initialTouched: false, }; return ; }