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] Fix required outlined label space with no asterisk #20715

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/material-ui/src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ const TextField = React.forwardRef(function TextField(props, ref) {
InputMore.notched = InputLabelProps.shrink;
}
if (label) {
const displayRequired = InputLabelProps?.required ?? required;
Copy link
Member

@oliviertassinari oliviertassinari Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line transforms into:

var _InputLabelProps$requ, _InputLabelProps;

var displayRequired = (_InputLabelProps$requ = (_InputLabelProps = InputLabelProps) === null ||
_InputLabelProps === void 0 ? void 0 : _InputLabelProps.required) !== null
&& _InputLabelProps$requ !== void 0 ? _InputLabelProps$requ : required;

What do you think of?

Suggested change
const displayRequired = InputLabelProps?.required ?? required;
const displayRequired = InputLabelProps && InputLabelProps.required != null ? required : undefined;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick with the correct semantics and improve transpiling with modern bundles.

Copy link
Member

@oliviertassinari oliviertassinari Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about the tradeoff:

  • cost: what's the broader impact if we fully scale-out this syntax? +X% bundle size impact, will X be negligible? I wonder about why the transpiled code tests for strict equality with undefined and null vs != null. Maybe Babel can/will optimize it. I hope it won't, at scale, have the same overhead has the class syntax.
  • value: what cases does the new syntax allow us to better cover?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the broader impact if we fully scale-out this syntax?

Valid point. But we need to be careful that we don't fall into worrying about things we don't need (see "yagni").

I'm looking through all our plugins and see if we can safely enable loose mode in preset-env. For only these two syntaxes babel/babel#6978 would help.

InputMore.label = (
<React.Fragment>
{label}
{required && '\u00a0*'}
{displayRequired && '\u00a0*'}
</React.Fragment>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import TextField from '@material-ui/core/TextField';

export default function OutlinedHiddenRequiredIndicator() {
return (
<TextField
label="Name"
variant="outlined"
required
InputLabelProps={{
shrink: true,
required: false,
}}
/>
);
}