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

fix(material/form-field): allow ng-container to be used as text prefix/suffix #26127

Merged
merged 2 commits into from Nov 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/material/form-field/directives/prefix.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, ElementRef, InjectionToken} from '@angular/core';
import {Directive, InjectionToken, Input} from '@angular/core';

/**
* Injection token that can be used to reference instances of `MatPrefix`. It serves as
Expand All @@ -21,9 +21,10 @@ export const MAT_PREFIX = new InjectionToken<MatPrefix>('MatPrefix');
providers: [{provide: MAT_PREFIX, useExisting: MatPrefix}],
})
export class MatPrefix {
_isText = false;

constructor(elementRef: ElementRef) {
this._isText = elementRef.nativeElement.hasAttribute('matTextPrefix');
@Input('matTextPrefix')
set _isTextSelector(value: '') {
this._isText = true;
}

_isText = false;
}
11 changes: 6 additions & 5 deletions src/material/form-field/directives/suffix.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, ElementRef, InjectionToken} from '@angular/core';
import {Directive, InjectionToken, Input} from '@angular/core';

/**
* Injection token that can be used to reference instances of `MatSuffix`. It serves as
Expand All @@ -21,9 +21,10 @@ export const MAT_SUFFIX = new InjectionToken<MatSuffix>('MatSuffix');
providers: [{provide: MAT_SUFFIX, useExisting: MatSuffix}],
})
export class MatSuffix {
_isText = false;

constructor(elementRef: ElementRef) {
this._isText = elementRef.nativeElement.hasAttribute('matTextSuffix');
@Input('matTextSuffix')
set _isTextSelector(value: '') {
this._isText = true;
}

_isText = false;
}
28 changes: 28 additions & 0 deletions src/material/input/input.spec.ts
Expand Up @@ -784,6 +784,21 @@ describe('MatMdcInput without forms', () => {
expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('favorite');
}));

it('should allow ng-container as prefix and suffix', () => {
const fixture = createComponent(InputWithNgContainerPrefixAndSuffix);
fixture.detectChanges();

const textPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-prefix'))!;
const textSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-suffix'))!;
const iconPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-prefix'))!;
const iconSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-suffix'))!;

expect(textPrefixEl.nativeElement.innerText.trim()).toEqual('text-prefix');
expect(textSuffixEl.nativeElement.innerText.trim()).toEqual('text-suffix');
expect(iconPrefixEl.nativeElement.innerText.trim()).toEqual('icon-prefix');
expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('icon-suffix');
});

it('should update empty class when value changes programmatically and OnPush', fakeAsync(() => {
let fixture = createComponent(MatInputOnPush);
fixture.detectChanges();
Expand Down Expand Up @@ -2064,3 +2079,16 @@ class MatInputWithRequiredFormControl {
`,
})
class MatInputSimple {}

@Component({
template: `
<mat-form-field>
<ng-container matIconPrefix>icon-prefix</ng-container>
<ng-container matTextPrefix>text-prefix</ng-container>
<input matInput>
<ng-container matTextSuffix>text-suffix</ng-container>
<ng-container matIconSuffix>icon-suffix</ng-container>
</mat-form-field>
`,
})
class InputWithNgContainerPrefixAndSuffix {}
10 changes: 6 additions & 4 deletions tools/public_api_guard/material/form-field.md
Expand Up @@ -223,22 +223,24 @@ export class MatLabel {

// @public
export class MatPrefix {
constructor(elementRef: ElementRef);
// (undocumented)
_isText: boolean;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MatPrefix, "[matPrefix], [matIconPrefix], [matTextPrefix]", never, {}, {}, never, never, false, never>;
set _isTextSelector(value: '');
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MatPrefix, "[matPrefix], [matIconPrefix], [matTextPrefix]", never, { "_isTextSelector": "matTextPrefix"; }, {}, never, never, false, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatPrefix, never>;
}

// @public
export class MatSuffix {
constructor(elementRef: ElementRef);
// (undocumented)
_isText: boolean;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MatSuffix, "[matSuffix], [matIconSuffix], [matTextSuffix]", never, {}, {}, never, never, false, never>;
set _isTextSelector(value: '');
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MatSuffix, "[matSuffix], [matIconSuffix], [matTextSuffix]", never, { "_isTextSelector": "matTextSuffix"; }, {}, never, never, false, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatSuffix, never>;
}
Expand Down