Skip to content

Commit f22c92a

Browse files
committed
refactor(material/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 7b3006f commit f22c92a

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
@@ -27,7 +27,7 @@ import {
2727
ViewChild,
2828
ViewEncapsulation
2929
} from '@angular/core';
30-
import {NgControl} from '@angular/forms';
30+
import {AbstractControlDirective} from '@angular/forms';
3131
import {ThemePalette} from '@angular/material-experimental/mdc-core';
3232
import {
3333
getMatFormFieldDuplicatedHintError,
@@ -571,10 +571,13 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
571571
return this._control.shouldLabelFloat || this._shouldAlwaysFloat();
572572
}
573573

574-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
575-
_shouldForward(prop: keyof NgControl): boolean {
576-
const ngControl = this._control ? this._control.ngControl : null;
577-
return ngControl && ngControl[prop];
574+
/**
575+
* Determines whether a class from the AbstractControlDirective
576+
* should be forwarded to the host element.
577+
*/
578+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
579+
const control = this._control ? this._control.ngControl : null;
580+
return control && control[prop];
578581
}
579582

580583
/** 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
@@ -48,7 +48,7 @@ import {MatPlaceholder} from './placeholder';
4848
import {MAT_PREFIX, MatPrefix} from './prefix';
4949
import {MAT_SUFFIX, MatSuffix} from './suffix';
5050
import {Platform} from '@angular/cdk/platform';
51-
import {NgControl} from '@angular/forms';
51+
import {AbstractControlDirective} from '@angular/forms';
5252
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
5353

5454

@@ -388,10 +388,13 @@ export class MatFormField extends _MatFormFieldBase
388388
this._destroyed.complete();
389389
}
390390

391-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
392-
_shouldForward(prop: keyof NgControl): boolean {
393-
const ngControl = this._control ? this._control.ngControl : null;
394-
return ngControl && ngControl[prop];
391+
/**
392+
* Determines whether a class from the AbstractControlDirective
393+
* should be forwarded to the host element.
394+
*/
395+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
396+
const control = this._control ? this._control.ngControl : null;
397+
return control && control[prop];
395398
}
396399

397400
_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 _MatFormFieldBase implements AfterCont
6363
_hasPlaceholder(): boolean;
6464
_hideControlPlaceholder(): boolean;
6565
_shouldAlwaysFloat(): boolean;
66-
_shouldForward(prop: keyof NgControl): boolean;
66+
_shouldForward(prop: keyof AbstractControlDirective): boolean;
6767
_shouldLabelFloat(): boolean;
6868
protected _validateControlChild(): void;
6969
getConnectedOverlayOrigin(): ElementRef;
@@ -92,7 +92,7 @@ export declare abstract class MatFormFieldControl<T> {
9292
readonly errorState: boolean;
9393
readonly focused: boolean;
9494
readonly id: string;
95-
readonly ngControl: NgControl | null;
95+
readonly ngControl: NgControl | AbstractControlDirective | null;
9696
readonly placeholder: string;
9797
readonly required: boolean;
9898
readonly shouldLabelFloat: boolean;

0 commit comments

Comments
 (0)