Skip to content

Commit bff7b4b

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 db4b0cd commit bff7b4b

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
@@ -28,7 +28,7 @@ import {
2828
ViewChild,
2929
ViewEncapsulation
3030
} from '@angular/core';
31-
import {NgControl} from '@angular/forms';
31+
import {AbstractControlDirective} from '@angular/forms';
3232
import {LabelOptions, MAT_LABEL_GLOBAL_OPTIONS, ThemePalette} from '@angular/material/core';
3333
import {
3434
getMatFormFieldDuplicatedHintError,
@@ -515,10 +515,13 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
515515
return this._control.shouldLabelFloat || this._shouldAlwaysFloat();
516516
}
517517

518-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
519-
_shouldForward(prop: keyof NgControl): boolean {
520-
const ngControl = this._control ? this._control.ngControl : null;
521-
return ngControl && ngControl[prop];
518+
/**
519+
* Determines whether a class from the AbstractControlDirective
520+
* should be forwarded to the host element.
521+
*/
522+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
523+
const control = this._control ? this._control.ngControl : null;
524+
return control && control[prop];
522525
}
523526

524527
/** 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, NgControl} 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: 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 {MAT_PREFIX, MatPrefix} from './prefix';
5151
import {MAT_SUFFIX, 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

@@ -396,10 +396,13 @@ export class MatFormField extends _MatFormFieldMixinBase
396396
this._destroyed.complete();
397397
}
398398

399-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
400-
_shouldForward(prop: keyof NgControl): boolean {
401-
const ngControl = this._control ? this._control.ngControl : null;
402-
return ngControl && ngControl[prop];
399+
/**
400+
* Determines whether a class from the AbstractControlDirective
401+
* should be forwarded to the host element.
402+
*/
403+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
404+
const control = this._control ? this._control.ngControl : null;
405+
return control && control[prop];
403406
}
404407

405408
_hasPlaceholder() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export declare class MatFormField extends _MatFormFieldMixinBase implements Afte
6363
_hasLabel(): boolean;
6464
_hasPlaceholder(): boolean;
6565
_hideControlPlaceholder(): boolean;
66-
_shouldForward(prop: keyof NgControl): boolean;
66+
_shouldForward(prop: keyof AbstractControlDirective): boolean;
6767
_shouldLabelFloat(): boolean;
6868
protected _validateControlChild(): void;
6969
getConnectedOverlayOrigin(): ElementRef;
@@ -91,7 +91,7 @@ export declare abstract class MatFormFieldControl<T> {
9191
readonly errorState: boolean;
9292
readonly focused: boolean;
9393
readonly id: string;
94-
readonly ngControl: NgControl | null;
94+
readonly ngControl: NgControl | AbstractControlDirective | null;
9595
readonly placeholder: string;
9696
readonly required: boolean;
9797
readonly shouldLabelFloat: boolean;

0 commit comments

Comments
 (0)