Advertisement
metalni

form autocomplete

Sep 12th, 2023
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { type FC } from 'react'
  2. import { useController, useFormContext } from 'react-hook-form'
  3. import { AutoComplete, type IAutoCompleteProps } from '@components'
  4.  
  5. interface IAutoCompleteFormFieldProps
  6.   extends Omit<IAutoCompleteProps, 'field'> {
  7.   name: string
  8. }
  9.  
  10. export const AutoCompleteFormField: FC<IAutoCompleteFormFieldProps> = ({
  11.   name,
  12.   ...rest
  13. }) => {
  14.   const { control } = useFormContext()
  15.   const { field } = useController({ name, control })
  16.  
  17.   return (
  18.     // TODO: Refactor this
  19.     <AutoComplete
  20.       field={{
  21.         ...field,
  22.         // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  23.         value: { value: field.value },
  24.         onChange: ({ value }) => field.onChange(value),
  25.       }}
  26.       {...rest}
  27.     />
  28.   )
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement