2018-01-30 11:53:12 -05:00
|
|
|
const allCountries = require('react-telephone-input/lib/country_data').allCountries;
|
|
|
|
const classNames = require('classnames');
|
|
|
|
const ComponentMixin = require('formsy-react-components').ComponentMixin;
|
|
|
|
const createReactClass = require('create-react-class');
|
|
|
|
const FormsyMixin = require('formsy-react').Mixin;
|
|
|
|
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;
|
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
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
const allIso2 = allCountries.map(country => (country.iso2));
|
2016-06-03 00:01:46 -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
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
const PhoneInput = createReactClass({ // eslint-disable-line react/prefer-es6-class
|
2016-06-02 15:25:02 -04:00
|
|
|
displayName: 'PhoneInput',
|
2018-01-30 11:53:12 -05:00
|
|
|
propTypes: {
|
|
|
|
className: PropTypes.string,
|
|
|
|
defaultCountry: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
name: PropTypes.string,
|
|
|
|
onChange: PropTypes.func
|
|
|
|
},
|
2016-06-02 15:25:02 -04:00
|
|
|
mixins: [
|
|
|
|
FormsyMixin,
|
|
|
|
ComponentMixin
|
|
|
|
],
|
|
|
|
getDefaultProps: function () {
|
|
|
|
return {
|
|
|
|
validations: {
|
2016-08-10 07:53:49 -04:00
|
|
|
isPhone: true
|
2016-06-02 15:25:02 -04:00
|
|
|
},
|
2016-06-03 00:01:46 -04:00
|
|
|
flagsImagePath: '/images/flags.png',
|
|
|
|
defaultCountry: 'us'
|
2016-06-02 15:25:02 -04:00
|
|
|
};
|
|
|
|
},
|
2018-01-30 11:53:12 -05:00
|
|
|
handleChangeInput: function (number, country) {
|
|
|
|
const value = {
|
|
|
|
national_number: number,
|
|
|
|
country_code: country
|
|
|
|
};
|
2016-06-02 15:25:02 -04:00
|
|
|
this.setValue(value);
|
|
|
|
this.props.onChange(this.props.name, value);
|
|
|
|
},
|
|
|
|
render: function () {
|
2018-01-30 11:53:12 -05:00
|
|
|
let defaultCountry = PhoneInput.getDefaultProps().defaultCountry;
|
2016-06-03 00:01:46 -04:00
|
|
|
if (allIso2.indexOf(this.props.defaultCountry.toLowerCase()) !== -1) {
|
2018-01-30 11:53:12 -05:00
|
|
|
defaultCountry = this.props.defaultCountry.toLowerCase();
|
2016-06-03 00:01:46 -04:00
|
|
|
}
|
2016-06-02 15:25:02 -04:00
|
|
|
return (
|
2018-01-30 11:53:12 -05:00
|
|
|
<Row
|
|
|
|
htmlFor={this.getId()}
|
|
|
|
rowClassName={classNames('phone-input', this.props.className)}
|
|
|
|
{...this.getRowProperties()}
|
2016-06-02 15:25:02 -04:00
|
|
|
>
|
|
|
|
<div className="input-group">
|
2018-01-30 11:53:12 -05:00
|
|
|
<ReactPhoneInput
|
|
|
|
className="form-control"
|
|
|
|
defaultCountry={defaultCountry}
|
|
|
|
disabled={this.isFormDisabled() || this.props.disabled}
|
|
|
|
id={this.getId()}
|
|
|
|
label={null}
|
|
|
|
onChange={this.handleChangeInput}
|
|
|
|
{...omit(this.props, ['className', 'disabled', 'onChange'])}
|
2016-06-02 15:25:02 -04:00
|
|
|
/>
|
2016-07-26 08:06:38 -04:00
|
|
|
{this.renderHelp()}
|
2016-07-25 15:43:44 -04:00
|
|
|
{this.renderErrorMessage()}
|
2016-06-02 15:25:02 -04:00
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2016-06-13 12:48:58 -04:00
|
|
|
module.exports = inputHOC(defaultValidationHOC(phoneValidationHOC(PhoneInput)));
|