|
1
|
import { |
|
2
|
ComponentPropsWithoutRef, |
|
3
|
forwardRef, |
|
4
|
useImperativeHandle, |
|
5
|
useRef, |
|
6
|
} from "react" |
|
7
|
import { useTranslation } from "react-i18next" |
|
8
|
import { countries } from "../../../lib/data/countries" |
|
9
|
import { Combobox } from "../combobox" |
|
10
|
|
|
11
|
export const CountrySelect = forwardRef< |
|
12
|
HTMLInputElement, |
|
13
|
Omit< |
|
14
|
ComponentPropsWithoutRef<typeof Combobox>, |
|
15
|
"options" | "multiple" | "value" | "onChange" |
|
16
|
> & { |
|
17
|
placeholder?: string |
|
18
|
defaultValue?: string |
|
19
|
allowClear?: boolean |
|
20
|
value?: string |
|
21
|
onChange?: (value: string | undefined) => void |
|
22
|
} |
|
23
|
>( |
|
24
|
( |
|
25
|
{ placeholder, defaultValue, allowClear, onChange, value, ...props }, |
|
26
|
ref |
|
27
|
) => { |
|
28
|
const { t } = useTranslation() |
|
29
|
const innerRef = useRef<HTMLInputElement>(null) |
|
30
|
|
|
31
|
useImperativeHandle(ref, () => innerRef.current as HTMLInputElement) |
|
32
|
|
|
33
|
return ( |
|
34
|
<Combobox |
|
35
|
{...props} |
|
36
|
ref={innerRef} |
|
37
|
value={value || ""} |
|
38
|
onChange={(newValue) => onChange?.(newValue || "")} |
|
39
|
options={countries.map((country) => ({ |
|
40
|
label: country.display_name, |
|
41
|
value: country.iso_2.toLowerCase(), |
|
42
|
}))} |
|
43
|
placeholder={placeholder || t("fields.selectCountry")} |
|
44
|
allowClear={allowClear} |
|
45
|
/> |
|
46
|
) |
|
47
|
} |
|
48
|
) |
|
49
|
|
|
50
|
CountrySelect.displayName = "CountrySelect" |