|
1 | | -import {useMemo, useState} from "react"; |
2 | | -import ReactPhoneInput from "react-phone-input-2"; |
| 1 | +import {useContext, useEffect, useMemo} from "react"; |
| 2 | +import theme from "antd/lib/theme"; |
| 3 | +import genComponentStyleHook from "antd/lib/input/style"; |
| 4 | +import {FormItemInputContext} from "antd/lib/form/context"; |
| 5 | +import {getStatusClassNames} from "antd/lib/_util/statusUtils"; |
3 | 6 |
|
4 | | -import {ParsePhoneNumber, PhoneInputProps, ReactPhoneOnChange, ReactPhoneOnMount} from "./types"; |
| 7 | +import InputLegacy from "./legacy"; |
| 8 | +import {PhoneInputProps} from "./types"; |
5 | 9 |
|
6 | | -import masks from "./phoneMasks.json"; |
7 | | -import timezones from "./timezones.json"; |
8 | | -import validations from "./validations.json"; |
9 | | - |
10 | | -import "react-phone-input-2/lib/style.css"; |
11 | | - |
12 | | -type ISO2Code = keyof typeof masks; |
13 | | -type Timezone = keyof typeof timezones; |
14 | | - |
15 | | -const getDefaultISO2Code = () => { |
16 | | - /** Returns the default ISO2 code based on the user's timezone */ |
17 | | - const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone as Timezone; |
18 | | - return (timezones[timezone] || "").toLowerCase() || "us"; |
19 | | -} |
20 | | - |
21 | | -const parsePhoneNumber: ParsePhoneNumber = (value, data, formattedNumber) => { |
22 | | - const isoCode = data?.countryCode; |
23 | | - const countryCodePattern = /\+\d+/; |
24 | | - const areaCodePattern = /\((\d+)\)/; |
25 | | - |
26 | | - /** Parses the matching partials of the phone number by predefined regex patterns */ |
27 | | - const countryCodeMatch = formattedNumber ? (formattedNumber.match(countryCodePattern) || []) : []; |
28 | | - const areaCodeMatch = formattedNumber ? (formattedNumber.match(areaCodePattern) || []) : []; |
29 | | - |
30 | | - /** Converts the parsed values of the country and area codes to integers if values present */ |
31 | | - const countryCode = countryCodeMatch.length > 0 ? parseInt(countryCodeMatch[0]) : null; |
32 | | - const areaCode = areaCodeMatch.length > 1 ? parseInt(areaCodeMatch[1]) : null; |
33 | | - |
34 | | - /** Parses the phone number by removing the country and area codes from the formatted value */ |
35 | | - const phoneNumberPattern = new RegExp(`^${countryCode}${(areaCode || "")}(\\d+)`); |
36 | | - const phoneNumberMatch = value ? (value.match(phoneNumberPattern) || []) : []; |
37 | | - const phoneNumber = phoneNumberMatch.length > 1 ? phoneNumberMatch[1] : null; |
38 | | - |
39 | | - /** Checks if both the area code and phone number length satisfy the validation rules */ |
40 | | - const rules = validations[isoCode as ISO2Code] || {areaCode: [], phoneNumber: []}; |
41 | | - const valid = [ |
42 | | - rules.areaCode.includes((areaCode || "").toString().length), |
43 | | - rules.phoneNumber.includes((phoneNumber || "").toString().length), |
44 | | - ].every(Boolean); |
45 | | - |
46 | | - return {countryCode, areaCode, phoneNumber, isoCode, valid}; |
47 | | -} |
48 | | - |
49 | | -const PhoneInput = ({ |
50 | | - value, |
51 | | - style, |
52 | | - country, |
53 | | - className, |
54 | | - size = "middle", |
55 | | - onPressEnter = () => null, |
56 | | - onMount: handleMount = () => null, |
57 | | - onChange: handleChange = () => null, |
58 | | - ...reactPhoneInputProps |
59 | | - }: PhoneInputProps) => { |
60 | | - const [currentCode, setCurrentCode] = useState(""); |
61 | | - |
62 | | - const countryCode = useMemo(() => country || getDefaultISO2Code(), [country]); |
63 | | - |
64 | | - const rawPhone = useMemo(() => { |
65 | | - const {countryCode, areaCode, phoneNumber} = {...value}; |
66 | | - return [countryCode, areaCode, phoneNumber].map(v => v || "").join(""); |
67 | | - }, [value]); |
| 10 | +const PhoneInput = (inputLegacyProps: PhoneInputProps) => { |
| 11 | + const {token} = theme.useToken(); |
| 12 | + const {status}: any = useContext(FormItemInputContext); |
| 13 | + const [_1, inputCls] = genComponentStyleHook("ant-input"); |
| 14 | + const [_2, dropdownCls] = genComponentStyleHook("ant-dropdown"); |
68 | 15 |
|
69 | 16 | const inputClass = useMemo(() => { |
70 | | - const suffix = {small: "sm", middle: "", large: "lg"}[size]; |
71 | | - return "ant-input" + (suffix ? " ant-input-" + suffix : ""); |
72 | | - }, [size]); |
73 | | - |
74 | | - const onChange: ReactPhoneOnChange = (value, data, event, formattedNumber) => { |
75 | | - const metadata = parsePhoneNumber(value, data, formattedNumber); |
76 | | - const code = metadata.isoCode as ISO2Code; |
77 | | - |
78 | | - if (code !== currentCode) { |
79 | | - /** Clears phone number when the country is selected manually */ |
80 | | - handleChange({...metadata, areaCode: null, phoneNumber: null}, event); |
81 | | - setCurrentCode(code); |
82 | | - return; |
| 17 | + return `${inputCls} ` + getStatusClassNames("ant-input", status); |
| 18 | + }, [inputCls, status]); |
| 19 | + |
| 20 | + const dropdownClass = useMemo(() => "ant-dropdown " + dropdownCls, [dropdownCls]); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + /** Load antd 5.x styles dynamically observing the theme change */ |
| 24 | + for (let styleSheet of document.styleSheets) { |
| 25 | + let rule: any; |
| 26 | + for (rule of styleSheet.cssRules || styleSheet.rules) { |
| 27 | + if (rule.selectorText === ".react-tel-input .country-list") { |
| 28 | + rule.style.boxShadow = token.boxShadow; |
| 29 | + rule.style.backgroundColor = token.colorBgElevated; |
| 30 | + } |
| 31 | + if (rule.selectorText === ".react-tel-input .selected-flag") { |
| 32 | + rule.style.borderColor = token.colorBorder; |
| 33 | + } |
| 34 | + if (rule.selectorText === ".react-tel-input .country-list .search") { |
| 35 | + rule.style.backgroundColor = token.colorBgElevated; |
| 36 | + } |
| 37 | + if (rule.selectorText === ".react-tel-input .country-list .country") { |
| 38 | + rule.style.borderRadius = token.borderRadiusOuter + "px"; |
| 39 | + } |
| 40 | + if (rule.selectorText === ".react-tel-input .country-list .country-name") { |
| 41 | + rule.style.color = token.colorText; |
| 42 | + } |
| 43 | + if (rule.selectorText === ".react-tel-input .country-list .country .dial-code") { |
| 44 | + rule.style.color = token.colorTextDescription; |
| 45 | + } |
| 46 | + if (rule.selectorText === ".react-tel-input .country-list .country:hover") { |
| 47 | + rule.style.backgroundColor = token.colorBgTextHover; |
| 48 | + } |
| 49 | + if (rule.selectorText === ".react-tel-input .country-list .country.highlight") { |
| 50 | + rule.style.backgroundColor = token.colorPrimaryBg; |
| 51 | + } |
| 52 | + if (rule.selectorText === `:where(.${inputCls}).ant-input`) { |
| 53 | + rule.selectorText += "\n,.react-tel-input .country-list .search-box"; |
| 54 | + rule.style.backgroundColor = token.colorBgElevated; |
| 55 | + } |
| 56 | + if (rule.selectorText === `:where(.${inputCls}).ant-input:hover`) { |
| 57 | + rule.selectorText += "\n,.react-tel-input .country-list .search-box:focus"; |
| 58 | + rule.selectorText += "\n,.react-tel-input .country-list .search-box:hover"; |
| 59 | + } |
| 60 | + } |
83 | 61 | } |
84 | | - |
85 | | - handleChange(metadata, event); |
86 | | - } |
87 | | - |
88 | | - const onMount: ReactPhoneOnMount = (rawValue, {countryCode, ...event}, formattedNumber) => { |
89 | | - const metadata = parsePhoneNumber(rawValue, {countryCode}, formattedNumber); |
90 | | - /** Initiates the current country code with the code of initial value */ |
91 | | - setCurrentCode(metadata.isoCode as ISO2Code); |
92 | | - /** Initializes the existing value */ |
93 | | - handleChange(metadata, event); |
94 | | - handleMount(metadata); |
95 | | - } |
| 62 | + }, [inputCls, token]) |
96 | 63 |
|
97 | 64 | return ( |
98 | | - <ReactPhoneInput |
99 | | - /** Static properties for stable functionality */ |
100 | | - masks={masks} |
101 | | - value={rawPhone} |
102 | | - enableAreaCodes |
103 | | - disableSearchIcon |
104 | | - /** Static properties providing dynamic behavior */ |
105 | | - onMount={onMount} |
106 | | - onChange={onChange} |
107 | | - country={countryCode} |
| 65 | + <InputLegacy |
| 66 | + {...inputLegacyProps} |
108 | 67 | inputClass={inputClass} |
109 | | - /** Dynamic properties for customization */ |
110 | | - {...reactPhoneInputProps} |
111 | | - containerStyle={style} |
112 | | - containerClass={className} |
113 | | - onEnterKeyPress={onPressEnter} |
| 68 | + dropdownClass={dropdownClass} |
114 | 69 | /> |
115 | 70 | ) |
116 | 71 | } |
|
0 commit comments