Skip to content

fix(material/input): preserve native placeholder on non-legacy appearances #20936

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
Mar 18, 2022
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
14 changes: 13 additions & 1 deletion src/material-experimental/mdc-input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,17 @@ describe('MatMdcInput without forms', () => {
expect(formField.classList).not.toContain('mat-mdc-form-field-type-mat-native-select');
});

it('should preserve the native placeholder on a non-legacy appearance', fakeAsync(() => {
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
fixture.componentInstance.floatLabel = 'auto';
fixture.componentInstance.appearance = 'outline';
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('input').getAttribute('placeholder')).toBe(
'Placeholder',
);
}));

it(
'should use the native input value when determining whether ' +
'the element is empty with a custom accessor',
Expand Down Expand Up @@ -1861,14 +1872,15 @@ class MatInputWithLabel {}

@Component({
template: `
<mat-form-field [floatLabel]="floatLabel">
<mat-form-field [floatLabel]="floatLabel" [appearance]="appearance">
<mat-label>Label</mat-label>
<input matInput placeholder="Placeholder">
</mat-form-field>
`,
})
class MatInputWithLabelAndPlaceholder {
floatLabel: FloatLabelType;
appearance: MatFormFieldAppearance;
}

@Component({
Expand Down
10 changes: 10 additions & 0 deletions src/material/datepicker/date-range-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ $date-range-input-part-max-width: calc(50% - #{$date-range-input-separator-spaci
.mat-date-range-input-separator {
@include _placeholder-transition(opacity);
margin: 0 $date-range-input-separator-spacing;

._mat-animation-noopable & {
transition: none;
}
}

.mat-date-range-input-separator-hidden {
Expand Down Expand Up @@ -85,6 +89,12 @@ $date-range-input-part-max-width: calc(50% - #{$date-range-input-separator-spaci
}
}
}

._mat-animation-noopable & {
@include vendor-prefixes.input-placeholder {
transition: none;
}
}
}

// We want the start input to be flush against the separator, no matter how much text it has, but
Expand Down
6 changes: 6 additions & 0 deletions src/material/form-field/form-field-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
}
}
}

._mat-animation-noopable & {
@include vendor-prefixes.input-placeholder {
transition: none;
}
}
}

// Prevents IE from always adding a scrollbar by default.
Expand Down
15 changes: 13 additions & 2 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,6 @@ describe('MatInput without forms', () => {
expect(container.classList).toContain('mat-form-field-hide-placeholder');
expect(container.classList).not.toContain('mat-form-field-should-float');
expect(label.textContent.trim()).toBe('Label');
expect(input.hasAttribute('placeholder')).toBe(false);

input.value = 'Value';
fixture.detectChanges();
Expand Down Expand Up @@ -1020,6 +1019,17 @@ describe('MatInput without forms', () => {
expect(container.classList).not.toContain('mat-form-field-should-float');
});

it('should preserve the native placeholder on a non-legacy appearance', fakeAsync(() => {
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
fixture.componentInstance.floatLabel = 'auto';
fixture.componentInstance.appearance = 'standard';
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('input').getAttribute('placeholder')).toBe(
'Placeholder',
);
}));

it('should not add the native select class if the control is not a native select', () => {
const fixture = createComponent(MatInputWithId);
fixture.detectChanges();
Expand Down Expand Up @@ -2232,14 +2242,15 @@ class MatInputWithLabel {}

@Component({
template: `
<mat-form-field [floatLabel]="floatLabel">
<mat-form-field [floatLabel]="floatLabel" [appearance]="appearance">
<mat-label>Label</mat-label>
<input matInput placeholder="Placeholder">
</mat-form-field>
`,
})
class MatInputWithLabelAndPlaceholder {
floatLabel: FloatLabelType;
appearance: MatFormFieldAppearance = 'legacy';
}

@Component({
Expand Down
6 changes: 5 additions & 1 deletion src/material/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,11 @@ export class MatInput
// screen readers will read it out twice: once from the label and once from the attribute.
// TODO: can be removed once we get rid of the `legacy` style for the form field, because it's
// the only one that supports promoting the placeholder to a label.
const placeholder = this._formField?._hideControlPlaceholder?.() ? null : this.placeholder;
const formField = this._formField;
const placeholder =
formField && formField.appearance === 'legacy' && !formField._hasLabel?.()
? null
: this.placeholder;
if (placeholder !== this._previousPlaceholder) {
const element = this._elementRef.nativeElement;
this._previousPlaceholder = placeholder;
Expand Down