Material-ui: TextField's outline doesn't resize when `label` prop changes

Created on 1 Apr 2019  路  4Comments  路  Source: mui-org/material-ui

The border of a TextField with variant="outlined" does not update when new label props are being passed into the component.

  • [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 outlined TextField component should resize its borders properly when a new label is passed in

Current Behavior 馃槸

The outlined TextField component doesn't resize its borders when a new label is passed in.
material-textfield-outline

I suspect that the label is somehow calculating the label width of the previous ref, therefore it doesn't change.

I've tried some suggestions such as forcing a new key prop when the label should change, but it defocuses the input and is not very user-friendly.

Furthermore, I've tried setting a background on InputLabelProps; it works, but it doesn't suit my needs.

Steps to Reproduce 馃暪

Link: https://codesandbox.io/s/pp6yk84wwx

  1. Type 'a'
  2. Type 'aa'
  3. Type 'aaa', and the label should change to 'Invalid Tag', but the border doesn't change
  4. Type 'aaaa', now the border changes to the correct width of the label 'Invalid Tag'
  5. Delete two a's, and the label should change to 'Tag', but the border doesn't change
  6. Delete one more 'a', and the border shrinks to the width of 'Tag'

Your Environment 馃寧

| Tech | Version |
|--------------|---------|
| Material-UI | v3.9.2 |
| React | v16.8.4 |
| Browser | Chrome |
| TypeScript | v3.3.3333 |

TextField discussion

Most helpful comment

@oliviertassinari I've tried that, but changing the key defocuses the input, which isn't a good UX behaviour

There is no perfect answer to when we should recalculate the size of outline. A good heuristic is probably when the label changes. I didn't implement this because I wanted to see the use case first. I suspected this would get abused.

I don't think it's a good idea to change the label for an input. Those are important for accessibility. The integration examples with form libraries usually use helper text for error hints. We should be consistent with that too (good usage, bad usage).

A better implementation of your use case would be

<TextField
  error={error}
  label="Tag"
  variant="outlined"
  value={value}
  onChange={onChange}
  helperText={error ? "Invalid Tag" : ""}
/>

All 4 comments

@liangchunn One workaround is to change the key on the text field when the label width change.

@oliviertassinari I've tried that, but changing the key defocuses the input, which isn't a good UX behaviour

@oliviertassinari I've tried that, but changing the key defocuses the input, which isn't a good UX behaviour

There is no perfect answer to when we should recalculate the size of outline. A good heuristic is probably when the label changes. I didn't implement this because I wanted to see the use case first. I suspected this would get abused.

I don't think it's a good idea to change the label for an input. Those are important for accessibility. The integration examples with form libraries usually use helper text for error hints. We should be consistent with that too (good usage, bad usage).

A better implementation of your use case would be

<TextField
  error={error}
  label="Tag"
  variant="outlined"
  value={value}
  onChange={onChange}
  helperText={error ? "Invalid Tag" : ""}
/>

@eps1lon Thanks for the suggestion, using helperText is actually more sensible in this case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sys13 picture sys13  路  3Comments

ghost picture ghost  路  3Comments

anthony-dandrea picture anthony-dandrea  路  3Comments

activatedgeek picture activatedgeek  路  3Comments

rbozan picture rbozan  路  3Comments