question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextField autoFocus prop does not shrink label

See original GitHub issue

I’m developing a server rendered app with nextjs. When I set autoFocus={true} on the TextField component and refresh (page coming from server), TextField does not recognize the input focus and nothing changes (no shrink={true}, no css). This works correctly when navigating through the client.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

`shrink` prop should be correctly set to `true` when input is focused via `autofocus={true}`, css should change to reflect focus state

Current Behavior 😯

`shrink` prop seem to not be affected, css does not change

Steps to Reproduce 🕹

Link:

https://codesandbox.io/s/8nov84y178

Please wait a few seconds for styles to load. Loading seems to be slow due to a specific issue with codesandbox, but it does highlight the issue more clearly.

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v3.7.1
React v16.6.3
Browser Chrome
TypeScript v3.2.2
etc.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
zerefwaynecommented, Nov 30, 2019

@oliviertassinari I’m want to contribute to material-ui, can I take up this issue?

1reaction
oliviertassinaricommented, Jan 11, 2019

@doronnac Thank you. I have a look at the issue. It’s definitely not an easy one to handle. Here are some important point to take into account:

I’m proposing the following change:

--- a/packages/material-ui/src/InputBase/InputBase.js
+++ b/packages/material-ui/src/InputBase/InputBase.js
@@ -139,6 +139,9 @@ export const styles = theme => {
   };
 };

+// This variable will be true once the server-side hydration is completed.
+let hydrationCompleted = false;
+
 /**
  * `InputBase` contains as few styles as possible.
  * It aims to be a simple building block for creating an input.
@@ -170,6 +173,14 @@ class InputBase extends React.Component {
     if (!this.isControlled) {
       this.checkDirty(this.inputRef);
     }
+
+    // Workaround https://github.com/facebook/react/issues/11159
+    if (!hydrationCompleted && this.inputRef === document.activeElement && !this.state.focused) {
+      this.handleFocus(null);
+    }
+
+    hydrationCompleted = true;
   }

   componentDidUpdate(prevProps) {
@@ -186,6 +197,7 @@ class InputBase extends React.Component {
   }

@joshwooding What do you think about it? @doronnac Do you want to work on it? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

TextField autoFocus prop does not shrink label #14132 - GitHub
The problem is with autoFocus on hydration which is marked as fixed in react. There should be no workaround required. It seems like...
Read more >
Cannot change font size of text field in material ui
The most straight forward way to change the font size of both the input label and the input text is to use inline...
Read more >
TextField API - Material UI - MUI
Name Type Default autoComplete string autoFocus bool false classes object
Read more >
Mui TextField doesn't autofocus : r/reactjs - Reddit
Autofocus is used to tell that input should have focus as default when page loads for first time, after that it will do...
Read more >
MudTextField - MudBlazor - Blazor Component Library
Name Type Default Mask IMask null Text string null Value T null
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