From 826ab5c10f3718d575f98e41747b391c2ec3a101 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sat, 29 Feb 2020 23:14:49 -0500 Subject: [PATCH] refactor: extract flagbox --- src/components/CountryList.js | 52 ++++++++++++------------------- src/components/FlagBox.js | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 33 deletions(-) create mode 100644 src/components/FlagBox.js diff --git a/src/components/CountryList.js b/src/components/CountryList.js index e8560bf3f..bee7fa56a 100644 --- a/src/components/CountryList.js +++ b/src/components/CountryList.js @@ -3,6 +3,8 @@ import PropTypes from 'prop-types'; import classNames from 'classnames'; import utils from './utils'; +import FlagBox from './FlagBox' + function partial(fn, ...args) { return fn.bind(fn, ...args); } @@ -79,38 +81,23 @@ export default class CountryList extends Component { preferred: isPreferred, }; const countryClass = classNames(countryClassObj); - const keyPrefix = isPreferred ? 'pref-' : ''; - + const onMouseOverOrFocus = this.props.isMobile ? () => {} : this.handleMouseOver + const keyPrefix = isPreferred ? 'pref-' : '' + return ( -
  • -
    { - this.selectedFlag = selectedFlag; - }} - className="flag-box" - > -
    { - this.selectedFlagInner = selectedFlagInner; - }} - className={`iti-flag ${country.iso2}`} - /> -
    - - {country.name} - -+ - {country.dialCode} - -
  • - ); + onFocus={onMouseOverOrFocus} + flagRef={selectedFlag => { this.selectedFlag = selectedFlag }} + innerFlagRef={selectedFlagInner => { this.selectedFlagInner = selectedFlagInner }} + countryClass={countryClass} + /> + ) }); }; @@ -123,13 +110,12 @@ export default class CountryList extends Component { }; render() { + const { preferredCountries, countries, showDropdown } = this.props let options = ''; - const preferredCountries = this.props.preferredCountries; let preferredOptions = null; - const countries = this.props.countries; const className = classNames({ 'country-list': true, - hide: !this.props.showDropdown, + hide: !showDropdown, }); let divider = null; diff --git a/src/components/FlagBox.js b/src/components/FlagBox.js new file mode 100644 index 000000000..653ab3b22 --- /dev/null +++ b/src/components/FlagBox.js @@ -0,0 +1,58 @@ +import React from 'react' +import PropTypes from 'prop-types' + +const FlagBox = ({ + dialCode, + isoCode, + name, + onMouseOver, + onFocus, + onClick, + flagRef, + innerFlagRef, + countryClass, +}) => ( +
  • +
    +
    +
    + + {name} + + {`+ ${dialCode}`} + +
  • +) + +FlagBox.propTypes = { + dialCode: PropTypes.string.isRequired, + isoCode: PropTypes.string.isRequired, + name: PropTypes.string.isRequired, + onMouseOver: PropTypes.func, + onFocus: PropTypes.func, + onClick: PropTypes.func, + flagRef: PropTypes.func, + innerFlagRef: PropTypes.func, + countryClass: PropTypes.string.isRequired, +} + +FlagBox.defaultProps = { + onFocus: () => {}, + onMouseOver: () => {}, + onClick: () => {}, +} + +export default FlagBox