mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
58 lines
1.1 KiB
TypeScript
58 lines
1.1 KiB
TypeScript
import {
|
|
FieldValues,
|
|
RegisterOptions,
|
|
useFormContext,
|
|
} from 'react-hook-form';
|
|
import { ChangeEventHandler } from 'react';
|
|
|
|
// Styled
|
|
import { StyledSelect } from './styled';
|
|
|
|
export interface ISelectOptions {
|
|
value: string;
|
|
label: string;
|
|
}
|
|
interface ISelect {
|
|
options: ISelectOptions[];
|
|
optionsConfigForm?: RegisterOptions<FieldValues, string> | undefined;
|
|
value?: string;
|
|
onChange?: ChangeEventHandler<HTMLSelectElement> | undefined;
|
|
id?: string;
|
|
ariaLabel: string;
|
|
disable?: boolean;
|
|
}
|
|
|
|
const Select = ({
|
|
options,
|
|
id,
|
|
optionsConfigForm,
|
|
value,
|
|
onChange,
|
|
ariaLabel,
|
|
disable = false,
|
|
}: ISelect) => {
|
|
const { register } = useFormContext() ?? {};
|
|
|
|
if (!options) return;
|
|
|
|
return (
|
|
<StyledSelect
|
|
aria-label={ariaLabel}
|
|
id={id}
|
|
value={value}
|
|
disabled={disable}
|
|
{...(register
|
|
? { ...register(id!, optionsConfigForm) }
|
|
: { onChange: onChange })}
|
|
>
|
|
{options.map((option) => (
|
|
<option value={option.value} key={option.value} data-testid='select-option'>
|
|
{option.label}
|
|
</option>
|
|
))}
|
|
</StyledSelect>
|
|
);
|
|
};
|
|
|
|
export default Select;
|