import { Controller } from 'react-hook-form'
import { DatePicker } from '../Input'
import { IDatePicker } from '../Input/DatePicker'
interface Props extends Omit<IDatePicker, 'onChange' | 'value'> {
  name: string
  control: any
}

export default function FormDatePicker({
  name,
  control,
  minDate,
  maxDate,
  label,
  required = false,
  ...rest
}: Props) {
  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <DatePicker
          label={label}
          required={required}
          onChange={field.onChange}
          value={field.value}
          minDate={minDate}
          maxDate={maxDate}
          inputProps={{
            ...rest.inputProps,
            error: !!error,
            helperText: error?.message ?? '',
          }}
          {...rest}
        />
      )}
    />
  )
}
