|
| 1 | +import {ChangeEvent, forwardRef, KeyboardEvent, useCallback, useEffect, useRef, useState} from "react"; |
| 2 | +import {Input as BaseInput, InputProps} from "@mui/base/Input"; |
| 3 | + |
| 4 | +import { |
| 5 | + checkValidity, |
| 6 | + getDefaultISO2Code, |
| 7 | + getFormattedNumber, |
| 8 | + getRawValue, |
| 9 | + parsePhoneNumber, |
| 10 | + useMask, |
| 11 | + usePhone, |
| 12 | +} from "../../phone-hooks"; |
| 13 | + |
| 14 | +import {injectMergedStyles} from "./styles"; |
| 15 | +import {PhoneInputProps, PhoneNumber} from "./types"; |
| 16 | + |
| 17 | +injectMergedStyles(); |
| 18 | + |
| 19 | +const Input = forwardRef<HTMLInputElement, InputProps>(({slotProps, ...props}, ref) => { |
| 20 | + const defaultInputProps = (slotProps?.input as any)?.className ? {} : {outline: "none", border: "none", paddingLeft: 5, width: "calc(100% - 30px)"}; |
| 21 | + const defaultRootProps = (slotProps?.root as any)?.className ? {} : {background: "white", color: "black", paddingLeft: 5}; |
| 22 | + return ( |
| 23 | + <BaseInput |
| 24 | + ref={ref} |
| 25 | + {...props} |
| 26 | + slotProps={{ |
| 27 | + ...slotProps, |
| 28 | + root: { |
| 29 | + ...slotProps?.root, |
| 30 | + style: { |
| 31 | + ...defaultRootProps, |
| 32 | + ...(slotProps?.root as any)?.style, |
| 33 | + alignItems: "center", |
| 34 | + display: "flex", |
| 35 | + } |
| 36 | + }, |
| 37 | + input: { |
| 38 | + ...slotProps?.input, |
| 39 | + style: { |
| 40 | + ...defaultInputProps, |
| 41 | + ...(slotProps?.input as any)?.style, |
| 42 | + } |
| 43 | + } |
| 44 | + }} |
| 45 | + /> |
| 46 | + ) |
| 47 | +}) |
| 48 | + |
| 49 | +const PhoneInput = forwardRef(({ |
| 50 | + value: initialValue = "", |
| 51 | + country = getDefaultISO2Code(), |
| 52 | + onlyCountries = [], |
| 53 | + excludeCountries = [], |
| 54 | + preferredCountries = [], |
| 55 | + onMount: handleMount = () => null, |
| 56 | + onInput: handleInput = () => null, |
| 57 | + onChange: handleChange = () => null, |
| 58 | + onKeyDown: handleKeyDown = () => null, |
| 59 | + ...muiInputProps |
| 60 | + }: PhoneInputProps, ref: any) => { |
| 61 | + const initiatedRef = useRef<boolean>(false); |
| 62 | + const [countryCode, setCountryCode] = useState<string>(country); |
| 63 | + |
| 64 | + const { |
| 65 | + value, |
| 66 | + pattern, |
| 67 | + metadata, |
| 68 | + setValue, |
| 69 | + countriesList, |
| 70 | + } = usePhone({ |
| 71 | + country, |
| 72 | + countryCode, |
| 73 | + initialValue, |
| 74 | + onlyCountries, |
| 75 | + excludeCountries, |
| 76 | + preferredCountries, |
| 77 | + }); |
| 78 | + |
| 79 | + const { |
| 80 | + onInput: onInputMaskHandler, |
| 81 | + onKeyDown: onKeyDownMaskHandler, |
| 82 | + } = useMask(pattern); |
| 83 | + |
| 84 | + const onKeyDown = useCallback((event: KeyboardEvent<HTMLInputElement>) => { |
| 85 | + onKeyDownMaskHandler(event); |
| 86 | + handleKeyDown(event); |
| 87 | + }, [handleKeyDown, onKeyDownMaskHandler]) |
| 88 | + |
| 89 | + const onChange = useCallback((event: ChangeEvent<HTMLInputElement>) => { |
| 90 | + const formattedNumber = getFormattedNumber(event.target.value, pattern); |
| 91 | + const phoneMetadata = parsePhoneNumber(formattedNumber, countriesList); |
| 92 | + setCountryCode(phoneMetadata.isoCode as any); |
| 93 | + setValue(formattedNumber); |
| 94 | + handleChange({...phoneMetadata, valid: (strict: boolean) => checkValidity(phoneMetadata, strict)}, event); |
| 95 | + }, [countriesList, handleChange, pattern, setValue]) |
| 96 | + |
| 97 | + const onInput = useCallback((event: ChangeEvent<HTMLInputElement>) => { |
| 98 | + onInputMaskHandler(event); |
| 99 | + handleInput(event); |
| 100 | + }, [handleInput, onInputMaskHandler]) |
| 101 | + |
| 102 | + const onMount = useCallback((value: PhoneNumber) => { |
| 103 | + handleMount(value); |
| 104 | + }, [handleMount]) |
| 105 | + |
| 106 | + useEffect(() => { |
| 107 | + if (initiatedRef.current) return; |
| 108 | + initiatedRef.current = true; |
| 109 | + let initialValue = getRawValue(value); |
| 110 | + if (!initialValue.startsWith(metadata?.[2] as string)) { |
| 111 | + initialValue = metadata?.[2] as string; |
| 112 | + } |
| 113 | + const formattedNumber = getFormattedNumber(initialValue, pattern); |
| 114 | + const phoneMetadata = parsePhoneNumber(formattedNumber, countriesList); |
| 115 | + onMount({...phoneMetadata, valid: (strict: boolean) => checkValidity(phoneMetadata, strict)}); |
| 116 | + setCountryCode(phoneMetadata.isoCode as any); |
| 117 | + setValue(formattedNumber); |
| 118 | + }, [countriesList, metadata, onMount, pattern, setValue, value]) |
| 119 | + |
| 120 | + return ( |
| 121 | + <Input |
| 122 | + ref={ref} |
| 123 | + type="tel" |
| 124 | + value={value} |
| 125 | + onInput={onInput} |
| 126 | + onChange={onChange} |
| 127 | + onKeyDown={onKeyDown} |
| 128 | + startAdornment={<div className={`flag ${countryCode}`}/>} |
| 129 | + {...(muiInputProps as any)} |
| 130 | + /> |
| 131 | + ) |
| 132 | +}) |
| 133 | + |
| 134 | +export default PhoneInput; |
0 commit comments