Skip to content

Commit ac8eae8

Browse files
committed
refactor(form-field): allow AbstractControlDirective as MatFormFieldControl.ngControl
Allows for an `AbstractControlDirective` to be passed in as the `MatFormFieldControl.ngControl`. The advantage of passing in the abstract control directive is that it allows both form controls and form groups. This is necessary for the date range picker.
1 parent 4405429 commit ac8eae8

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/material-experimental/mdc-form-field/form-field.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
ViewChild,
2525
ViewEncapsulation
2626
} from '@angular/core';
27-
import {NgControl} from '@angular/forms';
27+
import {AbstractControlDirective} from '@angular/forms';
2828
import {
2929
LabelOptions,
3030
MAT_LABEL_GLOBAL_OPTIONS,
@@ -421,10 +421,13 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
421421
return this._control.shouldLabelFloat || this._shouldAlwaysFloat();
422422
}
423423

424-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
425-
_shouldForward(prop: keyof NgControl): boolean {
426-
const ngControl = this._control ? this._control.ngControl : null;
427-
return ngControl && ngControl[prop];
424+
/**
425+
* Determines whether a class from the AbstractControlDirective
426+
* should be forwarded to the host element.
427+
*/
428+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
429+
const control = this._control ? this._control.ngControl : null;
430+
return control && control[prop];
428431
}
429432

430433
/** Determines whether to display hints or errors. */

src/material/form-field/form-field-control.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Observable} from 'rxjs';
10-
import {NgControl} from '@angular/forms';
10+
import {AbstractControlDirective} from '@angular/forms';
1111
import {Directive} from '@angular/core';
1212

1313

@@ -29,8 +29,8 @@ export abstract class MatFormFieldControl<T> {
2929
/** The placeholder for this control. */
3030
readonly placeholder: string;
3131

32-
/** Gets the NgControl for this control. */
33-
readonly ngControl: NgControl | null;
32+
/** Gets the AbstractControlDirective for this control. */
33+
readonly ngControl: AbstractControlDirective | null;
3434

3535
/** Whether the control is focused. */
3636
readonly focused: boolean;

src/material/form-field/form-field.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {MatPlaceholder} from './placeholder';
5050
import {MatPrefix} from './prefix';
5151
import {MatSuffix} from './suffix';
5252
import {Platform} from '@angular/cdk/platform';
53-
import {NgControl} from '@angular/forms';
53+
import {AbstractControlDirective} from '@angular/forms';
5454
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
5555

5656

@@ -382,10 +382,13 @@ export class MatFormField extends _MatFormFieldMixinBase
382382
this._destroyed.complete();
383383
}
384384

385-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
386-
_shouldForward(prop: keyof NgControl): boolean {
387-
const ngControl = this._control ? this._control.ngControl : null;
388-
return ngControl && ngControl[prop];
385+
/**
386+
* Determines whether a class from the AbstractControlDirective
387+
* should be forwarded to the host element.
388+
*/
389+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
390+
const control = this._control ? this._control.ngControl : null;
391+
return control && control[prop];
389392
}
390393

391394
_hasPlaceholder() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export declare class MatFormField extends _MatFormFieldMixinBase implements Afte
5353
_hasLabel(): boolean;
5454
_hasPlaceholder(): boolean;
5555
_hideControlPlaceholder(): boolean;
56-
_shouldForward(prop: keyof NgControl): boolean;
56+
_shouldForward(prop: keyof AbstractControlDirective): boolean;
5757
_shouldLabelFloat(): boolean;
5858
protected _validateControlChild(): void;
5959
getConnectedOverlayOrigin(): ElementRef;
@@ -81,7 +81,7 @@ export declare abstract class MatFormFieldControl<T> {
8181
readonly errorState: boolean;
8282
readonly focused: boolean;
8383
readonly id: string;
84-
readonly ngControl: NgControl | null;
84+
readonly ngControl: AbstractControlDirective | null;
8585
readonly placeholder: string;
8686
readonly required: boolean;
8787
readonly shouldLabelFloat: boolean;

0 commit comments

Comments
 (0)