|
1 | | -import {useContext, useEffect, useMemo} from "react"; |
2 | | -import theme from "antd/lib/theme"; |
| 1 | +import {useContext, useMemo} from "react"; |
| 2 | +import useToken from "antd/lib/theme/useToken"; |
3 | 3 | import genComponentStyleHook from "antd/lib/input/style"; |
| 4 | +import {ConfigContext} from "antd/lib/config-provider"; |
| 5 | +import {useStyleRegister} from "antd/lib/theme/internal"; |
4 | 6 | import {FormItemInputContext} from "antd/lib/form/context"; |
5 | 7 | import {getStatusClassNames} from "antd/lib/_util/statusUtils"; |
6 | 8 |
|
7 | 9 | import InputLegacy from "./legacy"; |
| 10 | +import genPhoneInputStyle from "./style"; |
8 | 11 | import {PhoneInputProps} from "./types"; |
9 | 12 |
|
10 | 13 | const PhoneInput = (inputLegacyProps: PhoneInputProps) => { |
11 | | - const {token} = theme.useToken(); |
| 14 | + const {getPrefixCls} = useContext(ConfigContext); |
12 | 15 | const {status}: any = useContext(FormItemInputContext); |
13 | | - const [_1, inputCls] = genComponentStyleHook("ant-input"); |
14 | | - const [_2, dropdownCls] = genComponentStyleHook("ant-dropdown"); |
| 16 | + const inputPrefixCls = getPrefixCls("input"); |
| 17 | + const dropdownPrefixCls = getPrefixCls("dropdown"); |
| 18 | + const [_1, inputCls] = genComponentStyleHook(inputPrefixCls); |
| 19 | + const [_2, dropdownCls] = genComponentStyleHook(dropdownPrefixCls); |
| 20 | + const [theme, token, _3] = useToken(); |
15 | 21 |
|
16 | 22 | const inputClass = useMemo(() => { |
17 | | - return `${inputCls} ` + getStatusClassNames("ant-input", status); |
18 | | - }, [inputCls, status]); |
| 23 | + return `${inputCls} ` + getStatusClassNames(inputPrefixCls, status); |
| 24 | + }, [inputPrefixCls, inputCls, status]); |
19 | 25 |
|
20 | 26 | const dropdownClass = useMemo(() => "ant-dropdown " + dropdownCls, [dropdownCls]); |
21 | 27 |
|
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 | | - } |
61 | | - } |
62 | | - }, [inputCls, token]) |
| 28 | + useStyleRegister({ |
| 29 | + theme, |
| 30 | + token, |
| 31 | + hashId: "react-tel-input", |
| 32 | + path: ["antd-phone-input"], |
| 33 | + }, () => [genPhoneInputStyle(token)]); |
63 | 34 |
|
64 | 35 | return ( |
65 | 36 | <InputLegacy |
|
0 commit comments