Merged in fix/LOY-95-localize-country-names (pull request #1364)

fix(LOY-95): localize and sort country names in select

Approved-by: Chuma Mcphoy (We Ahead)
This commit is contained in:
Christian Andolf
2025-02-18 14:31:00 +00:00

View File

@@ -15,6 +15,7 @@ import { useIntl } from "react-intl"
import Label from "@/components/TempDesignSystem/Form/Label"
import SelectChevron from "@/components/TempDesignSystem/Form/SelectChevron"
import Body from "@/components/TempDesignSystem/Text/Body"
import useLang from "@/hooks/useLang"
import ErrorMessage from "../ErrorMessage"
import { countries } from "./countries"
@@ -34,6 +35,7 @@ export default function CountrySelect({
readOnly = false,
registerOptions = {},
}: CountryProps) {
const lang = useLang()
const intl = useIntl()
const [rootDiv, setRootDiv] = useState<CountryPortalContainer>(undefined)
@@ -54,6 +56,7 @@ export default function CountrySelect({
}
const selectCountryLabel = intl.formatMessage({ id: "Select a country" })
const collator = new Intl.Collator(lang)
return (
<div className={`${styles.container} ${className}`} ref={setRef}>
@@ -102,17 +105,29 @@ export default function CountrySelect({
UNSTABLE_portalContainer={rootDiv}
>
<ListBox>
{countries.map((country, idx) => (
<Body asChild fontOnly key={`${country.code}-${idx}`}>
<ListBoxItem
aria-label={country.name}
className={styles.listBoxItem}
id={country.code}
>
{country.name}
</ListBoxItem>
</Body>
))}
{countries
.map((country) => ({
...country,
localizedDisplayName:
intl.formatDisplayName(country.code, { type: "region" }) ||
country.name,
}))
.sort((a, b) =>
collator.compare(a.localizedDisplayName, b.localizedDisplayName)
)
.map((country, idx) => {
return (
<Body asChild fontOnly key={`${country.code}-${idx}`}>
<ListBoxItem
aria-label={country.name}
className={styles.listBoxItem}
id={country.code}
>
{country.localizedDisplayName}
</ListBoxItem>
</Body>
)
})}
</ListBox>
</Popover>
</ComboBox>