const bindAll = require('lodash.bindall');
const classNames = require('classnames');
const formsyComponent = require('formsy-react-components/release/hoc/component').default;
const omit = require('lodash.omit');
const PropTypes = require('prop-types');
const React = require('react');
const ReactPhoneInput = require('react-telephone-input/lib/withStyles').default;
const Row = require('formsy-react-components').Row;
const Help = require('formsy-react-components/release/components/help').default;
const ErrorMessages = require('formsy-react-components/release/components/error-messages').default;
const defaultValidationHOC = require('./validations.jsx').defaultValidationHOC;
const inputHOC = require('./input-hoc.jsx');
const intl = require('../../lib/intl.jsx');
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);
bindAll(this, [
'handleBlur',
'handleChange',
'handleUpdate'
]);
}
handleUpdate (number, country) {
const value = {
national_number: number,
country_code: country
};
this.props.onSetValue(value);
}
handleChange (number, country) {
if (this.props.updateOnChange) {
this.handleUpdate(number, country);
}
}
handleBlur (number, country) {
if (this.props.updateOnBlur) {
this.handleUpdate(number, country);
}
}
render () {
return (
{this.props.help ? : null}
{this.props.showErrors ? (
) : null}
);
}
}
PhoneInput.defaultProps = {
type: 'tel',
updateOnChange: true
};
PhoneInput.propTypes = {
className: PropTypes.string,
defaultCountry: PropTypes.string,
disabled: PropTypes.bool,
errorMessages: PropTypes.arrayOf(PropTypes.node),
help: PropTypes.string,
id: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func,
onSetValue: PropTypes.func,
showErrors: PropTypes.bool,
updateOnBlur: PropTypes.bool,
updateOnChange: PropTypes.bool
};
const phoneValidationHOC = validationHOCFactory({
isPhone:
});
const ValidatedPhoneInput = inputHOC(defaultValidationHOC(phoneValidationHOC(formsyComponent(PhoneInput))));
ValidatedPhoneInput.defaultProps = {
validations: {
isPhone: true
}
};
ValidatedPhoneInput.propTypes = {
validations: PropTypes.objectOf(PropTypes.bool)
};
module.exports = ValidatedPhoneInput;