@@ -12,56 +12,67 @@ import { countries } from "../../../lib/data/countries"
12
12
13
13
export const CountrySelect = forwardRef <
14
14
HTMLSelectElement ,
15
- ComponentPropsWithoutRef < "select" > & { placeholder ?: string }
16
- > ( ( { className, disabled, placeholder, ...props } , ref ) => {
17
- const { t } = useTranslation ( )
18
- const innerRef = useRef < HTMLSelectElement > ( null )
15
+ ComponentPropsWithoutRef < "select" > & {
16
+ placeholder ?: string
17
+ value ?: string
18
+ defaultValue ?: string
19
+ }
20
+ > (
21
+ (
22
+ { className, disabled, placeholder, value, defaultValue, ...props } ,
23
+ ref
24
+ ) => {
25
+ const { t } = useTranslation ( )
26
+ const innerRef = useRef < HTMLSelectElement > ( null )
19
27
20
- useImperativeHandle ( ref , ( ) => innerRef . current as HTMLSelectElement )
28
+ useImperativeHandle ( ref , ( ) => innerRef . current as HTMLSelectElement )
21
29
22
- const isPlaceholder = innerRef . current ?. value === ""
30
+ const isPlaceholder = innerRef . current ?. value === ""
23
31
24
- return (
25
- < div className = "relative" >
26
- < TrianglesMini
27
- className = { clx (
28
- "text-ui-fg-muted transition-fg pointer-events-none absolute right-2 top-1/2 -translate-y-1/2" ,
29
- {
30
- "text-ui-fg-disabled" : disabled ,
31
- }
32
- ) }
33
- />
34
- < select
35
- disabled = { disabled }
36
- className = { clx (
37
- "bg-ui-bg-field shadow-buttons-neutral transition-fg txt-compact-small flex w-full select-none appearance-none items-center justify-between rounded-md px-2 py-1.5 outline-none" ,
38
- "placeholder:text-ui-fg-muted text-ui-fg-base" ,
39
- "hover:bg-ui-bg-field-hover" ,
40
- "focus-visible:shadow-borders-interactive-with-active data-[state=open]:!shadow-borders-interactive-with-active" ,
41
- "aria-[invalid=true]:border-ui-border-error aria-[invalid=true]:shadow-borders-error" ,
42
- "invalid::border-ui-border-error invalid:shadow-borders-error" ,
43
- "disabled:!bg-ui-bg-disabled disabled:!text-ui-fg-disabled" ,
44
- {
45
- "text-ui-fg-muted" : isPlaceholder ,
46
- } ,
47
- className
48
- ) }
49
- { ...props }
50
- ref = { innerRef }
51
- >
52
- { /* Add an empty option so the first option is preselected */ }
53
- < option value = "" disabled className = "text-ui-fg-muted" >
54
- { placeholder || t ( "fields.selectCountry" ) }
55
- </ option >
56
- { countries . map ( ( country ) => {
57
- return (
58
- < option key = { country . iso_2 } value = { country . iso_2 } >
59
- { country . display_name }
60
- </ option >
61
- )
62
- } ) }
63
- </ select >
64
- </ div >
65
- )
66
- } )
32
+ return (
33
+ < div className = "relative" >
34
+ < TrianglesMini
35
+ className = { clx (
36
+ "text-ui-fg-muted transition-fg pointer-events-none absolute right-2 top-1/2 -translate-y-1/2" ,
37
+ {
38
+ "text-ui-fg-disabled" : disabled ,
39
+ }
40
+ ) }
41
+ />
42
+ < select
43
+ value = { value ? value . toLowerCase ( ) : undefined }
44
+ defaultValue = { defaultValue ? defaultValue . toLowerCase ( ) : undefined }
45
+ disabled = { disabled }
46
+ className = { clx (
47
+ "bg-ui-bg-field shadow-buttons-neutral transition-fg txt-compact-small flex w-full select-none appearance-none items-center justify-between rounded-md px-2 py-1.5 outline-none" ,
48
+ "placeholder:text-ui-fg-muted text-ui-fg-base" ,
49
+ "hover:bg-ui-bg-field-hover" ,
50
+ "focus-visible:shadow-borders-interactive-with-active data-[state=open]:!shadow-borders-interactive-with-active" ,
51
+ "aria-[invalid=true]:border-ui-border-error aria-[invalid=true]:shadow-borders-error" ,
52
+ "invalid::border-ui-border-error invalid:shadow-borders-error" ,
53
+ "disabled:!bg-ui-bg-disabled disabled:!text-ui-fg-disabled" ,
54
+ {
55
+ "text-ui-fg-muted" : isPlaceholder ,
56
+ } ,
57
+ className
58
+ ) }
59
+ { ...props }
60
+ ref = { innerRef }
61
+ >
62
+ { /* Add an empty option so the first option is preselected */ }
63
+ < option value = "" disabled className = "text-ui-fg-muted" >
64
+ { placeholder || t ( "fields.selectCountry" ) }
65
+ </ option >
66
+ { countries . map ( ( country ) => {
67
+ return (
68
+ < option key = { country . iso_2 } value = { country . iso_2 . toLowerCase ( ) } >
69
+ { country . display_name }
70
+ </ option >
71
+ )
72
+ } ) }
73
+ </ select >
74
+ </ div >
75
+ )
76
+ }
77
+ )
67
78
CountrySelect . displayName = "CountrySelect"
0 commit comments