-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
input ref? #366
Comments
Import { useRef } from 'React'; const phoneNoRef = useRef(); return ( |
I'm not sure if you can do what @goelshobhit suggests but I might be wrong. I have checked Here's what I'm doing (using v4.3.4 of TLDR; const InputPhone = ({ autoFocus, field, onKeyDown, onPhoneNumberChange, onSelectFlag }: InputPhoneProps) => {
// NOTE: all this getting of input el to add a blur event listener to it is to
// remove the error / success message on blur of input (after user re-selects input after submit).
// Submit (enter key down on input or press of button) will auto blur input without removing messages.
// If this is not required, remove this code:
const containerRef: MutableRefObject<null | HTMLDivElement> = useRef(null);
const inputRef: MutableRefObject<null | HTMLInputElement> = useRef(null);
useEffect(() => {
const inputEl = containerRef.current?.getElementsByTagName('input')?.[0];
if (!inputEl) {
throw new Error(`Could not find the phone input element with name ${fieldName}`);
}
inputRef.current = inputEl;
inputRef.current.addEventListener('blur', field.onBlur);
return () => {
if (inputRef.current && field.onBlur) {
inputRef.current.removeEventListener('blur', field.onBlur);
}
};
}, []);
return (
<div ref={containerRef}>
<CPInputPhone
autoFocus={autoFocus}
fieldName={field.name ?? fieldName}
onPhoneNumberChange={onPhoneNumberChange}
onSelectFlag={onSelectFlag}
// Pass onKeyDown to telInputProps because these get passed to <input> element
// rendered by react-intl-tel-input.
// onPhoneNumberChange does not get an event to work with. <input>'s onKeyDown does
// so you can detect enter press:
// NOTE: since react-intl-tel-input overwrites onChange and onBlur, passing them to
// telInputProps is useless. onKeyDown is not overwritten.
// https://github.com/patw0929/react-intl-tel-input/blob/v4.3.4/src/components/TelInput.js#L7-L22
telInputProps={{ onKeyDown }}
/>
</div>
);
};
Edit:Also note that if you're planning on adding some event listeners (like above) and you're working with const [error, setError] = useState(undefined)
field.onBlur = (e) => {
// error will always be `undefined` here.
if (error !== undefined) {
setError(undefined);
}
}
// Fix:
const [error, setError] = useState(undefined)
const errorRef = useRef(error);
const setErrorRef = (err) => {
// keep new value of error after rerender:
errorRef.current = err;
// trigger rerender if necessary:
setError(err);
};
field.onBlur = (e) => {
if (errorRef.current !== undefined) {
setErrorRef(undefined);
}
} |
@stoplion @goelshobhit @justin-calleja yes, there is one. import React, { useEffect, useRef, useState } from 'react';
import IntlTelInput from 'react-intl-tel-input';
function App() {
const intlTelInputRef = useRef<IntlTelInput | null>(null);
useEffect(() => {
intlTelInputRef.current?.tel?.focus();
}, []);
return (
<div className="App">
<IntlTelInput ref={intlTelInputRef} />
</div>
);
} The |
I'm using this package with react-hook-form Controller and I'm using the ref props like this
but when I submit the form with empty value it doesn't focus on the input. any ideas? |
Is there a way to get a ref of the input?
The text was updated successfully, but these errors were encountered: