import OLFormGroup from '@/shared/components/ol/ol-form-group' import OLFormLabel from '@/shared/components/ol/ol-form-label' import OLFormSelect from '@/shared/components/ol/ol-form-select' import { ChangeEventHandler, useCallback, useEffect, useRef } from 'react' import { useEditorLeftMenuContext } from '@/features/editor-left-menu/components/editor-left-menu-context' import OLSpinner from '@/shared/components/ol/ol-spinner' type PossibleValue = string | number | boolean export type Option = { value: T label: string ariaHidden?: 'true' | 'false' disabled?: boolean } export type Optgroup = { label: string options: Array> } type SettingsMenuSelectProps = { label: string name: string options?: Array> optgroups?: Array> loading?: boolean onChange: (val: T) => void value?: T disabled?: boolean translateOptions?: 'yes' | 'no' } export default function SettingsMenuSelect({ label, name, options, optgroups, loading, onChange, value, disabled = false, translateOptions, }: SettingsMenuSelectProps) { const handleChange: ChangeEventHandler = useCallback( event => { const selectedValue = event.target.value let onChangeValue: PossibleValue = selectedValue if (typeof value === 'boolean') { onChangeValue = selectedValue === 'true' } else if (typeof value === 'number') { onChangeValue = parseInt(selectedValue, 10) } onChange(onChangeValue as T) }, [onChange, value] ) const { settingToFocus } = useEditorLeftMenuContext() const selectRef = useRef(null) useEffect(() => { if (settingToFocus === name && selectRef.current) { selectRef.current.scrollIntoView({ block: 'center', behavior: 'smooth', }) selectRef.current.focus() } // clear the focus setting window.dispatchEvent( new CustomEvent('ui.focus-setting', { detail: undefined }) ) }, [name, settingToFocus]) return ( {label} {loading ? ( ) : ( {options?.map(option => ( ))} {optgroups?.map(optgroup => ( {optgroup.options.map(option => ( ))} ))} )} ) }