[TextField] autoFocus doesn't seem to be working

See original GitHub issue

Problem description

I tried making auto focus on my TextField with no success.

I also looked and saw that the autoFocus is being @ignore from the docs. Why is that?

Tried with autoFocus={true}

<TextField
        autoFocus={true}
        onChange={ (e) => debounceOnChange(e.target.value) }
        type="search"
      />

Tried with inputProps={true}

<TextField
        inputProps={autofocus: {true}}
        onChange={ (e) => debounceOnChange(e.target.value) }
        type="search"
      />

And even InputProps:

<TextField
        InputProps={autofocus: {true}}
        onChange={ (e) => debounceOnChange(e.target.value) }
        type="search"
      />

Versions

  • Material-UI: 1.0.0-alpha-19
  • React: 15.6.1
  • Browser: Al of them

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

23reactions
stunazcommented, Jun 25, 2017

Well it seems to work well with inputProps={{ autoFocus: true }} see https://codesandbox.io/s/ERxnnX9mm

13reactions
cyrfercommented, Jan 19, 2020

In case someone is struggling with this.

I updated the implementation in the codesandbox link above to use the latest version, and it works in codesandbox. https://codesandbox.io/s/recursing-framework-deoxv?fontsize=14&hidenavigation=1&theme=dark

However I’m struggling with this in my application. I verified onChange is not invoked immediately as well as onBlur so I don’t think it is an event issue.

      <TextField
        {/* have also tried autoFocus={talent.autofocus} */}
        inputProps={{ autoFocus: talent.autofocus }}
        label="Talent"
        type="text"
        value={talent.name || ''}
        onChange={() => {console.log('onchange')}}
        onBlur={() => {console.log('onblur')}}
        margin="normal"
      />

However I was able to get it to work using a ref

const MyComponent = ({talent}) => {
  const inputRef = useRef()

  useEffect(() => {
    if (talent.autofocus) {
      inputRef.current.focus()
    }
  }, [talent.autofocus, inputRef])

  return (
      <TextField
        inputRef={inputRef}
        label="Talent"
        type="text"
        value={talent.name || ''}
        onChange={() => {console.log('onchange')}}
        margin="normal"
      />
  )
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

[TextField] autoFocus doesn't seem to be working · Issue #7247
I have noticed that the autoFocus behaviour is unreliable, I believe it's an issue with React internally. I have noticed the same thing...
Read more >
Flutter throws an error when i use Text field with auto focus
Because with auto focus, the keyboard show up first but textfield is still not exist ... Rebuild your project, and it should work....
Read more >
Mui TextField doesn't autofocus : r/reactjs - Reddit
If you are manually setting focus to input it has nothing to do with autofocus attribute. Autofocus is used to tell that input...
Read more >
SwiftUI default focus on TextField… | Apple Developer Forums
This doesn't work for me. I hope they fix this bug — one of the most important use cases of focus is to...
Read more >
autofocus not working in UI for ASP.NET MVC | Telerik Forums
Hi, I'm trying to set the autofocus on my edit screens. If it's an Kendo-control that I want to have autofocus, it doesn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found