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
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,24 @@ NOTE: If you use localization in the [documented](https://mui.com/material-ui/gu

Apart from the phone-specific properties described below, all [Input](https://mui.com/material-ui/api/input/#props) and [TextField](https://mui.com/material-ui/api/text-field/#props) properties supported by the used Material distribution can be applied to the phone input component.

| Property | Description | Type |
|--------------------|-------------------------------------------------------------------------------------------------------------------------------|---------------------------|
| value | An object containing a parsed phone number or the raw number. | [object](#value) / string |
| country | Country code to be selected by default. By default, it will show the flag of the user's country. | string |
| distinct | Show the distinct list of country codes not listing all area codes. | boolean |
| enableArrow | Shows an arrow next to the country flag. Default value is `false`. | boolean |
| enableSearch | Enables search in the country selection dropdown menu. Default value is `false`. | boolean |
| searchVariant | Accepts an Input variant, and values depend on the chosen Material distribution. | TextFieldVariants |
| searchNotFound | The value is shown if `enableSearch` is `true` and the query does not match any country. Default value is `No country found`. | string |
| searchPlaceholder | The value is shown if `enableSearch` is `true`. Default value is `Search country`. | string |
| disableDropdown | Disables the manual country selection through the dropdown menu. | boolean |
| disableParentheses | Disables parentheses from the input masks. Default enabled. | boolean |
| onlyCountries | Country codes to be included in the list. E.g. `onlyCountries={['us', 'ca', 'uk']}`. | string[] |
| excludeCountries | Country codes to be excluded from the list of countries. E.g. `excludeCountries={['us', 'ca', 'uk']}`. | string[] |
| preferredCountries | Country codes to be at the top of the list. E.g. `preferredCountries={['us', 'ca', 'uk']}`. | string[] |
| onChange | The only difference from the default `onChange` is that value comes first. | function(value, event) |
| onMount | The callback is triggered once the component gets mounted. | function(value) |
| Property | Description | Type |
|--------------------|-------------------------------------------------------------------------------------------------------------------------------|-------------------------------|
| value | An object containing a parsed phone number or the raw number. | [object](#value) / string |
| useSVG | Whether to use **SVG** icons for the flags or keep the default. By default, it will use **PNG** icons. | boolean |
| country | Country code to be selected by default. By default, it will show the flag of the user's country. | string |
| distinct | Show the distinct list of country codes not listing all area codes. | boolean |
| enableArrow | Shows an arrow next to the country flag. Default value is `false`. | boolean |
| enableSearch | Enables search in the country selection dropdown menu. Default value is `false`. | boolean |
| searchVariant | Accepts an Input variant, and values depend on the chosen Material distribution. | TextFieldVariants |
| searchNotFound | The value is shown if `enableSearch` is `true` and the query does not match any country. Default value is `No country found`. | string |
| searchPlaceholder | The value is shown if `enableSearch` is `true`. Default value is `Search country`. | string |
| disableDropdown | Disables the manual country selection through the dropdown menu. | boolean |
| disableParentheses | Disables parentheses from the input masks. Default enabled. | boolean |
| onlyCountries | Country codes to be included in the list. E.g. `onlyCountries={['us', 'ca', 'uk']}`. | string[] |
| excludeCountries | Country codes to be excluded from the list of countries. E.g. `excludeCountries={['us', 'ca', 'uk']}`. | string[] |
| preferredCountries | Country codes to be at the top of the list. E.g. `preferredCountries={['us', 'ca', 'uk']}`. | string[] |
| onChange | The only difference from the default `onChange` is that value comes first. | function(value, event) |
| onMount | The callback is triggered once the component gets mounted. | function(value) |

## Contribute

Expand Down
3 changes: 3 additions & 0 deletions examples/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ cd ~/mui-phone-input/
git restore .
git pull

rm -r node_modules package-lock.json
npm i && npm run build && npm pack

cd ~/mui-phone-input/examples/material
rm -r node_modules package-lock.json
npm install && npm run build
Expand Down
14 changes: 13 additions & 1 deletion examples/material/src/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Demo = () => {
const [mode, setMode] = useState<string>("light");
const [show, setShow] = useState<boolean>(true);
const [arrow, setArrow] = useState<boolean>(false);
const [useSvg, setUseSvg] = useState(false);
const [strict, setStrict] = useState<boolean>(false);
const [search, setSearch] = useState<boolean>(false);
const [copied, setCopied] = useState<boolean>(false);
Expand Down Expand Up @@ -51,6 +52,7 @@ const Demo = () => {

const code = useMemo(() => {
let code = "<PhoneInput\n";
if (useSvg) code += " useSVG\n";
if (distinct) code += " distinct\n";
if (disabled) code += " disabled\n";
if (arrow) code += " enableArrow\n";
Expand All @@ -60,7 +62,7 @@ const Demo = () => {
if (code === "<PhoneInput\n") code = "<PhoneInput />";
else code += "/>";
return code;
}, [distinct, disabled, arrow, search, dropdown, parentheses])
}, [distinct, disabled, useSvg, arrow, search, dropdown, parentheses])

return (
<ThemeProvider theme={theme}>
Expand Down Expand Up @@ -105,6 +107,15 @@ const Demo = () => {
style={{margin: 0}}
label="Parentheses"
/>
<FormControlLabel
control={<Switch
color="primary"
onChange={() => setUseSvg(!useSvg)}
/>}
labelPlacement="start"
style={{margin: 0}}
label="SVG"
/>
</div>
<div style={{gap: 24, display: "flex", alignItems: "center"}}>
<FormControlLabel
Expand Down Expand Up @@ -196,6 +207,7 @@ const Demo = () => {
<PhoneInput
{...phoneProps}
error={error}
useSVG={useSvg}
distinct={distinct}
disabled={disabled}
onChange={onChange}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.5",
"version": "0.1.6",
"name": "mui-phone-input",
"description": "Advanced, highly customizable phone input component for Material UI.",
"keywords": [
Expand Down Expand Up @@ -48,7 +48,7 @@
"react": "^16.8.6 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
"react-phone-hooks": "^0.1.14"
"react-phone-hooks": "^0.1.17"
},
"devDependencies": {
"@babel/core": "^7.26.0",
Expand Down
3 changes: 2 additions & 1 deletion src/base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const Input = forwardRef<HTMLInputElement, InputProps>(({slotProps, ...props}, r
const PhoneInput = forwardRef(({
value: initialValue = "",
country = getDefaultISO2Code(),
useSVG = false,
disableParentheses = false,
onlyCountries = [],
excludeCountries = [],
Expand Down Expand Up @@ -138,7 +139,7 @@ const PhoneInput = forwardRef(({
onInput={onInput}
onChange={onChange}
onKeyDown={onKeyDown}
startAdornment={<div className={`flag ${countryCode}`}/>}
startAdornment={<div className={`flag ${countryCode} ${useSVG ? "svg" : ""}`}/>}
{...(muiInputProps as any)}
/>
)
Expand Down
2 changes: 2 additions & 0 deletions src/base/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type PhoneNumber = types.PhoneNumber;
export interface PhoneInputProps extends Omit<InputProps, "onChange"> {
value?: PhoneNumber | string;

useSVG?: boolean;

country?: string;

disableParentheses?: boolean;
Expand Down
5 changes: 3 additions & 2 deletions src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const PhoneInput = forwardRef(({
variant = undefined,
searchVariant = undefined,
country = getDefaultISO2Code(),
useSVG = false,
distinct = false,
disabled = false,
enableArrow = false,
Expand Down Expand Up @@ -186,7 +187,7 @@ const PhoneInput = forwardRef(({
setTimeout(() => input.focus(), 100);
}}
children={<div className="mui-phone-input-select-item">
<div className={`flag ${iso}`}/>
<div className={`flag ${iso} ${useSVG ? "svg" : ""}`}/>
<div className="label">
{countries[name]}&nbsp;{displayFormat(mask)}
</div>
Expand Down Expand Up @@ -219,7 +220,7 @@ const PhoneInput = forwardRef(({
}}
onClick={() => setOpen(!open)}
>
<div className={`flag ${countryCode}`}/>
<div className={`flag ${countryCode} ${useSVG ? "svg" : ""}`}/>
{enableArrow && (
<svg viewBox="0 0 24 24" focusable="false" fill="currentColor"
style={{paddingLeft: 4}} width="22" height="20">
Expand Down
2 changes: 2 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type PhoneNumber = types.PhoneNumber;
export interface PhoneInputProps extends Omit<TextFieldProps, "onChange"> {
value?: PhoneNumber | string;

useSVG?: boolean;

searchVariant?: TextFieldProps["variant"];

country?: string;
Expand Down
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PhoneInput = forwardRef(({
variant = undefined,
searchVariant = undefined,
country = getDefaultISO2Code(),
useSVG = false,
distinct = false,
disabled = false,
enableArrow = false,
Expand Down Expand Up @@ -181,7 +182,7 @@ const PhoneInput = forwardRef(({
setTimeout(() => input.focus(), 100);
}}
children={<div className="mui-phone-input-select-item">
<div className={`flag ${iso}`}/>
<div className={`flag ${iso} ${useSVG ? "svg" : ""}`}/>
<div className="label">
{countries[name]}&nbsp;{displayFormat(mask)}
</div>
Expand Down Expand Up @@ -214,7 +215,7 @@ const PhoneInput = forwardRef(({
}}
onClick={() => setOpen(!open)}
>
<div className={`flag ${countryCode}`}/>
<div className={`flag ${countryCode} ${useSVG ? "svg" : ""}`}/>
{enableArrow && (
<svg viewBox="0 0 24 24" focusable="false" fill="currentColor"
style={{paddingLeft: 4}} width="22" height="20">
Expand Down
5 changes: 3 additions & 2 deletions src/joy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PhoneInput = forwardRef(({
variant = undefined,
searchVariant = undefined,
country = getDefaultISO2Code(),
useSVG = false,
distinct = false,
disabled = false,
enableArrow = false,
Expand Down Expand Up @@ -178,7 +179,7 @@ const PhoneInput = forwardRef(({
setTimeout(() => input.focus(), 100);
}}
children={<div className="mui-phone-input-select-item">
<div className={`flag ${iso}`}/>
<div className={`flag ${iso} ${useSVG ? "svg" : ""}`}/>
<div className="label">
{countries[name]}&nbsp;{displayFormat(mask)}
</div>
Expand Down Expand Up @@ -209,7 +210,7 @@ const PhoneInput = forwardRef(({
}}
onClick={() => setOpen(!open)}
>
<div className={`flag ${countryCode}`}/>
<div className={`flag ${countryCode} ${useSVG ? "svg" : ""}`}/>
{enableArrow && (
<svg viewBox="0 0 24 24" focusable="false" fill="currentColor"
style={{paddingLeft: 4}} width="22" height="20">
Expand Down
2 changes: 2 additions & 0 deletions src/joy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type PhoneNumber = types.PhoneNumber;
export interface PhoneInputProps extends Omit<InputProps, "value" | "onChange"> {
value?: PhoneNumber | string;

useSVG?: boolean;

searchVariant?: InputProps["variant"];

country?: string;
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type PhoneNumber = types.PhoneNumber;
export interface PhoneInputProps extends Omit<TextFieldProps, "onChange"> {
value?: PhoneNumber | string;

useSVG?: boolean;

searchVariant?: TextFieldProps["variant"];

country?: string;
Expand Down