Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flagIcons prop for custom icons #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ function handleOnChange(value) {
<td> bool </td>
<td colspan="2"> true by default </td>
</tr>
<tr>
<td> flagIcons </td>
<td> object </td>
<td colspan="2"> A dictionary of the countryCode => asset.svg (see country-flag-icons/react/3x2) </td>
</tr>

<tr>
<td colspan="4"><b>Supported TextField props</b></td>
Expand Down
6 changes: 3 additions & 3 deletions src/components/Item.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from 'react';
import PropTypes from 'prop-types';
import MenuItem from '@mui/material/MenuItem';
import Flags from 'country-flag-icons/react/3x2'

class Item extends React.PureComponent {
render() {
const ref = React.createRef();
const {
name, iso2, dialCode, localization,
itemRef, native, className = '', ...restProps
itemRef, native, flagIcons, className = '', ...restProps
} = this.props;

if (native) {
Expand All @@ -27,7 +26,7 @@ class Item extends React.PureComponent {
);
}

const FlagComponent = Flags[iso2.toUpperCase()];
const FlagComponent = flagIcons[iso2.toUpperCase()];

return (
<MenuItem
Expand Down Expand Up @@ -60,6 +59,7 @@ Item.propTypes = {
itemRef: PropTypes.func.isRequired,
localization: PropTypes.string,
native: PropTypes.bool,
flagIcons: PropTypes.object,
};

Item.defaultProps = {
Expand Down
13 changes: 10 additions & 3 deletions src/components/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ class MaterialUiPhoneNumber extends React.Component {

const {
classes, dropdownClass, localization, disableDropdown,
native,
native, flagIcons
} = this.props;

onlyCountries.sort((a, b) => {
Expand All @@ -603,7 +603,8 @@ class MaterialUiPhoneNumber extends React.Component {

const isSelected = (country) => Boolean(selectedCountry && selectedCountry.dialCode === country.dialCode);

const FlagComponent = Flags[selectedCountry.iso2.toUpperCase()];
const flagIconsDictionary = flagIcons || Flags
const FlagComponent = flagIconsDictionary[selectedCountry.iso2.toUpperCase()];

const dropdownProps = disableDropdown ? {} : {
startAdornment: (
Expand Down Expand Up @@ -636,6 +637,7 @@ class MaterialUiPhoneNumber extends React.Component {
iso2={country.iso2}
dialCode={country.dialCode}
localization={localization && localization[country.name]}
flagIcons={flagIconsDictionary}
native
/>
))}
Expand All @@ -649,6 +651,7 @@ class MaterialUiPhoneNumber extends React.Component {
name={country.name}
iso2={country.iso2}
dialCode={country.dialCode}
flagIcons={flagIconsDictionary}
localization={localization && localization[country.name]}
native
/>
Expand Down Expand Up @@ -687,6 +690,7 @@ class MaterialUiPhoneNumber extends React.Component {
iso2={country.iso2}
dialCode={country.dialCode}
localization={localization && localization[country.name]}
flagIcons={flagIconsDictionary}
className={classes.flagIcon}
/>
))}
Expand All @@ -705,6 +709,7 @@ class MaterialUiPhoneNumber extends React.Component {
iso2={country.iso2}
dialCode={country.dialCode}
localization={localization && localization[country.name]}
flagIcons={flagIconsDictionary}
className={classes.flagIcon}
/>
))}
Expand All @@ -729,7 +734,7 @@ class MaterialUiPhoneNumber extends React.Component {
dropdownClass, autoFormat, disableAreaCodes, isValid, disableCountryCode,
disableDropdown, enableLongNumbers, countryCodeEditable, onEnterKeyPress,
isModernBrowser, classes, keys, localization, placeholder, regions, onChange,
value,
value, flagIcons,
// end placeholder props
inputClass, error, InputProps,
...restProps
Expand Down Expand Up @@ -805,6 +810,8 @@ MaterialUiPhoneNumber.propTypes = {
isModernBrowser: PropTypes.func,
onEnterKeyPress: PropTypes.func,
keys: PropTypes.object,

flagIcons: PropTypes.object,
};

MaterialUiPhoneNumber.defaultProps = {
Expand Down
7 changes: 7 additions & 0 deletions src/demo.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { render } from 'react-dom';
import Flags from 'country-flag-icons/react/1x1';
import MaterialUiPhoneNumber from './index';

class ChangeDemo extends React.Component {
Expand Down Expand Up @@ -61,6 +62,12 @@ export default render(
preferredCountries={['it', 'se']}
native
/>
<p>Prop supplied icons</p>
<MaterialUiPhoneNumber
defaultCountry="it"
preferredCountries={['it', 'se']}
flagIcons={Flags}
/>
</div>

<div style={{ display: 'inline-block', marginLeft: '40px' }}>
Expand Down
57 changes: 46 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,13 @@
"resolved" "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.13.1.tgz"
"version" "1.13.1"

"bindings@^1.5.0":
"integrity" "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ=="
"resolved" "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz"
"version" "1.5.0"
dependencies:
"file-uri-to-path" "1.0.0"

"bluebird@^3.5.5":
"integrity" "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg=="
"resolved" "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz"
Expand Down Expand Up @@ -2854,6 +2861,11 @@
"jsx-ast-utils" "^3.1.0"
"language-tags" "^1.0.5"

"eslint-plugin-react-hooks@^4 || ^3 || ^2.3.0 || ^1.7.0":
"integrity" "sha512-XslZy0LnMn+84NEG9jSGR6eGqaZB3133L8xewQo3fQagbQuGt7a63gf+P1NGKZavEYEC3UXaWEAA/AqDkuN6xA=="
"resolved" "https://registry.npmjs.org/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.3.0.tgz"
"version" "4.3.0"

"eslint-plugin-react@^7.21.5", "eslint-plugin-react@^7.26.1":
"integrity" "sha512-Lug0+NOFXeOE+ORZ5pbsh6mSKjBKXDXItUD2sQoT+5Yl0eoT82DqnXeTMfUare4QVCn9QwXbfzO/dBLjLXwVjQ=="
"resolved" "https://registry.npmjs.org/eslint-plugin-react/-/eslint-plugin-react-7.26.1.tgz"
Expand Down Expand Up @@ -2912,7 +2924,7 @@
"resolved" "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.0.0.tgz"
"version" "3.0.0"

"eslint@^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8", "eslint@^3 || ^4 || ^5 || ^6 || ^7", "eslint@^5.16.0 || ^6.8.0 || ^7.2.0", "eslint@^8.0.0", "eslint@>= 4.12.1", "eslint@>=5":
"eslint@^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8", "eslint@^3 || ^4 || ^5 || ^6 || ^7", "eslint@^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0", "eslint@^5.16.0 || ^6.8.0 || ^7.2.0", "eslint@^8.0.0", "eslint@>= 4.12.1", "eslint@>=5":
"integrity" "sha512-03spzPzMAO4pElm44m60Nj08nYonPGQXmw6Ceai/S4QK82IgwWO1EXx1s9namKzVlbVu3Jf81hb+N+8+v21/HQ=="
"resolved" "https://registry.npmjs.org/eslint/-/eslint-8.0.0.tgz"
"version" "8.0.0"
Expand Down Expand Up @@ -3182,6 +3194,11 @@
"loader-utils" "^2.0.0"
"schema-utils" "^3.0.0"

"[email protected]":
"integrity" "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
"resolved" "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz"
"version" "1.0.0"

"fill-range@^4.0.0":
"integrity" "sha1-1USBHUKPmOsGpj3EAtJAPDKMOPc="
"resolved" "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz"
Expand Down Expand Up @@ -3321,6 +3338,14 @@
"resolved" "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
"version" "1.0.0"

"fsevents@^1.2.7":
"integrity" "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw=="
"resolved" "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz"
"version" "1.2.13"
dependencies:
"bindings" "^1.5.0"
"nan" "^2.12.1"

"function-bind@^1.1.1":
"integrity" "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
"resolved" "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"
Expand Down Expand Up @@ -4207,14 +4232,7 @@
"resolved" "https://registry.npmjs.org/killable/-/killable-1.0.1.tgz"
"version" "1.0.1"

"kind-of@^3.0.2", "kind-of@^3.0.3":
"integrity" "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ="
"resolved" "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz"
"version" "3.2.2"
dependencies:
"is-buffer" "^1.1.5"

"kind-of@^3.2.0":
"kind-of@^3.0.2", "kind-of@^3.0.3", "kind-of@^3.2.0":
"integrity" "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ="
"resolved" "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz"
"version" "3.2.2"
Expand All @@ -4233,7 +4251,12 @@
"resolved" "https://registry.npmjs.org/kind-of/-/kind-of-5.1.0.tgz"
"version" "5.1.0"

"kind-of@^6.0.0", "kind-of@^6.0.2":
"kind-of@^6.0.0":
"integrity" "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw=="
"resolved" "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz"
"version" "6.0.3"

"kind-of@^6.0.2":
"integrity" "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw=="
"resolved" "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz"
"version" "6.0.3"
Expand Down Expand Up @@ -4623,6 +4646,11 @@
"dns-packet" "^1.3.1"
"thunky" "^1.0.2"

"nan@^2.12.1":
"integrity" "sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ=="
"resolved" "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz"
"version" "2.15.0"

"nanocolors@^0.1.12":
"integrity" "sha512-2nMHqg1x5PU+unxX7PGY7AuYxl2qDx7PSrTRjizr8sxdd3l/3hBuWWaki62qmtYm2U5i4Z5E7GbjlyDFhs9/EQ=="
"resolved" "https://registry.npmjs.org/nanocolors/-/nanocolors-0.1.12.tgz"
Expand Down Expand Up @@ -5822,7 +5850,14 @@
"resolved" "https://registry.npmjs.org/retry/-/retry-0.12.0.tgz"
"version" "0.12.0"

"rimraf@^2.5.4", "rimraf@^2.6.3":
"rimraf@^2.5.4":
"integrity" "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA=="
"resolved" "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz"
"version" "2.6.3"
dependencies:
"glob" "^7.1.3"

"rimraf@^2.6.3":
"integrity" "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA=="
"resolved" "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz"
"version" "2.6.3"
Expand Down