Skip to content

Commit

Permalink
chore(application-system): Translate country list in phone prefix dro…
Browse files Browse the repository at this point in the history
…pdown (#16708)

* chore(application-system): Translate country list in phone prefix dropdown

* PR comments

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and jonnigs committed Nov 12, 2024
1 parent 8bb98e0 commit 1002d0b
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const PhoneFormField: FC<React.PropsWithChildren<Props>> = ({
)
: undefined
}
locale={locale as Locale}
autoFocus={autoFocus}
error={error}
control={control}
Expand Down
27 changes: 21 additions & 6 deletions libs/island-ui/core/src/lib/PhoneInput/PhoneInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ import { Icon } from '../IconRC/Icon'
import { StringOption } from '../Select/Select.types'
import { CountryCodeSelect } from './CountryCodeSelect/CountryCodeSelect'
import NumberFormat, { NumberFormatValues } from 'react-number-format'
import { countryCodes as countryCodeList } from './countryCodes'
import { countryCodesIS, countryCodesEN } from './countryCodes'
import { parse } from 'libphonenumber-js'
import { useEffectOnce } from 'react-use'
import { Locale } from '@island.is/shared/types'

const DEFAULT_COUNTRY_CODE = '+354'

const getCountryCodes = (allowedCountryCodes?: string[]): StringOption[] => {
const getCountryCodes = (
lang: Locale,
allowedCountryCodes?: string[],
): StringOption[] => {
const countryCodeList = lang === 'is' ? countryCodesIS : countryCodesEN
return countryCodeList
.filter((x) =>
allowedCountryCodes ? allowedCountryCodes.includes(x.code) : true,
Expand Down Expand Up @@ -60,9 +65,10 @@ const getDefaultValue = (
* getDefaultCountryCode("+455812345") // +45
* getDefaultCountryCode("5812345") // +354
*/
const getDefaultCountryCode = (phoneNumber?: string) => {
const getDefaultCountryCode = (lang: Locale, phoneNumber?: string) => {
if (!phoneNumber) return DEFAULT_COUNTRY_CODE
const parsedPhoneNumber = parse(phoneNumber)
const countryCodeList = lang === 'is' ? countryCodesIS : countryCodesEN

if (parsedPhoneNumber && parsedPhoneNumber.country) {
return (
Expand All @@ -85,6 +91,7 @@ export type PhoneInputProps = Omit<
| 'value'
> & {
defaultValue?: string
locale?: Locale
value?: string
backgroundColor?: InputBackgroundColor
onValueChange?: (values: NumberFormatValues) => void
Expand All @@ -103,6 +110,7 @@ export const PhoneInput = forwardRef(
const {
name,
label,
locale,
errorMessage = '',
hasError = Boolean(errorMessage),
value,
Expand Down Expand Up @@ -135,10 +143,14 @@ export const PhoneInput = forwardRef(
const [isMenuOpen, setIsMenuOpen] = useState(false)
const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null)
const mergedRefs = useMergeRefs(inputRef, ref || null)
const countryCodeList = locale === 'is' ? countryCodesIS : countryCodesEN

// Extract default country code from value, with value from form context having priority
const defaultCountryCode = getDefaultCountryCode(value || defaultValue)
const countryCodes = getCountryCodes(allowedCountryCodes)
const defaultCountryCode = getDefaultCountryCode(
locale || 'is',
value || defaultValue,
)
const countryCodes = getCountryCodes(locale || 'is', allowedCountryCodes)

const selected = countryCodes.find(
(x) => x.value === defaultCountryCode,
Expand Down Expand Up @@ -171,7 +183,10 @@ export const PhoneInput = forwardRef(
*/
const handleInputChange = (e: SyntheticEvent<HTMLInputElement>) => {
if (e.currentTarget.value.startsWith('+')) {
const updatedCC = getDefaultCountryCode(e.currentTarget.value)
const updatedCC = getDefaultCountryCode(
locale || 'is',
e.currentTarget.value,
)
e.currentTarget.value = e.currentTarget.value.replace(updatedCC, '')
if (!disableDropdown) {
setSelectedCountryCode(
Expand Down
Loading

0 comments on commit 1002d0b

Please sign in to comment.