UNPKG

@wulperstudio/cms

Version:
199 lines (198 loc) 8.19 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getInitialState = getInitialState; exports.usePhoneDigits = usePhoneDigits; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _libphonenumberJs = require("libphonenumber-js"); var _react = _interopRequireDefault(require("react")); var _constants = require("../constants"); var _helpers = require("../helpers"); function getInitialState(params) { var _COUNTRIES$defaultCou; var defaultCountry = params.defaultCountry, initialValue = params.initialValue, disableFormatting = params.disableFormatting; var fallbackValue = defaultCountry ? "+".concat((_COUNTRIES$defaultCou = _constants.COUNTRIES[defaultCountry]) == null ? void 0 : _COUNTRIES$defaultCou[0]) : ''; var asYouType = new _libphonenumberJs.AsYouType(defaultCountry); var inputValue = asYouType.input(initialValue); var phoneNumberValue = asYouType.getNumberValue(); if (defaultCountry && asYouType.getCountry() === undefined) { inputValue = fallbackValue; } else if (disableFormatting && phoneNumberValue) { inputValue = phoneNumberValue; } return { inputValue: inputValue || fallbackValue, isoCode: asYouType.getCountry() || defaultCountry || null }; } function matchIsIsoCodeAccepted(isoCode, filters) { var excludedCountries = filters.excludedCountries, onlyCountries = filters.onlyCountries, continents = filters.continents; if ((0, _helpers.matchIsArray)(excludedCountries, true) && excludedCountries.includes(isoCode)) { return false; } if ((0, _helpers.matchIsArray)(onlyCountries) && !onlyCountries.includes(isoCode)) { return false; } if ((0, _helpers.matchIsArray)(continents) && !(0, _helpers.matchContinentsIncludeCountry)(continents, isoCode)) { return false; } return true; } function usePhoneDigits(_ref) { var value = _ref.value, onChange = _ref.onChange, defaultCountry = _ref.defaultCountry, forceCallingCode = _ref.forceCallingCode, onlyCountries = _ref.onlyCountries, excludedCountries = _ref.excludedCountries, continents = _ref.continents, disableFormatting = _ref.disableFormatting; var previousCountryRef = _react["default"].useRef(defaultCountry || null); var asYouTypeRef = _react["default"].useRef(new _libphonenumberJs.AsYouType(defaultCountry)); var inputRef = _react["default"].useRef(null); // eslint-disable-next-line max-len var _React$useState = _react["default"].useState(defaultCountry), _React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2), previousDefaultCountry = _React$useState2[0], setPreviousDefaultCountry = _React$useState2[1]; var _React$useState3 = _react["default"].useState(function () { return getInitialState({ initialValue: value, defaultCountry: defaultCountry, disableFormatting: disableFormatting }); }), _React$useState4 = (0, _slicedToArray2["default"])(_React$useState3, 2), state = _React$useState4[0], setState = _React$useState4[1]; var _React$useState5 = _react["default"].useState(value), _React$useState6 = (0, _slicedToArray2["default"])(_React$useState5, 2), previousValue = _React$useState6[0], setPreviousValue = _React$useState6[1]; var buildOnChangeInfo = function buildOnChangeInfo(reason) { return { countryCallingCode: asYouTypeRef.current.getCallingCode() || null, countryCode: asYouTypeRef.current.getCountry() || null, nationalNumber: asYouTypeRef.current.getNationalNumber(), numberValue: asYouTypeRef.current.getNumberValue() || null, reason: reason }; }; var matchIsIsoCodeValid = function matchIsIsoCodeValid(isoCode) { return isoCode && matchIsIsoCodeAccepted(isoCode, { onlyCountries: onlyCountries, excludedCountries: excludedCountries, continents: continents }); }; var typeNewValue = function typeNewValue(inputValue) { asYouTypeRef.current.reset(); return asYouTypeRef.current.input(inputValue); }; var onInputChange = function onInputChange(event) { var inputVal = event.target.value; inputVal = inputVal.startsWith('+') || inputVal === '' ? inputVal : "+".concat(inputVal); var formattedValue = typeNewValue(inputVal); var country = inputVal === '+' || !inputVal ? null : asYouTypeRef.current.getCountry() || previousCountryRef.current || null; previousCountryRef.current = country; var phoneNumber = asYouTypeRef.current.getNumber() || null; var numberValue = asYouTypeRef.current.getNumberValue() || ''; if (forceCallingCode && !phoneNumber && (state.isoCode || defaultCountry)) { var inputValueIsoCode = "+".concat((0, _helpers.getCallingCodeOfCountry)(state.isoCode || defaultCountry)); onChange == null || onChange(inputValueIsoCode, buildOnChangeInfo('input')); setPreviousValue(inputValueIsoCode); setState({ isoCode: state.isoCode || defaultCountry || null, inputValue: inputValueIsoCode }); } else if (numberValue && (!country || !matchIsIsoCodeValid(country))) { // Do not format when isoCode is not valid onChange == null || onChange(numberValue, Object.assign({}, buildOnChangeInfo('input'), { countryCode: null, countryCallingCode: null, nationalNumber: null })); setPreviousValue(numberValue); setState({ isoCode: null, inputValue: numberValue }); } else if (disableFormatting) { onChange == null || onChange(numberValue, buildOnChangeInfo('input')); setPreviousValue(numberValue); setState({ isoCode: country || null, inputValue: numberValue }); } else { onChange == null || onChange(formattedValue, buildOnChangeInfo('input')); setPreviousValue(formattedValue); setState({ isoCode: country || null, inputValue: formattedValue }); } }; _react["default"].useEffect(function () { if (value !== previousValue) { setPreviousValue(value); var newState = getInitialState({ initialValue: value, defaultCountry: defaultCountry }); previousCountryRef.current = newState.isoCode; setState(newState); } }, [value, previousValue, defaultCountry]); _react["default"].useEffect(function () { if (defaultCountry !== previousDefaultCountry) { setPreviousDefaultCountry(defaultCountry); asYouTypeRef.current = new _libphonenumberJs.AsYouType(defaultCountry); var _getInitialState = getInitialState({ initialValue: '', defaultCountry: defaultCountry }), inputValue = _getInitialState.inputValue, isoCode = _getInitialState.isoCode; setPreviousValue(inputValue); asYouTypeRef.current.input(inputValue); previousCountryRef.current = asYouTypeRef.current.getCountry() || null; onChange == null || onChange(inputValue, buildOnChangeInfo('country')); setState({ inputValue: inputValue, isoCode: isoCode }); } }, [defaultCountry, previousDefaultCountry, onChange]); var onCountryChange = function onCountryChange(newCountry) { var _COUNTRIES$newCountry; if (newCountry === state.isoCode) { return; } var callingCode = (_COUNTRIES$newCountry = _constants.COUNTRIES[newCountry]) == null ? void 0 : _COUNTRIES$newCountry[0]; var formattedValue = typeNewValue("+".concat(callingCode)); onChange == null || onChange(formattedValue, Object.assign({}, buildOnChangeInfo('country'), { // Some country have the same calling code, so we choose what the user has selected countryCode: newCountry })); previousCountryRef.current = newCountry; setPreviousValue(formattedValue); setState({ isoCode: newCountry, inputValue: formattedValue }); }; return { inputValue: state.inputValue, isoCode: state.isoCode, onInputChange: onInputChange, onCountryChange: onCountryChange, inputRef: inputRef }; }