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

TextField's outline doesn't resize when label prop changes #15142

Closed
2 tasks done
liangchunn opened this issue Apr 1, 2019 · 6 comments
Closed
2 tasks done

TextField's outline doesn't resize when label prop changes #15142

liangchunn opened this issue Apr 1, 2019 · 6 comments
Labels
component: text field This is the name of the generic UI component, not the React module! discussion

Comments

@liangchunn
Copy link
Contributor

liangchunn commented Apr 1, 2019

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

  • 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 🤔

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
@oliviertassinari
Copy link
Member

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

@liangchunn
Copy link
Contributor Author

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

@oliviertassinari oliviertassinari added the component: text field This is the name of the generic UI component, not the React module! label Apr 1, 2019
@eps1lon
Copy link
Member

eps1lon commented Apr 2, 2019

@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" : ""}
/>

@liangchunn
Copy link
Contributor Author

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

@pschichtel
Copy link

I don't think it's a good idea to change the label for an input. Those are important for accessibility.

we have this issue when we switch the language in our UI (in-application override option), which messes up most of the form field labels.

@oliviertassinari
Copy link
Member

@pschichtel it should be fixed in v5 already.

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! discussion
Projects
None yet
Development

No branches or pull requests

4 participants