import React from 'react'; import './style.scss'; export interface SelectOption { value: string; label: string; disabled?: boolean; } export interface SelectGroup { label: string; options: SelectOption[]; } export type SelectItem = SelectOption | SelectGroup; export interface FormSelectProps { /** Current value */ value: string; /** Change handler */ onChange: (value: string) => void; /** Options - can be flat array or grouped */ options: SelectItem[]; /** Placeholder text */ placeholder?: string; /** Whether the select is disabled */ disabled?: boolean; /** Label for the select */ label?: string; /** Help text shown below the select */ helpText?: string; /** Error message */ error?: string; /** Whether the field is required */ required?: boolean; /** Custom className */ className?: string; } const isSelectGroup = (item: SelectItem): item is SelectGroup => { return 'options' in item; }; export const FormSelect: React.FC = ({ value, onChange, options, placeholder = 'Select an option...', disabled = false, label, helpText, error, required = false, className = '' }) => { const handleChange = (e: React.ChangeEvent) => { onChange(e.target.value); }; const renderOptions = () => { return options.map((item, index) => { if (isSelectGroup(item)) { return ( {item.options.map((option) => ( ))} ); } else { return ( ); } }); }; const hasError = !!error; return (
{label && ( )} {helpText && !error && ( {helpText} )} {error && ( {error} )}
); };