Skip to content
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

Closed
mewben opened this issue Sep 5, 2015 · 17 comments
Closed

How to programmatically focus a TextField? #1594

mewben opened this issue Sep 5, 2015 · 17 comments
Labels
component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@mewben
Copy link

mewben commented Sep 5, 2015

StackOverflow: https://stackoverflow.com/questions/37949394/how-to-set-focus-to-a-materialui-textfield/44587705#44587705

Expand I have this textfield:

<TextField ref="item" />

I want to focus this textfield when I click a button. I have this

React.findDOMNode(this.refs.item).focus();

but it doesn't work.

I tried using a simple <input type="text" ref="item" /> and there's no problem with it.

Please help.

@oliviertassinari

This comment has been minimized.

@mewben

This comment has been minimized.

@mewben mewben closed this as completed Sep 6, 2015
@rkstar

This comment has been minimized.

@ydeshayes

This comment has been minimized.

@dogrocker

This comment has been minimized.

@bchenSyd

This comment has been minimized.

@bchenSyd

This comment has been minimized.

@ffxsam

This comment has been minimized.

@ffxsam
Copy link
Contributor

ffxsam commented Jan 13, 2017

2 options

<TextField autoFocus />
  1. If you have to put a slight time delay in to get the focus to happen.
function AutoFocusTextField(props) {
  const inputRef = React.useRef();

  React.useEffect(() => {
    const timeout = setTimeout(() => {
      inputRef.current.focus();
    }, 100);

    return () => {
      clearTimeout(timeout);
    };
  }, []);

  return <TextField inputRef={inputRef} {...props} />;
}

@lrettig

This comment has been minimized.

@saadtazi

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@saadtazi

This comment has been minimized.

@BloodhoundAllfather

This comment has been minimized.

@oliviertassinari oliviertassinari added support: question Community support but can be turned into an improvement component: text field This is the name of the generic UI component, not the React module! labels Oct 16, 2018
@LuizLeite
Copy link

I finded this answer and i am very happy. Thanks.

@drecali
Copy link

drecali commented Apr 24, 2024

If your TextField is in a Dialog, this solution should work for React 18+ MUI 15+ with Strict Mode enabled

  1. The Dialog should have the disableRestoreFocus prop.
  2. The text field in the Dialog should have the autoFocus prop.
<Dialog disableRestoreFocus {...otherProps}>
  ...
  <TextField autoFocus {...otherProps}/>
</Dialog>

@bjoernuhlig
Copy link

bjoernuhlig commented Nov 18, 2024

If your TextField is in a Dialog, this solution should work for React 18+ MUI 15+ with Strict Mode enabled

  1. The Dialog should have the disableRestoreFocus prop.
  2. The text field in the Dialog should have the autoFocus prop.
<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
User closes dialog --> continues navigating where he/she set off ( the button. the previously focussed element.)

The useEffect + setTimeout example should be preferred as it replicates accessibility requirements for dialogs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests