Skip to content

fix(material-experimental/form-field): add setLabelRequired adapter #19284

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

Merged
merged 1 commit into from
May 8, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@types/youtube": "^0.0.38",
"@webcomponents/custom-elements": "^1.1.0",
"core-js": "^2.6.9",
"material-components-web": "7.0.0-canary.af71cfdd6.0",
"material-components-web": "7.0.0-canary.047e6b337.0",
"rxjs": "^6.5.3",
"systemjs": "0.19.43",
"tslib": "^1.10.0",
Expand Down
5 changes: 5 additions & 0 deletions src/material-experimental/mdc-form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
// have the ability to control the floating label state (i.e. `shouldLabelFloat`), and we
// want to update the notch whenever the `_shouldLabelFloat()` value changes.
getLabelWidth: () => 0,

// TODO: MDC now supports setting the required asterisk marker directly on
// the label component. This adapter method may be implemented and
// mat-mdc-form-field-required-marker removed.
Copy link
Member

Choose a reason for hiding this comment

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

Is the plan that we clean this up as a follow-up? I don't think we'll use that given that we support custom form field controls and/or their associated ng control.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's optional to clean up.

I didn't do a deep dive into the component, but it looks like the <span class="mat-mdc-form-field-required-marker"> accomplishes the same goal.

Instead of doing it in markup, you could do it in this method:

setLabelRequired(isRequired: boolean) {
  if (!this.hideRequiredMarker && this._control.required && !this.control.disabled) {
    // toggles the `mdc-floating-label--required` class
    this._floatingLabel.required = isRequired;
  }
}

Definitely a lot of opinions and unknowns on my part. Feel free to update the comment and either implement something, or leave it as no-op.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. I don't think we'll be able to rely on that method since we want to update whenever the input is refreshed, so we rely on change detection. Doing that manually might be overly complicated

setLabelRequired: () => {},
notchOutline: () => {},
closeOutline: () => {},

Expand Down
Loading