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

91 lines
2.8 KiB
React
Raw Normal View History

const bindAll = require('lodash.bindall');
const classNames = require('classnames');
2018-03-26 10:56:25 -04:00
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 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
require('./row.scss');
2016-06-23 07:27:43 -04:00
require('./phone-input.scss');
class PhoneInput extends React.Component {
constructor (props) {
super(props);
2018-03-30 11:21:41 -04:00
bindAll(this, [
'handleChange'
]);
this.state = {value: props.value};
}
componentWillReceiveProps (nextProps) {
if (nextProps.value !== this.state.value) {
this.setState({value: nextProps.value});
this.props.onSetValue(nextProps.value);
}
}
2018-03-30 11:21:41 -04:00
handleChange (telNumber) {
if (this.updateOnChange) {
this.onSetValue(telNumber);
}
}
2018-03-30 11:21:41 -04:00
handleBlur (telNumber) {
if (this.updateOnBlur) {
this.onSetValue(telNumber);
}
}
render () {
2016-06-02 15:25:02 -04:00
return (
<Row
{...this.props}
htmlFor={this.props.id}
rowClassName={classNames('phone-input', this.props.className)}
2016-06-02 15:25:02 -04:00
>
<div className="input-group">
<ReactPhoneInput
className="form-control"
defaultCountry={this.props.defaultCountry}
flagsImagePath="/images/flags.png"
id={this.props.id}
label={null}
onBlur={this.handleBlur}
2018-03-30 11:21:41 -04:00
onChange={this.handleChange}
{...omit(this.props, ['className', 'onChange', 'onBlur'])}
2016-06-02 15:25:02 -04:00
/>
</div>
</Row>
);
}
}
PhoneInput.defaultProps = {
2018-03-30 11:21:41 -04:00
type: 'tel',
value: '',
updateOnChange: true
};
PhoneInput.propTypes = {
className: PropTypes.string,
defaultCountry: PropTypes.string,
disabled: PropTypes.bool,
id: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func,
onSetValue: PropTypes.func,
value: PropTypes.string
};
2016-06-02 15:25:02 -04: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
});
module.exports = inputHOC(phoneValidationHOC(defaultValidationHOC(formsyComponent(PhoneInput))));