-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
How to programmatically focus a TextField? #1594
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
2 options <TextField autoFocus />
function AutoFocusTextField(props) {
const inputRef = React.useRef();
React.useEffect(() => {
const timeout = setTimeout(() => {
inputRef.current.focus();
}, 100);
return () => {
clearTimeout(timeout);
};
}, []);
return <TextField inputRef={inputRef} {...props} />;
} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I finded this answer and i am very happy. Thanks. |
If your TextField is in a Dialog, this solution should work for React 18+ MUI 15+ with Strict Mode enabled
<Dialog disableRestoreFocus {...otherProps}>
...
<TextField autoFocus {...otherProps}/>
</Dialog> |
This breaks html dialogs default behaviour. By disabling expected focus behaviour (restore previously focus object on dialog close) one interferes with default Html behaviour. This is not better than using useEffect + setTimeout hacks. They do not break default behaviour and an argument can be made for better focus management (set focus to important field on dialog open). The same argument cannot be made for disabling the focus-restore (why is it there in the first place? What is the use case for disabling that behaviour?) User clicks button --> context changed to dialog, focus is trapped inside dialog. Focus is set manually to important input field to smooth user experience The useEffect + setTimeout example should be preferred as it replicates accessibility requirements for dialogs: |
StackOverflow: https://stackoverflow.com/questions/37949394/how-to-set-focus-to-a-materialui-textfield/44587705#44587705
Expand
I have this textfield:I want to focus this textfield when I click a button. I have this
but it doesn't work.
I tried using a simple
<input type="text" ref="item" />
and there's no problem with it.Please help.
The text was updated successfully, but these errors were encountered: