import { useCallback } from 'react' import { Controller, get, ControllerProps, FieldValues } from 'react-hook-form' import type { ControlledSelectProps } from './types' import { Field } from '../../molecules' import { SelectKey } from '@/design-system' export const ControlledSelect = < T extends SelectKey, TFieldValues extends FieldValues = FieldValues, >({ control, name, errors, rules, isRequired, ...props }: ControlledSelectProps) => { const errorMessage = get(errors, name)?.message const renderSelect = useCallback( ({ field: { onChange, value } }: Parameters[0]) => { const handleChange = (newValue: T[]) => { if (props.maxSelectedItems === 1) { onChange(newValue[0]) } else { onChange(newValue) } } const properValue = Array.isArray(value) ? value : [value] return ( ) }, [errorMessage, props, isRequired] ) return ( ) }