Only require libphonenumber when rendering phone inputs

This commit is contained in:
Paul Kaplan 2021-02-26 10:30:06 -05:00
parent fd26186d8b
commit da2ca40ccf
2 changed files with 16 additions and 13 deletions

View file

@ -17,6 +17,21 @@ const validationHOCFactory = require('./validations.jsx').validationHOCFactory;
require('./row.scss');
require('./phone-input.scss');
const Formsy = require('formsy-react');
const libphonenumber = require('google-libphonenumber');
const phoneNumberUtil = libphonenumber.PhoneNumberUtil.getInstance();
Formsy.addValidationRule('isPhone', (values, value) => {
if (typeof value === 'undefined') return true;
if (value && value.national_number === '+') return true;
try {
const parsed = phoneNumberUtil.parse(value.national_number, value.country_code.iso2);
return phoneNumberUtil.isValidNumber(parsed);
} catch (err) {
return false;
}
});
class PhoneInput extends React.Component {
constructor (props) {
super(props);

View file

@ -1,24 +1,12 @@
const defaults = require('lodash.defaultsdeep');
const intl = require('../../lib/intl.jsx');
const libphonenumber = require('google-libphonenumber');
const omit = require('lodash.omit');
const phoneNumberUtil = libphonenumber.PhoneNumberUtil.getInstance();
const PropTypes = require('prop-types');
const React = require('react');
module.exports.validations = {
notEquals: (values, value, neq) => (value !== neq),
notEqualsField: (values, value, field) => (value !== values[field]),
isPhone: (values, value) => {
if (typeof value === 'undefined') return true;
if (value && value.national_number === '+') return true;
try {
const parsed = phoneNumberUtil.parse(value.national_number, value.country_code.iso2);
return phoneNumberUtil.isValidNumber(parsed);
} catch (err) {
return false;
}
}
notEqualsField: (values, value, field) => (value !== values[field])
};
module.exports.validations.notEqualsUsername = module.exports.validations.notEquals;