@@ -13,6 +13,11 @@ interface SelectFieldProps {
1313 placeholder ?: string ;
1414}
1515
16+ type SelectedOption = {
17+ value : string ;
18+ label : string ;
19+ } | null ;
20+
1621export function SelectField ( {
1722 label,
1823 optional = false ,
@@ -25,14 +30,14 @@ export function SelectField({
2530 placeholder = "Select an option" ,
2631} : SelectFieldProps ) : React . ReactElement {
2732 const [ isOpen , setIsOpen ] = useState ( false ) ;
28- const [ selectedOption , setSelectedOption ] = useState < string | null > ( null ) ;
33+ const [ selectedOption , setSelectedOption ] = useState < SelectedOption | null > ( null ) ;
2934
3035 const handleToggle = ( ) => {
3136 setIsOpen ( ! isOpen ) ;
3237 } ;
3338
3439 const handleSelectOption = ( value : string , label : string ) => {
35- setSelectedOption ( label ) ;
40+ setSelectedOption ( { value , label } ) ;
3641 setIsOpen ( false ) ;
3742 } ;
3843
@@ -46,7 +51,7 @@ export function SelectField({
4651 </ div >
4752 < div className = { styles . selectWrapper } >
4853 < button type = "button" className = { styles . selectField } onClick = { handleToggle } >
49- < span className = { selectedOption ? styles . selectedText : styles . placeholderText } > { selectedOption || placeholder } </ span >
54+ < span className = { selectedOption ? styles . selectedText : styles . placeholderText } > { selectedOption ?. label || placeholder } </ span >
5055 < SvgUse
5156 href = "/assets/icons/chevron-down.svg#root"
5257 className = { clsx ( styles . chevron , isOpen && styles [ "chevron--expanded" ] ) }
@@ -57,7 +62,11 @@ export function SelectField({
5762 { isOpen && (
5863 < div className = { styles . dropdown } >
5964 { options . map ( ( option ) => (
60- < div key = { option . value } className = { styles . dropdownOption } onClick = { ( ) => handleSelectOption ( option . value , option . label ) } >
65+ < div
66+ key = { option . value }
67+ className = { clsx ( styles . dropdownOption , selectedOption ?. value === option . value && styles [ "dropdownOption--selected" ] ) }
68+ onClick = { ( ) => handleSelectOption ( option . value , option . label ) }
69+ >
6170 { option . label }
6271 </ div >
6372 ) ) }
0 commit comments