Skip to content

Commit 4a9b5f8

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 27e60e8 commit 4a9b5f8

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, NgControl} from '@angular/forms';
3131
import {ThemePalette} from '@angular/material-experimental/mdc-core';
3232
import {
3333
getMatFormFieldDuplicatedHintError,
@@ -546,10 +546,13 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
546546
return this._control.shouldLabelFloat || this._shouldAlwaysFloat();
547547
}
548548

549-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
550-
_shouldForward(prop: keyof NgControl): boolean {
551-
const ngControl = this._control ? this._control.ngControl : null;
552-
return ngControl && ngControl[prop];
549+
/**
550+
* Determines whether a class from the AbstractControlDirective
551+
* should be forwarded to the host element.
552+
*/
553+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
554+
const control = this._control ? this._control.ngControl : null;
555+
return control && control[prop];
553556
}
554557

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

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

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

406409
_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
_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)