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