Skip to content

Commit 26ecb8c

Browse files
committed
refactor(form-field): explicitly set static flag on all queries
Continuation of #15680. Explicitly marks all ViewChild and ContentChild queries so that the timing is consistent for apps using Ivy or ViewEngine.
1 parent 48c70da commit 26ecb8c

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/lib/form-field/form-field.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,31 @@ export class MatFormField extends _MatFormFieldMixinBase
235235
* @deprecated
236236
* @breaking-change 8.0.0
237237
*/
238-
@ViewChild('underline') underlineRef: ElementRef;
239-
240-
@ViewChild('connectionContainer') _connectionContainerRef: ElementRef;
241-
@ViewChild('inputContainer') _inputContainerRef: ElementRef;
242-
@ViewChild('label') private _label: ElementRef;
243-
@ContentChild(MatFormFieldControl) _control: MatFormFieldControl<any>;
244-
@ContentChild(MatPlaceholder) _placeholderChild: MatPlaceholder;
245-
@ContentChild(MatLabel) _labelChild: MatLabel;
238+
@ViewChild('underline', {static: false}) underlineRef: ElementRef;
239+
240+
@ViewChild('connectionContainer', {static: true}) _connectionContainerRef: ElementRef;
241+
@ViewChild('inputContainer', {static: false}) _inputContainerRef: ElementRef;
242+
@ViewChild('label', {static: false}) private _label: ElementRef;
243+
244+
@ContentChild(MatFormFieldControl, {static: false}) _controlNonStatic: MatFormFieldControl<any>;
245+
@ContentChild(MatFormFieldControl, {static: true}) _controlStatic: MatFormFieldControl<any>;
246+
get _control() {
247+
// TODO(crisbeto): we need this hacky workaround in order to support both Ivy
248+
// and ViewEngine. We should clean this up once Ivy is the default renderer.
249+
return this._explicitFormFieldControl || this._controlNonStatic || this._controlStatic;
250+
}
251+
set _control(value) {
252+
this._explicitFormFieldControl = value;
253+
}
254+
private _explicitFormFieldControl: MatFormFieldControl<any>;
255+
256+
@ContentChild(MatLabel, {static: false}) _labelChildNonStatic: MatLabel;
257+
@ContentChild(MatLabel, {static: true}) _labelChildStatic: MatLabel;
258+
get _labelChild() {
259+
return this._labelChildNonStatic || this._labelChildStatic;
260+
}
261+
262+
@ContentChild(MatPlaceholder, {static: false}) _placeholderChild: MatPlaceholder;
246263
@ContentChildren(MatError) _errorChildren: QueryList<MatError>;
247264
@ContentChildren(MatHint) _hintChildren: QueryList<MatHint>;
248265
@ContentChildren(MatPrefix) _prefixChildren: QueryList<MatPrefix>;

src/lib/select/select.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,17 @@ describe('MatSelect', () => {
24412441
}));
24422442
});
24432443

2444+
describe('with ngIf and mat-label', () => {
2445+
beforeEach(async(() => configureMatSelectTestingModule([SelectWithNgIfAndLabel])));
2446+
2447+
it('should not throw when using ngIf on a select with an associated label', fakeAsync(() => {
2448+
expect(() => {
2449+
const fixture = TestBed.createComponent(SelectWithNgIfAndLabel);
2450+
fixture.detectChanges();
2451+
}).not.toThrow();
2452+
}));
2453+
});
2454+
24442455
describe('inside of a form group', () => {
24452456
beforeEach(async(() => configureMatSelectTestingModule([SelectInsideFormGroup])));
24462457

@@ -4983,3 +4994,17 @@ class SelectWithoutOptionCentering {
49834994
class SelectWithFormFieldLabel {
49844995
placeholder: string;
49854996
}
4997+
4998+
@Component({
4999+
template: `
5000+
<mat-form-field appearance="fill">
5001+
<mat-label>Select something</mat-label>
5002+
<mat-select *ngIf="showSelect">
5003+
<mat-option value="1">One</mat-option>
5004+
</mat-select>
5005+
</mat-form-field>
5006+
`
5007+
})
5008+
class SelectWithNgIfAndLabel {
5009+
showSelect = true;
5010+
}

tools/public_api_guard/lib/form-field.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ export declare class MatFormField extends _MatFormFieldMixinBase implements Afte
1818
readonly _canLabelFloat: boolean;
1919
_connectionContainerRef: ElementRef;
2020
_control: MatFormFieldControl<any>;
21+
_controlNonStatic: MatFormFieldControl<any>;
22+
_controlStatic: MatFormFieldControl<any>;
2123
_elementRef: ElementRef;
2224
_errorChildren: QueryList<MatError>;
2325
_hintChildren: QueryList<MatHint>;
2426
_hintLabelId: string;
2527
_inputContainerRef: ElementRef;
26-
_labelChild: MatLabel;
28+
readonly _labelChild: MatLabel;
29+
_labelChildNonStatic: MatLabel;
30+
_labelChildStatic: MatLabel;
2731
_labelId: string;
2832
_placeholderChild: MatPlaceholder;
2933
_prefixChildren: QueryList<MatPrefix>;

0 commit comments

Comments
 (0)