2018-03-30 08:25:04 -04:00
|
|
|
const bindAll = require('lodash.bindall');
|
2018-01-30 11:53:12 -05:00
|
|
|
const classNames = require('classnames');
|
2018-03-26 10:56:25 -04:00
|
|
|
const formsyComponent = require('formsy-react-components/release/hoc/component').default;
|
2018-01-30 11:53:12 -05:00
|
|
|
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;
|
2018-03-30 13:45:24 -04:00
|
|
|
const Help = require('formsy-react-components/release/components/help').default;
|
|
|
|
const ErrorMessages = require('formsy-react-components/release/components/error-messages').default;
|
2016-08-02 15:23:09 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
const defaultValidationHOC = require('./validations.jsx').defaultValidationHOC;
|
|
|
|
const inputHOC = require('./input-hoc.jsx');
|
|
|
|
const intl = require('../../lib/intl.jsx');
|
|
|
|
const validationHOCFactory = require('./validations.jsx').validationHOCFactory;
|
2016-06-02 15:25:02 -04:00
|
|
|
|
2016-06-13 12:48:58 -04:00
|
|
|
require('./row.scss');
|
2016-06-23 07:27:43 -04:00
|
|
|
require('./phone-input.scss');
|
2016-06-13 12:48:58 -04:00
|
|
|
|
2021-02-26 10:30:06 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-30 08:25:04 -04:00
|
|
|
class PhoneInput extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2018-03-30 11:21:41 -04:00
|
|
|
bindAll(this, [
|
2018-03-30 13:45:24 -04:00
|
|
|
'handleBlur',
|
2018-03-30 12:10:03 -04:00
|
|
|
'handleChange',
|
2018-03-30 13:45:24 -04:00
|
|
|
'handleUpdate'
|
2018-03-30 08:25:04 -04:00
|
|
|
]);
|
|
|
|
}
|
2018-03-30 13:45:24 -04:00
|
|
|
handleUpdate (number, country) {
|
|
|
|
const value = {
|
|
|
|
national_number: number,
|
|
|
|
country_code: country
|
|
|
|
};
|
|
|
|
this.props.onSetValue(value);
|
2018-03-30 08:25:04 -04:00
|
|
|
}
|
2018-03-30 13:45:24 -04:00
|
|
|
handleChange (number, country) {
|
2018-03-30 12:10:03 -04:00
|
|
|
if (this.props.updateOnChange) {
|
2018-03-30 13:45:24 -04:00
|
|
|
this.handleUpdate(number, country);
|
2018-03-30 08:25:04 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 13:45:24 -04:00
|
|
|
handleBlur (number, country) {
|
2018-03-30 12:10:03 -04:00
|
|
|
if (this.props.updateOnBlur) {
|
2018-03-30 13:45:24 -04:00
|
|
|
this.handleUpdate(number, country);
|
2016-06-03 00:01:46 -04:00
|
|
|
}
|
2018-03-30 08:25:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-06-02 15:25:02 -04:00
|
|
|
return (
|
2018-01-30 11:53:12 -05:00
|
|
|
<Row
|
2018-03-30 08:25:04 -04:00
|
|
|
{...this.props}
|
|
|
|
htmlFor={this.props.id}
|
2018-01-30 11:53:12 -05:00
|
|
|
rowClassName={classNames('phone-input', this.props.className)}
|
2016-06-02 15:25:02 -04:00
|
|
|
>
|
|
|
|
<div className="input-group">
|
2018-01-30 11:53:12 -05:00
|
|
|
<ReactPhoneInput
|
|
|
|
className="form-control"
|
2018-03-30 08:25:04 -04:00
|
|
|
defaultCountry={this.props.defaultCountry}
|
|
|
|
flagsImagePath="/images/flags.png"
|
2018-01-30 11:53:12 -05:00
|
|
|
label={null}
|
2018-03-30 08:25:04 -04:00
|
|
|
onBlur={this.handleBlur}
|
2018-03-30 11:21:41 -04:00
|
|
|
onChange={this.handleChange}
|
2018-03-30 13:45:24 -04:00
|
|
|
{...omit(this.props, ['className', 'isValid', 'onChange', 'onBlur', 'value'])}
|
2016-06-02 15:25:02 -04:00
|
|
|
/>
|
|
|
|
</div>
|
2018-03-30 13:45:24 -04:00
|
|
|
{this.props.help ? <Help help={this.props.help} /> : null}
|
|
|
|
{this.props.showErrors ? (
|
|
|
|
<ErrorMessages messages={this.props.errorMessages} />
|
|
|
|
) : null}
|
2016-06-02 15:25:02 -04:00
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
}
|
2018-03-30 08:25:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PhoneInput.defaultProps = {
|
2018-03-30 11:21:41 -04:00
|
|
|
type: 'tel',
|
|
|
|
updateOnChange: true
|
|
|
|
};
|
|
|
|
|
|
|
|
PhoneInput.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
defaultCountry: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
2018-04-02 12:26:12 -04:00
|
|
|
errorMessages: PropTypes.arrayOf(PropTypes.node),
|
|
|
|
help: PropTypes.string,
|
2018-03-30 11:21:41 -04:00
|
|
|
id: PropTypes.string,
|
|
|
|
name: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onSetValue: PropTypes.func,
|
2018-04-02 12:26:12 -04:00
|
|
|
showErrors: PropTypes.bool,
|
2018-03-30 12:10:03 -04:00
|
|
|
updateOnBlur: PropTypes.bool,
|
2018-03-30 13:45:24 -04:00
|
|
|
updateOnChange: PropTypes.bool
|
2018-03-30 08:25:04 -04:00
|
|
|
};
|
2016-06-02 15:25:02 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
const phoneValidationHOC = validationHOCFactory({
|
2016-08-10 07:53:49 -04:00
|
|
|
isPhone: <intl.FormattedMessage id="teacherRegistration.validationPhoneNumber" />
|
2016-06-02 15:25:02 -04:00
|
|
|
});
|
|
|
|
|
2018-03-30 13:45:24 -04:00
|
|
|
const ValidatedPhoneInput = inputHOC(defaultValidationHOC(phoneValidationHOC(formsyComponent(PhoneInput))));
|
|
|
|
|
|
|
|
ValidatedPhoneInput.defaultProps = {
|
|
|
|
validations: {
|
|
|
|
isPhone: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ValidatedPhoneInput.propTypes = {
|
|
|
|
validations: PropTypes.objectOf(PropTypes.bool)
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ValidatedPhoneInput;
|