Skip to content

Commit 205e72e

Browse files
committed
Wrap components with forward ref
1 parent 77821ed commit 205e72e

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

development/src/ant-phone/index.tsx

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import {ChangeEvent, KeyboardEvent, useCallback, useContext, useEffect, useMemo, useRef, useState} from "react";
1+
import {
2+
ChangeEvent,
3+
forwardRef,
4+
KeyboardEvent,
5+
useCallback,
6+
useContext,
7+
useEffect,
8+
useMemo,
9+
useRef,
10+
useState
11+
} from "react";
212
import useFormInstance from "antd/es/form/hooks/useFormInstance";
313
import {FormContext} from "antd/es/form/context";
414
import Select from "antd/es/select";
@@ -21,22 +31,22 @@ import {PhoneInputProps, PhoneNumber} from "./types";
2131

2232
injectMergedStyles();
2333

24-
const PhoneInput = ({
25-
value: initialValue = "",
26-
country = getDefaultISO2Code(),
27-
enableSearch = false,
28-
disableDropdown = false,
29-
onlyCountries = [],
30-
excludeCountries = [],
31-
preferredCountries = [],
32-
searchNotFound = "No country found",
33-
searchPlaceholder = "Search country",
34-
onMount: handleMount = () => null,
35-
onInput: handleInput = () => null,
36-
onChange: handleChange = () => null,
37-
onKeyDown: handleKeyDown = () => null,
38-
...antInputProps
39-
}: PhoneInputProps) => {
34+
const PhoneInput = forwardRef(({
35+
value: initialValue = "",
36+
country = getDefaultISO2Code(),
37+
enableSearch = false,
38+
disableDropdown = false,
39+
onlyCountries = [],
40+
excludeCountries = [],
41+
preferredCountries = [],
42+
searchNotFound = "No country found",
43+
searchPlaceholder = "Search country",
44+
onMount: handleMount = () => null,
45+
onInput: handleInput = () => null,
46+
onChange: handleChange = () => null,
47+
onKeyDown: handleKeyDown = () => null,
48+
...antInputProps
49+
}: PhoneInputProps, ref: any) => {
4050
const formInstance = useFormInstance();
4151
const formContext = useContext(FormContext);
4252
const backRef = useRef<boolean>(false);
@@ -164,6 +174,7 @@ const PhoneInput = ({
164174
<div className="ant-phone-input-wrapper"
165175
ref={node => setMinWidth(node?.offsetWidth || 0)}>
166176
<Input
177+
ref={ref}
167178
inputMode="tel"
168179
value={value}
169180
onInput={onInput}
@@ -174,6 +185,6 @@ const PhoneInput = ({
174185
/>
175186
</div>
176187
)
177-
}
188+
})
178189

179190
export default PhoneInput;

development/src/mui-phone/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ChangeEvent, KeyboardEvent, useCallback, useEffect, useMemo, useRef, useState} from "react";
1+
import {ChangeEvent, forwardRef, KeyboardEvent, useCallback, useEffect, useMemo, useRef, useState} from "react";
22
import {InputAdornment, MenuItem, Select, TextField} from "@mui/material";
33

44
import {
@@ -18,7 +18,7 @@ import {PhoneInputProps, PhoneNumber} from "./types";
1818

1919
injectMergedStyles();
2020

21-
const PhoneInput = ({
21+
const PhoneInput = forwardRef(({
2222
value: initialValue = "",
2323
variant = undefined,
2424
searchVariant = undefined,
@@ -35,7 +35,7 @@ const PhoneInput = ({
3535
onChange: handleChange = () => null,
3636
onKeyDown: handleKeyDown = () => null,
3737
...muiInputProps
38-
}: PhoneInputProps) => {
38+
}: PhoneInputProps, ref: any) => {
3939
searchVariant = searchVariant || variant;
4040
const backRef = useRef<boolean>(false);
4141
const searchRef = useRef<boolean>(false);
@@ -148,6 +148,7 @@ const PhoneInput = ({
148148
</div>
149149
</Select>
150150
<TextField
151+
ref={ref}
151152
type="tel"
152153
value={value}
153154
variant={variant}
@@ -170,6 +171,6 @@ const PhoneInput = ({
170171
/>
171172
</div>
172173
)
173-
}
174+
})
174175

175176
export default PhoneInput;

development/src/mui-phone/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ export interface PhoneInputProps extends Omit<TextFieldProps, "onChange"> {
3333

3434
onKeyDown?(event: KeyboardEvent<HTMLInputElement>): void;
3535

36-
/** NOTE: This differs from the antd Input onChange interface */
3736
onChange?(value: PhoneNumber, event: ChangeEvent<HTMLInputElement>): void;
3837
}

0 commit comments

Comments
 (0)