UNPKG

react-phone-number-input

Version:

Telephone number input React component

128 lines (117 loc) 5.09 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import React, { useCallback, useMemo } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import getUnicodeFlagIcon from 'country-flag-icons/unicode'; export default function CountrySelect(_ref) { var value = _ref.value, onChange = _ref.onChange, options = _ref.options, rest = _objectWithoutProperties(_ref, ["value", "onChange", "options"]); var onChange_ = useCallback(function (event) { var value = event.target.value; onChange(value === 'ZZ' ? undefined : value); }, [onChange]); var selectedOption = useMemo(function () { return getSelectedOption(options, value); }, [options, value]); // "ZZ" means "International". // (HTML requires each `<option/>` have some string `value`). return React.createElement("select", _extends({}, rest, { value: value || 'ZZ', onChange: onChange_ }), options.map(function (_ref2) { var value = _ref2.value, label = _ref2.label, divider = _ref2.divider; return React.createElement("option", { key: divider ? '|' : value || 'ZZ', value: divider ? '|' : value || 'ZZ', disabled: divider ? true : false, style: divider ? DIVIDER_STYLE : undefined }, label); })); } CountrySelect.propTypes = { /** * A two-letter country code. * Example: "US", "RU", etc. */ value: PropTypes.string, /** * Updates the `value`. */ onChange: PropTypes.func.isRequired, // `<select/>` options. options: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.string, label: PropTypes.string, divider: PropTypes.bool })).isRequired }; var DIVIDER_STYLE = { fontSize: '1px', backgroundColor: 'currentColor', color: 'inherit' }; export function CountrySelectWithIcon(_ref3) { var value = _ref3.value, options = _ref3.options, className = _ref3.className, Icon = _ref3.iconComponent, getIconAspectRatio = _ref3.getIconAspectRatio, Arrow = _ref3.arrowComponent, unicodeFlags = _ref3.unicodeFlags, rest = _objectWithoutProperties(_ref3, ["value", "options", "className", "iconComponent", "getIconAspectRatio", "arrowComponent", "unicodeFlags"]); var selectedOption = useMemo(function () { return getSelectedOption(options, value); }, [options, value]); return React.createElement("div", { className: "PhoneInputCountry" }, React.createElement(CountrySelect, _extends({}, rest, { value: value, options: options, className: classNames('PhoneInputCountrySelect', className) })), unicodeFlags && value && React.createElement("div", { className: "PhoneInputCountryIconUnicode" }, getUnicodeFlagIcon(value)), !(unicodeFlags && value) && React.createElement(Icon, { country: value, label: selectedOption && selectedOption.label, aspectRatio: unicodeFlags ? 1 : undefined }), React.createElement(Arrow, null)); } CountrySelectWithIcon.propTypes = { // Country flag component. iconComponent: PropTypes.elementType, // Select arrow component. arrowComponent: PropTypes.elementType.isRequired, // Set to `true` to render Unicode flag icons instead of SVG images. unicodeFlags: PropTypes.bool }; CountrySelectWithIcon.defaultProps = { // Is "International" icon square? arrowComponent: function arrowComponent() { return React.createElement("div", { className: "PhoneInputCountrySelectArrow" }); } }; function getSelectedOption(options, value) { for (var _iterator = options, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref4; if (_isArray) { if (_i >= _iterator.length) break; _ref4 = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref4 = _i.value; } var option = _ref4; if (!option.divider && option.value === value) { return option; } } } //# sourceMappingURL=CountrySelect.js.map