| 1 |
/** |
| 2 |
* External Dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { useState, useEffect } from 'react'; |
| 6 |
import IntlTelInput from 'intl-tel-input/react'; |
| 7 |
import 'intl-tel-input/build/css/intlTelInput.css'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Internal Dependencies |
| 11 |
*/ |
| 12 |
import styles from './styles.module.scss'; |
| 13 |
import { IntlTelInputSettings } from '../../types'; |
| 14 |
|
| 15 |
/** |
| 16 |
* @since 4.4.0 |
| 17 |
*/ |
| 18 |
type PhoneInputProps = { |
| 19 |
id?: string; |
| 20 |
value?: string; |
| 21 |
onChange: (value: string) => void; |
| 22 |
onError?: (errorMessage: string | null) => void; |
| 23 |
className?: string; |
| 24 |
intlTelInputSettings: IntlTelInputSettings; |
| 25 |
}; |
| 26 |
|
| 27 |
/** |
| 28 |
* @since 4.6.1 Add intlTelInputSettings to props |
| 29 |
* @since 4.4.0 |
| 30 |
*/ |
| 31 |
export default function PhoneInput({ id, value, onChange, onError, className, intlTelInputSettings }: PhoneInputProps) { |
| 32 |
const [country, setCountry] = useState<string>(intlTelInputSettings.initialCountry); |
| 33 |
|
| 34 |
const isIntlTelInput = intlTelInputSettings.initialCountry && intlTelInputSettings.utilsScriptUrl; |
| 35 |
|
| 36 |
useEffect(() => { |
| 37 |
if (!isIntlTelInput) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// This timeout is necessary to properly load the utilsScript - without this, the autoPlaceholder feature doesn't work. |
| 42 |
const interval = setTimeout(() => { |
| 43 |
window.intlTelInputGlobals.loadUtils(intlTelInputSettings.utilsScriptUrl); |
| 44 |
// It's necessary to fix a missing left padding that can happen in certain cases. |
| 45 |
document.querySelectorAll('.iti__tel-input').forEach(function (input: HTMLInputElement) { |
| 46 |
// @ts-ignore |
| 47 |
const countryContainerWidth = document.querySelector('.iti__country-container').offsetWidth; |
| 48 |
input.style.paddingLeft = String(countryContainerWidth + 4) + 'px'; |
| 49 |
}); |
| 50 |
}, 100); |
| 51 |
|
| 52 |
return () => { |
| 53 |
clearInterval(interval); |
| 54 |
}; |
| 55 |
}, [isIntlTelInput]); |
| 56 |
|
| 57 |
const onChangeNumber = (number: string) => { |
| 58 |
// Safety check to ensure intlTelInputUtils is available before using it |
| 59 |
if (number && window.intlTelInputUtils && !window.intlTelInputUtils.isValidNumber(number, country)) { |
| 60 |
const errorCode = window.intlTelInputUtils.getValidationError(number, country); |
| 61 |
onChange(String(errorCode)); |
| 62 |
if (onError) { |
| 63 |
onError(intlTelInputSettings.errorMap[errorCode]); |
| 64 |
} |
| 65 |
} else { |
| 66 |
onChange(number); |
| 67 |
if (onError) { |
| 68 |
onError(null); |
| 69 |
} |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
return ( |
| 74 |
<div id={id} className={`${styles.phoneInput} ${className || ''}`}> |
| 75 |
<IntlTelInput |
| 76 |
initialValue={value} |
| 77 |
onChangeCountry={setCountry} |
| 78 |
onChangeNumber={onChangeNumber} |
| 79 |
initOptions={{ |
| 80 |
initialCountry: intlTelInputSettings.initialCountry, |
| 81 |
showSelectedDialCode: intlTelInputSettings.showSelectedDialCode, |
| 82 |
strictMode: intlTelInputSettings.strictMode, |
| 83 |
i18n: intlTelInputSettings.i18n, |
| 84 |
useFullscreenPopup: intlTelInputSettings.useFullscreenPopup, |
| 85 |
utilsScript: intlTelInputSettings.utilsScriptUrl, |
| 86 |
}} |
| 87 |
inputProps={{ |
| 88 |
'aria-label': __('Phone number', 'give'), |
| 89 |
}} |
| 90 |
/> |
| 91 |
</div> |
| 92 |
); |
| 93 |
} |
| 94 |
|