Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clients/packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"countries-list": "^3.2.2",
"date-fns": "^4.1.0",
"input-otp": "^1.4.2",
"lucide-react": "^0.563.0",
Expand Down
33 changes: 18 additions & 15 deletions clients/packages/ui/src/components/atoms/CountryPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { getCountryData, getEmojiFlag, TCountryCode } from 'countries-list'
import { useMemo } from 'react'

import {
Select,
Expand All @@ -10,18 +10,9 @@ import {
SelectValue,
} from './Select'

const getCountryList = (codes: TCountryCode[]) => {
return codes
.map((countryCode) => ({
code: countryCode,
country: getCountryData(countryCode),
emoji: getEmojiFlag(countryCode),
}))
.sort((a, b) => a.country.name.localeCompare(b.country.name))
}

const CountryPicker = ({
allowedCountries,
locale,
value,
onChange,
autoComplete,
Expand All @@ -32,6 +23,7 @@ const CountryPicker = ({
placeholder = 'Country',
}: {
allowedCountries: readonly string[]
locale?: string
value?: string
onChange: (value: string) => void
autoComplete?: string
Expand All @@ -41,7 +33,18 @@ const CountryPicker = ({
contentClassName?: string
placeholder?: string
}) => {
const countryMap = getCountryList(allowedCountries as TCountryCode[])
const countryList = useMemo(() => {
const displayNames = new Intl.DisplayNames(locale ? [locale] : [], {
type: 'region',
})
return allowedCountries
.map((code) => ({
code,
name: displayNames.of(code) ?? code,
}))
.sort((a, b) => a.name.localeCompare(b.name, locale))
}, [allowedCountries, locale])

return (
<Select
onValueChange={onChange}
Expand All @@ -58,16 +61,16 @@ const CountryPicker = ({
/>
</SelectTrigger>
<SelectContent className={contentClassName}>
{countryMap.map(({ code, country }) => (
{countryList.map(({ code, name }) => (
<SelectItem
key={code}
value={code}
textValue={country.name}
textValue={name}
className={itemClassName}
>
{/* Wrap in div to workaround an issue with browser automatic translation
https://github.com/shadcn-ui/ui/issues/852 */}
<div>{country.name}</div>
<div>{name}</div>
</SelectItem>
))}
</SelectContent>
Expand Down
8 changes: 0 additions & 8 deletions clients/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading