Skip to content

refactor(form-field): explicitly set static flag on all queries #15720

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
Apr 18, 2019
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
33 changes: 25 additions & 8 deletions src/lib/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,31 @@ export class MatFormField extends _MatFormFieldMixinBase
* @deprecated
* @breaking-change 8.0.0
*/
@ViewChild('underline') underlineRef: ElementRef;

@ViewChild('connectionContainer') _connectionContainerRef: ElementRef;
@ViewChild('inputContainer') _inputContainerRef: ElementRef;
@ViewChild('label') private _label: ElementRef;
@ContentChild(MatFormFieldControl) _control: MatFormFieldControl<any>;
@ContentChild(MatPlaceholder) _placeholderChild: MatPlaceholder;
@ContentChild(MatLabel) _labelChild: MatLabel;
@ViewChild('underline', {static: false}) underlineRef: ElementRef;

@ViewChild('connectionContainer', {static: true}) _connectionContainerRef: ElementRef;
@ViewChild('inputContainer', {static: false}) _inputContainerRef: ElementRef;
@ViewChild('label', {static: false}) private _label: ElementRef;

@ContentChild(MatFormFieldControl, {static: false}) _controlNonStatic: MatFormFieldControl<any>;
@ContentChild(MatFormFieldControl, {static: true}) _controlStatic: MatFormFieldControl<any>;
get _control() {
// TODO(crisbeto): we need this hacky workaround in order to support both Ivy
// and ViewEngine. We should clean this up once Ivy is the default renderer.
return this._explicitFormFieldControl || this._controlNonStatic || this._controlStatic;
}
set _control(value) {
this._explicitFormFieldControl = value;
}
private _explicitFormFieldControl: MatFormFieldControl<any>;

@ContentChild(MatLabel, {static: false}) _labelChildNonStatic: MatLabel;
@ContentChild(MatLabel, {static: true}) _labelChildStatic: MatLabel;
get _labelChild() {
return this._labelChildNonStatic || this._labelChildStatic;
}

@ContentChild(MatPlaceholder, {static: false}) _placeholderChild: MatPlaceholder;
@ContentChildren(MatError) _errorChildren: QueryList<MatError>;
@ContentChildren(MatHint) _hintChildren: QueryList<MatHint>;
@ContentChildren(MatPrefix) _prefixChildren: QueryList<MatPrefix>;
Expand Down
25 changes: 25 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,17 @@ describe('MatSelect', () => {
}));
});

describe('with ngIf and mat-label', () => {
beforeEach(async(() => configureMatSelectTestingModule([SelectWithNgIfAndLabel])));

it('should not throw when using ngIf on a select with an associated label', fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(SelectWithNgIfAndLabel);
fixture.detectChanges();
}).not.toThrow();
}));
});

describe('inside of a form group', () => {
beforeEach(async(() => configureMatSelectTestingModule([SelectInsideFormGroup])));

Expand Down Expand Up @@ -4983,3 +4994,17 @@ class SelectWithoutOptionCentering {
class SelectWithFormFieldLabel {
placeholder: string;
}

@Component({
template: `
<mat-form-field appearance="fill">
<mat-label>Select something</mat-label>
<mat-select *ngIf="showSelect">
<mat-option value="1">One</mat-option>
</mat-select>
</mat-form-field>
`
})
class SelectWithNgIfAndLabel {
showSelect = true;
}
6 changes: 5 additions & 1 deletion tools/public_api_guard/lib/form-field.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ export declare class MatFormField extends _MatFormFieldMixinBase implements Afte
readonly _canLabelFloat: boolean;
_connectionContainerRef: ElementRef;
_control: MatFormFieldControl<any>;
_controlNonStatic: MatFormFieldControl<any>;
_controlStatic: MatFormFieldControl<any>;
_elementRef: ElementRef;
_errorChildren: QueryList<MatError>;
_hintChildren: QueryList<MatHint>;
_hintLabelId: string;
_inputContainerRef: ElementRef;
_labelChild: MatLabel;
readonly _labelChild: MatLabel;
_labelChildNonStatic: MatLabel;
_labelChildStatic: MatLabel;
_labelId: string;
_placeholderChild: MatPlaceholder;
_prefixChildren: QueryList<MatPrefix>;
Expand Down