Skip to content

Commit 0271043

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 2c48fc8 commit 0271043

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-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

@@ -375,10 +375,13 @@ export class MatFormField extends _MatFormFieldBase
375375
this._destroyed.complete();
376376
}
377377

378-
/** Determines whether a class from the NgControl should be forwarded to the host element. */
379-
_shouldForward(prop: keyof NgControl): boolean {
380-
const ngControl = this._control ? this._control.ngControl : null;
381-
return ngControl && ngControl[prop];
378+
/**
379+
* Determines whether a class from the AbstractControlDirective
380+
* should be forwarded to the host element.
381+
*/
382+
_shouldForward(prop: keyof AbstractControlDirective): boolean {
383+
const control = this._control ? this._control.ngControl : null;
384+
return control && control[prop];
382385
}
383386

384387
_hasPlaceholder() {

tools/public_api_guard/material/form-field.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
```ts
66

77
import { AbstractConstructor } from '@angular/material/core/common-behaviors/constructor';
8+
import { AbstractControlDirective } from '@angular/forms';
89
import { AfterContentChecked } from '@angular/core';
910
import { AfterContentInit } from '@angular/core';
1011
import { AfterViewInit } from '@angular/core';
@@ -133,7 +134,7 @@ export class MatFormField extends _MatFormFieldBase implements AfterContentInit,
133134
// (undocumented)
134135
_prefixChildren: QueryList<MatPrefix>;
135136
_shouldAlwaysFloat(): boolean;
136-
_shouldForward(prop: keyof NgControl): boolean;
137+
_shouldForward(prop: keyof AbstractControlDirective): boolean;
137138
// (undocumented)
138139
_shouldLabelFloat(): boolean;
139140
_subscriptAnimationState: string;
@@ -164,7 +165,7 @@ export abstract class MatFormFieldControl<T> {
164165
readonly errorState: boolean;
165166
readonly focused: boolean;
166167
readonly id: string;
167-
readonly ngControl: NgControl | null;
168+
readonly ngControl: NgControl | AbstractControlDirective | null;
168169
abstract onContainerClick(event: MouseEvent): void;
169170
readonly placeholder: string;
170171
readonly required: boolean;

0 commit comments

Comments
 (0)