Skip to content

Commit 924edd4

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 99ad347 commit 924edd4

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,
@@ -522,10 +522,13 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
522522
return this._control.shouldLabelFloat || this._shouldAlwaysFloat();
523523
}
524524

525-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
526-
_shouldForward(prop: keyof NgControl): boolean {
527-
const ngControl = this._control ? this._control.ngControl : null;
528-
return ngControl && ngControl[prop];
525+
/**
526+
* Determines whether a class from the AbstractControlDirective
527+
* should be forwarded to the host element.
528+
*/
529+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
530+
const control = this._control ? this._control.ngControl : null;
531+
return control && control[prop];
529532
}
530533

531534
/** 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

@@ -399,10 +399,13 @@ export class MatFormField extends _MatFormFieldMixinBase
399399
this._destroyed.complete();
400400
}
401401

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

408411
_hasPlaceholder() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export declare class MatFormField extends _MatFormFieldMixinBase implements Afte
6262
_hasPlaceholder(): boolean;
6363
_hideControlPlaceholder(): boolean;
6464
_shouldAlwaysFloat(): boolean;
65-
_shouldForward(prop: keyof NgControl): boolean;
65+
_shouldForward(prop: keyof AbstractControlDirective): boolean;
6666
_shouldLabelFloat(): boolean;
6767
protected _validateControlChild(): void;
6868
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)