Skip to content

Commit 84b7e53

Browse files
Telephone number country menu (#156)
This PR aims to fix the country menu in order to sort alphabetically the countries and to add the possibility to customize the menu width (200 by default)
1 parent 13a010c commit 84b7e53

4 files changed

Lines changed: 15 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- support into `requiredFields` property of `Form` component, for nested objects and arrays.
13-
- `form` helper functions
13+
- `form` helper functions.
14+
- `countryMenuWidth` property to `TelephoneNumberInput` in order to customize the country menu width.
1415

1516
### Fixed
1617

18+
- `TelephoneNumberInput` countries order, in order to be alphabetically sorted.
1719
- Required field label on `FormGroupLayoutLabel`, `ColorPicker`, `TelephoneNumberInput`, `TypeaheadTextField` (hence `StaticTypeaheadInput` and `AsyncTypeaheadInput`) in order to display \* also on nested and array fields.
1820

1921
1. `requiredFields` can still accept a `FieldPath<T>[]`

src/lib/TelephoneNumberInput.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface TelephoneNumberInputProps<T extends FieldValues>
1717
placeholder?: string;
1818
renderAutocompleteField?: (children: ReactNode) => ReactNode;
1919
locale?: string;
20+
countryMenuWidth?: number;
2021
}
2122

2223
const TelephoneNumberInput = <T extends FieldValues>(props: TelephoneNumberInputProps<T>) => {

src/lib/components/TelephoneNumberInput/TelephoneNumberAutocomplete.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { TextField } from "@mui/material";
1212
import { textFieldBootstrapStyle } from "src/lib/helpers/mui";
1313

1414
interface TelephoneNumberAutocompleteProps<T extends FieldValues>
15-
extends Pick<TelephoneNumberInputProps<T>, "pinnedCountries" | "locale" | "useBootstrapStyle" | "name" | "onChange"> {
15+
extends Pick<
16+
TelephoneNumberInputProps<T>,
17+
"pinnedCountries" | "locale" | "useBootstrapStyle" | "name" | "onChange" | "countryMenuWidth"
18+
> {
1619
popupState: PopupState;
1720
nationalPhoneNumber: string | undefined;
1821
country: Country;
@@ -24,6 +27,7 @@ const TelephoneNumberAutocomplete = <T extends FieldValues>(props: TelephoneNumb
2427
pinnedCountries = [],
2528
locale,
2629
useBootstrapStyle,
30+
countryMenuWidth = 200,
2731
name,
2832
onChange: propsOnChange,
2933
popupState,
@@ -42,7 +46,7 @@ const TelephoneNumberAutocomplete = <T extends FieldValues>(props: TelephoneNumb
4246
disableClearable={false}
4347
getOptionDisabled={(option) => option.disabled ?? false}
4448
renderInput={(params) => <TextField {...params} inputRef={inputRef} />}
45-
sx={{ ...(useBootstrapStyle && textFieldBootstrapStyle), width: 200 }}
49+
sx={{ ...(useBootstrapStyle && textFieldBootstrapStyle), width: countryMenuWidth }}
4650
onChange={(_, value, reason) => {
4751
// cannot be cleared
4852
if (reason === "clear") {

src/lib/helpers/telephoneNumber.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ const getCountriesOptions = (pinnedRegions: RegionCode[], locale?: string): Labe
100100
? locale
101101
: undefined;
102102

103-
const supportedRegions = phoneNumberUtil.getSupportedRegions().filter((x) => !pinnedRegions.includes(x));
103+
const supportedRegions = phoneNumberUtil
104+
.getSupportedRegions()
105+
.filter((x) => !pinnedRegions.includes(x))
106+
.sort((a, b) => a.localeCompare(b));
107+
104108
let labelValueOptions = [];
105109

106110
if (pinnedRegions.length > 0) {

0 commit comments

Comments
 (0)