Skip to content

Commit 56f634d

Browse files
fix(dashboard): Fix CountrySelect (#8301)
Resolves CC-214
1 parent d63ca00 commit 56f634d

File tree

2 files changed

+68
-51
lines changed

2 files changed

+68
-51
lines changed

packages/admin-next/dashboard/src/components/inputs/country-select/country-select.tsx

+60-49
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,67 @@ import { countries } from "../../../lib/data/countries"
1212

1313
export const CountrySelect = forwardRef<
1414
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)
1927

20-
useImperativeHandle(ref, () => innerRef.current as HTMLSelectElement)
28+
useImperativeHandle(ref, () => innerRef.current as HTMLSelectElement)
2129

22-
const isPlaceholder = innerRef.current?.value === ""
30+
const isPlaceholder = innerRef.current?.value === ""
2331

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+
)
6778
CountrySelect.displayName = "CountrySelect"

packages/admin-next/dashboard/src/lib/addresses.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AddressDTO } from "@medusajs/types"
22

3-
import { countries } from "./data/countries"
3+
import { countries, getCountryByIso2 } from "./data/countries"
44

55
export const isSameAddress = (a: AddressDTO | null, b: AddressDTO | null) => {
66
if (!a || !b) {
@@ -72,7 +72,13 @@ export const getFormattedAddress = ({
7272
if (country) {
7373
formattedAddress.push(country.display_name)
7474
} else if (country_code) {
75-
formattedAddress.push(country_code.toUpperCase())
75+
const country = getCountryByIso2(country_code)
76+
77+
if (country) {
78+
formattedAddress.push(country.display_name)
79+
} else {
80+
formattedAddress.push(country_code.toUpperCase())
81+
}
7682
}
7783

7884
return formattedAddress

0 commit comments

Comments
 (0)