Это не оптимальное решение, но связывает обратно IntlTelInput с setFieldTouched и setFieldValue от formik.
// @flow
import React, {Component, Fragment} from 'react';
import {ErrorMessage, Field} from 'formik';
import IntlTelInput from 'react-intl-tel-input';
export default class MobileField extends Component {
formatPhoneNumberOutput(
isValid: boolean,
newNumber: string,
countryData: Object,
fullNumber: string,
isExtension: boolean
) {
if (isValid && fullNumber) {
return fullNumber.replace(/(\s|-)/g, '');
}
return 'invalid_phone_number'; // caught by validator
}
render() {
return (
<Field
name={name}
render={({field, form: {errors, isSubmitting, touched, setFieldTouched, setFieldValue}}) => {
return (
<Fragment>
<IntlTelInput
defaultCountry="fr"
defaultValue={field.value}
disabled={isSubmitting}
fieldId={name}
fieldName={name}
onPhoneNumberBlur={() => {
setFieldTouched(name, true);
}}
onPhoneNumberChange={(...args) => {
setFieldValue(name, this.formatPhoneNumberOutput(...args));
}}
preferredCountries={['fr', 'gb', 'es', 'be', 'de']}
/>
<ErrorMessage name={name} render={msg => <p>{msg}</p>} />
</Fragment>
);
}}
/>
);
}
}
использует валидатор, например validate.js, чтобы проверить, что номер телефона не является "invalid_phone_number"
// @flow
import _mapValues from 'lodash/mapValues';
import validate from 'validate.js';
export type Values = {
mobile: string,
landline: string
};
export default (values: Values) => {
const options = {
fullMessages: false
};
const validation: {[key: string]: string[]} = validate(
values,
{
mobile: {
presence: {message: 'Please add a mobile phone number'},
format: {
pattern: '^((?!invalid_phone_number).)*$', // is not invalid_phone_number
message: 'This phone number looks like being invalid'
}
},
landline: {}
},
options
);
return _mapValues(validation, messages => messages[0]);
};