Material-ui: TextField Outline Legend width does not get calculated correctly on label prop change

Created on 31 Jul 2019  路  14Comments  路  Source: mui-org/material-ui


The border of a TextField with variant="outlined" does not update correctly when new label props are being passed into the component on language change. I am using key prop which changes on language change and this makes my input legend to render again but with wrong calculations

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

Expected Behavior 馃


The width of legend should be calculated properly

Current Behavior 馃槸


The legend width is not calculated correctly.
Initial render:
Screen Shot 2019-07-31 at 3 34 09 PM
After language switch:
Screen Shot 2019-07-31 at 3 34 21 PM

Steps to Reproduce 馃暪


Link:

1.
2.
3.
4.

Context 馃敠

Your Environment 馃寧

| Tech | Version |
|--------------|---------|
| Material-UI | v4.3.0 |
| React | 16.8.6 |
| Browser | Chrome |

bug 馃悰 TextField good first issue

Most helpful comment

No, this trick is no longer needed with the latest version.

All 14 comments

@ASHFAQPATWARI There are a couple of related issues:

  • #14530: First render is hidden
  • #15142: Update label to show error

@oliviertassinari This is not a duplicated issue. It seems similar to issues mentionedby you but in my case, adding key prop also does not help as application is becoming LTR/RTL with language switch and legend width calculation is not correct as shown in attached image

@ASHFAQPATWARI Do you have a reproduction?

@ASHFAQPATWARI Thanks, it's a bug with our styling solution. You can work around the problem with disableGlobal: https://codesandbox.io/s/react-hook-form-issue-on-first-click-1zi6b. I think that we should synchronously remove the CSS instead of waiting for the next frame.

@oliviertassinari The bug doesn't appear if i put key prop on both my inputs. https://codesandbox.io/s/react-hook-form-issue-on-first-click-gqcif

@oliviertassinari Putting the key prop results in reference being updated and form validations doesn't work. Have a look at this: https://github.com/bluebill1049/react-hook-form/issues/177

happy to assist and provide information here.

@oliviertassinari - The main issue I see is the effect that sets the label width does not re-execute when the label changes. The label should be added to the dependency array.

diff --git a/packages/material-ui/src/TextField/TextField.js b/packages/material-ui/src/TextField/TextField.js
index 9f595cca402..5731270e405 100644
--- a/packages/material-ui/src/TextField/TextField.js
+++ b/packages/material-ui/src/TextField/TextField.js
@@ -99,7 +99,7 @@ const TextField = React.forwardRef(function TextField(props, ref) {
       const labelNode = ReactDOM.findDOMNode(labelRef.current);
       setLabelWidth(labelNode != null ? labelNode.offsetWidth : 0);
     }
-  }, [variant, required]);
+  }, [variant, required, label]);

   warning(
     !select || Boolean(children),

Here is a codesandbox demonstrating a fixed version alongside the current version as the label changes (by clicking the button).

FYI - I found this issue after seeing a StackOverflow question related to this problem.

@ryancogswell We have left the label out of the dependency array to discourage people from changing it. But I think that you are right. It's another possible solution, +1 for going for it.

@ASHFAQPATWARI Do you want to submit a pull request? :)

@oliviertassinari I will not be able to submit a pull request. I am running with very tight deadlines

No problem, maybe somebody else will :).

@oliviertassinari is using the key prop the library recommended way of handling outline resize when label changes?

No, this trick is no longer needed with the latest version.

Was this page helpful?
0 / 5 - 0 ratings