Skip to content

fix(material-experimental/form-field): fix notch width after appearance change - #19682

Merged
mmalerba merged 6 commits into
angular:masterfrom
andrewseguin:form-field-notch
Jun 18, 2020
Merged

fix(material-experimental/form-field): fix notch width after appearance change#19682
mmalerba merged 6 commits into
angular:masterfrom
andrewseguin:form-field-notch

Conversation

@andrewseguin

Copy link
Copy Markdown
Contributor

When the form field initializes, it will determine the floating label notch width if it is outline appearance. This is done once at initialization.

If the form field initializes with an appearance other than outline, it does not determine the correct width.

Screen Shot 2020-06-17 at 8 33 17 PM

This change measures the correct width after the appearance is changed to outline and provides that input to the notched outline.

Screen Shot 2020-06-17 at 8 34 09 PM

@andrewseguin andrewseguin added P2 The issue is important to a large percentage of users, with a workaround merge safe target: minor This PR is targeted for the next minor release labels Jun 18, 2020
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jun 18, 2020
if (this._needsOutlineNotchWidthRefresh) {
this._refreshOutlineNotchWidth();
this._changeDetectorRef.detectChanges();
this._needsOutlineNotchWidthRefresh = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Couldn't we do this either inside the appearance setter or in ngOnChanges?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ngOnChanges probably would have been a better place for this - but removed due to Paul's suggestion

const fixture = createComponent(MatInputWithAppearance);
fixture.detectChanges();

expect(document.querySelector('.mdc-notched-outline__notch')).toBe(null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be better to use fixture.nativeElement.querySelector. Querying it off the document could fail if we have some other test that didn't clean up after itself. The same goes for the other two querySelector calls below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good to know - I wondered why we might not be using document and that makes sense

document.querySelector('input')!.focus();
fixture.detectChanges();

expect(notch.style.width).not.toBe('');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe this should be toBeTruthy? Otherwise this would pass for null or undefined.

if (this._appearance === 'outline' && this._appearance !== oldValue) {
// After the view changes the appearance to outline, the label's width should be measured
// so that it can be provided to the notched outline.
this._needsOutlineNotchWidthRefresh = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This worked before: e3413ba, but we just regressed due those changes. It surprised me initially that those changes had an effect here, but since we had the asterisk rendered through an ngIf template, the CDK content observer triggered and updated the notch width. This is no longer the case as of e3413ba, so the notched outline no longer works.

I think the right fix would be to just explicitly call _refreshOutlineNotchWidth here, and fixing that the label scales to full width unnecessarily (causing incorrect measurement in filled). We stretch it unnecessarily due to our CSS overrides where we set left: 0 and right: 0. We can set those to initial.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That works much better - glad to know that initial is valid so we can measure the right width immediately

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switched directly to using left and right's default value, auto, since initial is blocklisted due to IE

@devversion devversion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great to me!

@devversion devversion added lgtm action: merge The PR is ready for merge by the caretaker labels Jun 18, 2020
@andrewseguin andrewseguin added P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful and removed P2 The issue is important to a large percentage of users, with a workaround labels Jun 18, 2020
@andrewseguin

Copy link
Copy Markdown
Contributor Author

Marking as P1 because this is currently causing our internal Google3 tests to fail

@mmalerba
mmalerba merged commit 5ef23ba into angular:master Jun 18, 2020
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jul 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants