File size: 3,775 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import { FormLabel } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { find } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import FormCountrySelect from 'calypso/components/forms/form-country-select';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormTelInput from 'calypso/components/forms/form-tel-input';
import phoneValidation from 'calypso/lib/phone-validation';
const CLEAN_REGEX = /^0|[\s.\-()]+/g;
const noop = () => {};
export class FormPhoneInput extends Component {
static propTypes = {
initialCountryCode: PropTypes.string,
initialPhoneNumber: PropTypes.string,
countriesList: PropTypes.array.isRequired,
isDisabled: PropTypes.bool,
countrySelectProps: PropTypes.object,
phoneInputProps: PropTypes.object,
onChange: PropTypes.func,
translate: PropTypes.func,
};
static defaultProps = {
isDisabled: false,
countrySelectProps: {},
phoneInputProps: {},
onChange: noop,
};
state = {
countryCode: this.props.initialCountryCode || '',
phoneNumber: this.props.initialPhoneNumber || '',
};
componentDidMount() {
this.maybeSetCountryStateFromList();
}
componentDidUpdate() {
this.maybeSetCountryStateFromList();
}
render() {
return (
<div className={ clsx( this.props.className, 'form-phone-input' ) }>
<FormFieldset className="form-phone-input__country">
<FormLabel htmlFor="country_code">
{ this.props.translate( 'Country code', {
context: 'The country code for the phone for the user.',
} ) }
</FormLabel>
<FormCountrySelect
{ ...this.props.countrySelectProps }
countriesList={ this.props.countriesList }
disabled={ this.props.isDisabled }
name="country_code"
value={ this.state.countryCode }
onChange={ this.handleCountryChange }
/>
</FormFieldset>
<FormFieldset className="form-phone-input__phone-number">
<FormLabel htmlFor="phone_number">{ this.props.translate( 'Phone number' ) }</FormLabel>
<FormTelInput
{ ...this.props.phoneInputProps }
disabled={ this.props.isDisabled }
name="phone_number"
value={ this.state.phoneNumber }
onChange={ this.handlePhoneChange }
/>
</FormFieldset>
</div>
);
}
getCountryData() {
// TODO: move this to country-list or FormCountrySelect
return find( this.props.countriesList, {
code: this.state.countryCode,
} );
}
handleCountryChange = ( event ) => {
this.setState( { countryCode: event.target.value }, this.triggerOnChange );
};
handlePhoneChange = ( event ) => {
this.setState( { phoneNumber: event.target.value }, this.triggerOnChange );
};
triggerOnChange = () => {
this.props.onChange( this.getValue() );
};
cleanNumber( number ) {
return number.replace( CLEAN_REGEX, '' );
}
// Set the default state of the country code selector, if not already set
maybeSetCountryStateFromList() {
if ( this.state.countryCode ) {
return;
}
const { countriesList } = this.props;
if ( ! countriesList.length ) {
return;
}
this.setState( {
countryCode: countriesList[ 0 ].code,
} );
}
validate( number ) {
return phoneValidation( number );
}
getValue() {
const countryData = this.getCountryData();
const numberClean = this.cleanNumber( this.state.phoneNumber );
const countryNumericCode = countryData ? countryData.numeric_code : '';
const numberFull = countryNumericCode + numberClean;
const isValid = this.validate( numberFull );
return {
isValid: ! isValid.error,
validation: isValid,
countryData: countryData,
phoneNumber: numberClean,
phoneNumberFull: numberFull,
};
}
}
export default localize( FormPhoneInput );
|