import clsx from 'clsx'; import * as React from 'react'; import { RegisterOptions, useFormContext } from 'react-hook-form'; import { HiExclamationCircle } from 'react-icons/hi'; export type SelectInputProps = { label: string; id: string; placeholder?: string; helperText?: string; type?: string; readOnly?: boolean; validation?: RegisterOptions; children: React.ReactNode; } & React.ComponentPropsWithoutRef<'select'>; export default function SelectInput({ label, helperText, id, placeholder, readOnly = false, children, validation, ...rest }: SelectInputProps) { const { register, formState: { errors }, watch, } = useFormContext(); const value = watch(id); // Add disabled and selected attribute to option, will be used if readonly const readOnlyChildren = React.Children.map( children, (child) => { if (React.isValidElement(child)) { return React.cloneElement(child, { disabled: child.props.value !== rest?.defaultValue, // selected: child.props.value === rest?.defaultValue, }); } } ); return (
{errors[id] && (
)}
{helperText &&

{helperText}

} {errors[id] && ( {errors[id].message} )}
); }