[TextField] errorStyle no longer applies on floating text
Problem description
Upgrading from 15.4 to 16.x, I noticed that errorStyle in the TextField no longer applies to the floating label.
Before

After

Notice the floating text isn't styled with the same color as the error text.
This is the commit that seems to have caused this issue: https://github.com/callemall/material-ui/commit/a3ddda2539eb7e99767044983e3679e34594c8ee https://github.com/callemall/material-ui/pull/4756
Example code that causes the issue
The color deepOrange200 is not being applied to the floating label anymore.
<TextField
errorStyle={{ color: deepOrange200 }}
errorText={this.state.errors.password}
floatingLabelText="Password"
{ /* other props */ }
/>
Versions
- Material-UI: 16.6
- React: 15.4
- Browser: All
You can use the floatingLabelStyle property to solve your issue.
I noticed that errorStyle in the TextField no longer applies to the floating label.
Looking at #4756, I think that it's the right behavior. Actually, I think that the errorStyle changing the style of the underline could be considered a bug.
Previously, when we set the errorText, there was a consistency in style between the floating label text and the error text. It followed the style in errorStyle.
In my image, in the first one, both the floating text and the error message have the same color. This is in the case there is an error, ie we set the errorText prop to something. Otherwise they both are white.
Now, however, I'm not sure how to style the floating label text in the case where there is an error vs no error.. Setting the floatingLabelStyle fixes the style, whether or not there is a error text or not.
I ran into this today and was all fired up to submit a PR. I definitely want the floating label color to be co-ordinated with the underline and error. After all, this happens automatically with focus, and surely it's what most library end-users would expect.
But I'm not sure how best to fit in with the current API.
Option 1: override the floatingLabel color with any color specified in the errorStyle prop. Should this get higher priority than any floatingLableStyle prop? After all, there is no specific floatingLable style prop for the error state. But that might feel like ambiguous behaviour.
option 2: update the API so that we have an explicit floatingLabelErrorStyle to go with floatingLabelStyle and floatingLabelFocusStyle and floatingLabelShrinkStyle. It would be clear and explicit, but also feels icky. Just how many props do we have to pass to properly configure this component?
option 3: add a specific errorColor prop which overrides the error.color style with highest priority on all three elements. This feels to me like the cleanest and simplest way for what seems like the most common use case. But is maybe a slightly more radical change.
I'm happy to submit a PR (though I may have a problem coming up with a test). But which option to go for?
+1
...to which option?
I would go with option 2 as it sounds like the simplest one. The style properties are here so that users have an escape hatch to customize components. The simplest they are, the better.
Why is floatingLabelStyle not enough?
Why is floatingLabelStyle not enough?
Because I think the current behavior of TextField implies a promise. When I pass it an errorText, it restyles all three elements (floatingLabel, underline and errorText) to a predefined error color. So I'd expect to be able to change that color via a prop and have all three elements behave consistently.
And in fact, that used to be true, but now it's not, as described by OP. So I'm not sure why you think it's not a regression.
It is of course possible to use floatingLabelStyle, but that certainly doesn't feel like the simplest option. It implies extra conditional logic that has to complicate my code when that conditional is already being checked in the TextField code. e.g. here:
if (state.errorText) {
if (state.isFocused) {
styles.floatingLabel.color = styles.error.color;
}
}
So I'd expect to be able to change that color via a prop and have all three elements behave consistently
You can do it through the theme but a property sounds like a good feature. Thanks for providing more detail, that's enlightening 💡 !
So I'm not sure why you think it's not a regression.
From my perspective, the errorStyle property affecting multiple elements was a design flaw. It should have never worked.
At least, I would have called it errorStyles. Actually, that property is another option to fix the issue 👍 .
We have removed the if (state.isFocused) { logic in the next branch as the material specification let us think that it's the correct behavior.
I agree that if I only want to change the default error colour, I can change the theme.
However my current use-case is that our app distinguishes between front-end validations - which it shows as warnings in an orange colour - and back-end validation failures which it displays in the default red colour. I expect OP was doing something similar?
I have a ValidatedTextField component which wraps the material-ui TextField and handles these validations, passing errorText and errorStyles to the TextField depending on incoming props and validation checks. When doing something like this it would be nice to have the TextField behave consistently with regard to styling its elements when in an error state.
How about an errorColor prop? I can't think of any CSS attribute other than colour that would be universally applied to floatingLabel, underline and errorText.
edit: So far I've only been looking at the TextField code in master. I'll take a look at the next branch sometime soon, perhaps I can contribute there.
@oliviertassinari On master, I see that the error color of text fields is hard-coded to red500 and not taken from the palette. Is there a way to set the error color on the theme?
How about an errorColor prop?
@marksweston Thanks for providing more detail on your use case. I would say it's too specific, what if somebody wants to change the opacity, font size, line height?
edit: So far I've only been looking at the TextField code in master. I'll take a look at the next branch sometime soon, perhaps I can contribute there.
The next branch has made quite some progress. I'm not longer using the master branch. Any contribution is welcomed.
On master, I see that the error color of text fields is hard-coded
@etrejos You are right, it's hard coded. Have a look at the Customizing the theme section of the documentation, you can overrides everything.
@oliviertassinari Thanks a lot. This solved our issue:
export default getMuiTheme({
textField: {
errorColor: deepOrange200,
}
});
So is this closed?
No, It is not. following my comment: https://github.com/callemall/material-ui/issues/5869#issuecomment-280914624, we can improve things.
@marksweston Thanks for providing more detail on your use case. I would say it's too specific, what if somebody wants to change the opacity, font size, line height?
@oliviertassinari The point is, I think, that here we are talking about a behaviour that changes the colour of three different elements simultaneously. This issue is specifically about colour. And I'm struggling to think of any other CSS property that one might want to apply the same value to the error text, the underline and the floating text. None of your suggestions seem likely, except perhaps opacity, which could be done with an rgba colour anyway.
@marksweston Sounds fair. So what about the following plan:
- We exposing an
errorColorproperty affecting the three elements. - If people need more customizability power, we expose an additional
errorStylesproperty.
Makes sense to me.
We have been porting the component on the next branch. We reimplemented it from the ground-up. While we haven't tested it, I think that the issue is most likely fixed on that branch. Hence, I'm closing it.