| 1 |
import {PhoneProps} from '@givewp/forms/propTypes'; |
| 2 |
import {useEffect, useState} from 'react'; |
| 3 |
import IntlTelInput from 'intl-tel-input/react'; |
| 4 |
import 'intl-tel-input/build/css/intlTelInput.css'; |
| 5 |
import InputMask from 'react-input-mask'; |
| 6 |
import autoCompleteAttr from '@givewp/forms/registrars/templates/fields/utils/autoCompleteAttr'; |
| 7 |
|
| 8 |
/** |
| 9 |
* @since 4.3.0 Add autoComplete support |
| 10 |
* @since 3.9.0 |
| 11 |
*/ |
| 12 |
export default function Phone({ |
| 13 |
Label, |
| 14 |
ErrorMessage, |
| 15 |
placeholder, |
| 16 |
description, |
| 17 |
phoneFormat, |
| 18 |
inputProps, |
| 19 |
fieldError, |
| 20 |
intlTelInputSettings, |
| 21 |
}: PhoneProps) { |
| 22 |
const autoComplete = autoCompleteAttr(inputProps?.name); |
| 23 |
const FieldDescription = window.givewp.form.templates.layouts.fieldDescription; |
| 24 |
const {useFormContext} = window.givewp.form.hooks; |
| 25 |
const {setValue, setError, trigger} = useFormContext(); |
| 26 |
|
| 27 |
const isIntlTelInput = intlTelInputSettings.initialCountry && intlTelInputSettings.utilsScriptUrl; |
| 28 |
|
| 29 |
useEffect(() => { |
| 30 |
if (!isIntlTelInput) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// This timeout is necessary to properly load the utilsScript - without this, the autoPlaceholder feature doesn't work. |
| 35 |
const interval = setTimeout(() => { |
| 36 |
window.intlTelInputGlobals.loadUtils(intlTelInputSettings.utilsScriptUrl); |
| 37 |
// It's necessary to fix a missing left padding that can happen in certain cases. |
| 38 |
document.querySelectorAll('.iti__tel-input').forEach(function (input: HTMLInputElement) { |
| 39 |
// @ts-ignore |
| 40 |
const countryContainerWidth = document.querySelector('.iti__country-container').offsetWidth; |
| 41 |
input.style.paddingLeft = String(countryContainerWidth + 4) + 'px'; |
| 42 |
}); |
| 43 |
}, 100); |
| 44 |
|
| 45 |
return () => { |
| 46 |
clearInterval(interval); |
| 47 |
}; |
| 48 |
}, []); |
| 49 |
|
| 50 |
const [country, setCountry] = useState<string>(intlTelInputSettings.initialCountry); |
| 51 |
|
| 52 |
const onChangeNumber = (number: string) => { |
| 53 |
if (number && !window.intlTelInputUtils.isValidNumber(number, country)) { |
| 54 |
const errorCode = window.intlTelInputUtils.getValidationError(number, country); |
| 55 |
setValue(inputProps.name, errorCode); |
| 56 |
setError(inputProps.name, {type: 'custom', message: intlTelInputSettings.errorMap[errorCode]}); |
| 57 |
} else { |
| 58 |
setValue(inputProps.name, number); |
| 59 |
trigger(inputProps.name, {shouldFocus: false}); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
return ( |
| 64 |
<label> |
| 65 |
<Label /> |
| 66 |
{description && <FieldDescription description={description} />} |
| 67 |
|
| 68 |
{isIntlTelInput ? ( |
| 69 |
<> |
| 70 |
<input type={'hidden'} placeholder={placeholder} {...inputProps} /> |
| 71 |
<IntlTelInput |
| 72 |
onChangeCountry={setCountry} |
| 73 |
onChangeNumber={onChangeNumber} |
| 74 |
initOptions={{ |
| 75 |
initialCountry: intlTelInputSettings.initialCountry, |
| 76 |
showSelectedDialCode: intlTelInputSettings.showSelectedDialCode, |
| 77 |
strictMode: intlTelInputSettings.strictMode, |
| 78 |
i18n: intlTelInputSettings.i18n, |
| 79 |
useFullscreenPopup: intlTelInputSettings.useFullscreenPopup, |
| 80 |
}} |
| 81 |
inputProps={{ |
| 82 |
'aria-invalid': fieldError ? 'true' : 'false', |
| 83 |
}} |
| 84 |
/> |
| 85 |
</> |
| 86 |
) : phoneFormat === 'domestic' ? ( |
| 87 |
<InputMask type={'phone'} {...inputProps} mask={'(999) 999-9999'} placeholder={placeholder} /> |
| 88 |
) : ( |
| 89 |
<input type={'phone'} placeholder={placeholder} {...inputProps} autoComplete={autoComplete} /> |
| 90 |
)} |
| 91 |
|
| 92 |
<ErrorMessage /> |
| 93 |
</label> |
| 94 |
); |
| 95 |
} |
| 96 |
|