Skip to content

Commit 75846d9

Browse files
Add support for prefixCls provided by ConfigProvider (GH-82)
2 parents 09869f8 + 16e4294 commit 75846d9

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useState
1111
} from "react";
1212
import useFormInstance from "antd/es/form/hooks/useFormInstance";
13+
import {ConfigContext} from "antd/es/config-provider";
1314
import {FormContext} from "antd/es/form/context";
1415
import {useWatch} from "antd/es/form/Form";
1516
import Select from "antd/es/select";
@@ -32,8 +33,6 @@ import {
3233
import {injectMergedStyles} from "./styles";
3334
import {PhoneInputProps, PhoneNumber} from "./types";
3435

35-
injectMergedStyles();
36-
3736
const PhoneInput = forwardRef(({
3837
value: initialValue = "",
3938
country = getDefaultISO2Code(),
@@ -52,13 +51,17 @@ const PhoneInput = forwardRef(({
5251
}: PhoneInputProps, forwardedRef: any) => {
5352
const formInstance = useFormInstance();
5453
const formContext = useContext(FormContext);
54+
const {getPrefixCls} = useContext(ConfigContext);
5555
const inputRef = useRef<any>(null);
5656
const selectedRef = useRef<boolean>(false);
5757
const initiatedRef = useRef<boolean>(false);
5858
const [query, setQuery] = useState<string>("");
5959
const [minWidth, setMinWidth] = useState<number>(0);
6060
const [countryCode, setCountryCode] = useState<string>(country);
6161

62+
const prefixCls = getPrefixCls();
63+
injectMergedStyles(prefixCls);
64+
6265
const {
6366
value,
6467
pattern,
@@ -183,7 +186,7 @@ const PhoneInput = forwardRef(({
183186
dropdownStyle={{minWidth}}
184187
notFoundContent={searchNotFound}
185188
dropdownRender={(menu) => (
186-
<div className="ant-phone-input-search-wrapper">
189+
<div className={`${prefixCls}-phone-input-search-wrapper`}>
187190
{enableSearch && (
188191
<Input
189192
placeholder={searchPlaceholder}
@@ -199,7 +202,7 @@ const PhoneInput = forwardRef(({
199202
value={iso + dial}
200203
key={`${iso}_${mask}`}
201204
label={<div className={`flag ${iso}`}/>}
202-
children={<div className="ant-phone-input-select-item">
205+
children={<div className={`${prefixCls}-phone-input-select-item`}>
203206
<div className={`flag ${iso}`}/>
204207
{name}&nbsp;{displayFormat(mask)}
205208
</div>}
@@ -209,7 +212,7 @@ const PhoneInput = forwardRef(({
209212
), [selectValue, disableDropdown, minWidth, searchNotFound, countriesList, setFieldValue, setValue, enableSearch, searchPlaceholder])
210213

211214
return (
212-
<div className="ant-phone-input-wrapper"
215+
<div className={`${prefixCls}-phone-input-wrapper`}
213216
ref={node => setMinWidth(node?.offsetWidth || 0)}>
214217
<Input
215218
ref={ref}

src/styles.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,18 @@ import commonStyles from "react-phone-hooks/stylesheet.json";
33

44
import customStyles from "./resources/stylesheet.json";
55

6-
export const injectMergedStyles = () => injectStyles(jsonToCss(Object.assign(commonStyles, customStyles)));
6+
let prefix: any = null;
7+
8+
export const injectMergedStyles = (prefixCls: any = null) => {
9+
const stylesheet = customStyles as { [key: string]: any };
10+
if (prefixCls) {
11+
if (prefix === prefixCls) return;
12+
Object.entries(stylesheet).forEach(([k, value]) => {
13+
const key = k.replace(/ant(?=-)/g, prefixCls);
14+
stylesheet[key] = value;
15+
delete stylesheet[k];
16+
})
17+
prefix = prefixCls;
18+
}
19+
return injectStyles(jsonToCss(Object.assign(commonStyles, stylesheet)));
20+
}

tests/antd.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "assert";
22
import Form from "antd/lib/form";
33
import Button from "antd/lib/button";
44
import FormItem from "antd/lib/form/FormItem";
5+
import ConfigProvider from "antd/lib/config-provider";
56
import userEvent from "@testing-library/user-event";
67
import {act, render, screen} from "@testing-library/react";
78

@@ -112,6 +113,15 @@ describe("Checking the basic rendering and functionality", () => {
112113
assert(input.getAttribute("value") === "+1 (702) 123 4567");
113114
})
114115

116+
it("Using `prefixCls` with ConfigProvider", () => {
117+
render(<ConfigProvider prefixCls="custom-prefix">
118+
<PhoneInput data-testid="input"/>
119+
</ConfigProvider>);
120+
const input = screen.getByTestId("input");
121+
assert(!input.outerHTML.includes("ant-input"));
122+
assert(input.outerHTML.includes("custom-prefix-input"));
123+
})
124+
115125
it("Checking field value setters", async () => {
116126
const FormWrapper = () => {
117127
const [form] = Form.useForm();

0 commit comments

Comments
 (0)