scratch-www/src/components/forms/phone-input.jsx

86 lines
3 KiB
React
Raw Normal View History

const allCountries = require('country-telephone-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;
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
const allIso2 = allCountries.map(country => (country.iso2));
require('./row.scss');
2016-06-23 07:27:43 -04:00
require('./phone-input.scss');
const PhoneInput = createReactClass({ // eslint-disable-line react/prefer-es6-class
2016-06-02 15:25:02 -04:00
displayName: 'PhoneInput',
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
},
flagsImagePath: '/images/flags.png',
defaultCountry: 'us'
2016-06-02 15:25:02 -04: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 () {
let defaultCountry = PhoneInput.getDefaultProps().defaultCountry;
if (allIso2.indexOf(this.props.defaultCountry.toLowerCase()) !== -1) {
defaultCountry = this.props.defaultCountry.toLowerCase();
}
2016-06-02 15:25:02 -04:00
return (
<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">
<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
/>
{this.renderHelp()}
{this.renderErrorMessage()}
2016-06-02 15:25:02 -04:00
</div>
</Row>
);
}
});
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
});
module.exports = inputHOC(defaultValidationHOC(phoneValidationHOC(PhoneInput)));