Skip to content

Commit 623fca0

Browse files
committed
fix(ref): fix ref issues and make it optional
1 parent 64dab75 commit 623fca0

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

src/TextInput.tsx

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,64 @@ type Props = {
1818
}
1919
) => React.ReactElement;
2020
chainTo?: string;
21-
ref: React.RefObject<RNTextInput>;
21+
ref?: React.RefObject<RNTextInput>;
2222
};
2323

24-
export const TextInput = (props: Props) => {
25-
const id = React.useId();
26-
const name = React.useRef(props.name ?? id);
27-
const textInputRef = React.useRef<RNTextInput>(null);
24+
export const TextInput = React.forwardRef<RNTextInput, Props>(
25+
(props, inputRef) => {
26+
const id = React.useId();
27+
const name = React.useRef(props.name ?? id);
28+
const textInputRef = React.useRef<RNTextInput>(null);
2829

29-
const ref = mergeRefs([textInputRef, props.ref]);
30+
const ref = mergeRefs([textInputRef, inputRef]);
3031

31-
const { onBlur, onFocus, onSubmitEditing, ...textInputProps } =
32-
props.textInputProps ?? {};
32+
const { onBlur, onFocus, onSubmitEditing, ...textInputProps } =
33+
props.textInputProps ?? {};
3334

34-
const { registerInput, baseTextInputProps, chainInput } =
35-
useFormSmartScroll();
35+
const { registerInput, baseTextInputProps, chainInput } =
36+
useFormSmartScroll();
3637

37-
React.useEffect(() => {
38-
registerInput(name.current, textInputRef);
39-
}, []);
38+
React.useEffect(() => {
39+
registerInput(name.current, textInputRef);
40+
}, []);
4041

41-
return (
42-
<ViewWrapper name={name.current} style={props.containerStyle}>
43-
{typeof props.renderInput === 'function' ? (
44-
props.renderInput({
45-
...(textInputProps ?? {}),
46-
onSubmitEditing: (e) => {
47-
if (props.chainTo) {
48-
chainInput(props.chainTo);
49-
}
50-
51-
onSubmitEditing?.(e);
52-
},
53-
...baseTextInputProps(name.current, {
54-
onFocus,
55-
onBlur,
56-
}),
57-
ref,
58-
})
59-
) : (
60-
<>
61-
{props.renderTop?.()}
62-
<RNTextInput
63-
ref={ref}
64-
{...baseTextInputProps(name.current, { onFocus, onBlur })}
65-
{...textInputProps}
66-
onSubmitEditing={(e) => {
42+
return (
43+
<ViewWrapper name={name.current} style={props.containerStyle}>
44+
{typeof props.renderInput === 'function' ? (
45+
props.renderInput({
46+
...(textInputProps ?? {}),
47+
onSubmitEditing: (e) => {
6748
if (props.chainTo) {
6849
chainInput(props.chainTo);
6950
}
7051

7152
onSubmitEditing?.(e);
72-
}}
73-
/>
74-
{props.renderBottom?.()}
75-
</>
76-
)}
77-
</ViewWrapper>
78-
);
79-
};
53+
},
54+
...baseTextInputProps(name.current, {
55+
onFocus,
56+
onBlur,
57+
}),
58+
ref,
59+
})
60+
) : (
61+
<>
62+
{props.renderTop?.()}
63+
<RNTextInput
64+
ref={ref}
65+
{...baseTextInputProps(name.current, { onFocus, onBlur })}
66+
{...textInputProps}
67+
onSubmitEditing={(e) => {
68+
if (props.chainTo) {
69+
chainInput(props.chainTo);
70+
}
71+
72+
onSubmitEditing?.(e);
73+
}}
74+
/>
75+
{props.renderBottom?.()}
76+
</>
77+
)}
78+
</ViewWrapper>
79+
);
80+
}
81+
);

0 commit comments

Comments
 (0)