Skip to content

Commit 8ebc46d

Browse files
committed
refactor(material/core): no longer define stateChanges in mixinErrorState
The error state mixin provided in `@angular/material/core` currently defines the `stateChanges` class member as part of the mixin. This is unexpected as consumers might deal with the `stateChanges` member differently. e.g. in some components the `stateChanges` field is intended to show up in the docs, or the JSDoc descripton varies. e.g. the observable could emit whenever form-field state changes, and it should be updated, or it emits always when something changes (e.g. even a component input which is not relevant for the form-field control). In general we want to avoid this member being defined in the mixin as the mixin is rather about the error state, and not defining a subject that can emit whenever "state" changes.
1 parent 8424209 commit 8ebc46d

File tree

7 files changed

+58
-15
lines changed

7 files changed

+58
-15
lines changed

src/material-experimental/mdc-chips/chip-grid.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from '@angular/material-experimental/mdc-core';
3737
import {MatFormFieldControl} from '@angular/material-experimental/mdc-form-field';
3838
import {MatChipTextControl} from './chip-text-control';
39-
import {merge, Observable, Subscription} from 'rxjs';
39+
import {merge, Observable, Subject, Subscription} from 'rxjs';
4040
import {startWith, takeUntil} from 'rxjs/operators';
4141
import {MatChipEvent} from './chip';
4242
import {MatChipRow} from './chip-row';
@@ -57,13 +57,24 @@ export class MatChipGridChange {
5757
* @docs-private
5858
*/
5959
class MatChipGridBase extends MatChipSet {
60+
/**
61+
* Emits whenever the component state changes and should cause the parent
62+
* form-field to update. Implemented as part of `MatFormFieldControl`.
63+
* @docs-private
64+
*/
65+
readonly stateChanges = new Subject<void>();
66+
6067
constructor(_elementRef: ElementRef,
6168
_changeDetectorRef: ChangeDetectorRef,
6269
_dir: Directionality,
6370
public _defaultErrorStateMatcher: ErrorStateMatcher,
6471
public _parentForm: NgForm,
6572
public _parentFormGroup: FormGroupDirective,
66-
/** @docs-private */
73+
/**
74+
* Form control bound to the component.
75+
* Implemented as part of `MatFormFieldControl`.
76+
* @docs-private
77+
*/
6778
public ngControl: NgControl) {
6879
super(_elementRef, _changeDetectorRef, _dir);
6980
}

src/material/chips/chip-list.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,21 @@ import {MatChipTextControl} from './chip-text-control';
4343
// Boilerplate for applying mixins to MatChipList.
4444
/** @docs-private */
4545
const _MatChipListBase = mixinErrorState(class {
46+
/**
47+
* Emits whenever the component state changes and should cause the parent
48+
* form-field to update. Implemented as part of `MatFormFieldControl`.
49+
* @docs-private
50+
*/
51+
readonly stateChanges = new Subject<void>();
52+
4653
constructor(public _defaultErrorStateMatcher: ErrorStateMatcher,
4754
public _parentForm: NgForm,
4855
public _parentFormGroup: FormGroupDirective,
49-
/** @docs-private */
56+
/**
57+
* Form control bound to the component.
58+
* Implemented as part of `MatFormFieldControl`.
59+
* @docs-private
60+
*/
5061
public ngControl: NgControl) {}
5162
});
5263

src/material/core/common-behaviors/error-state.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {AbstractConstructor, Constructor} from './constructor';
1414

1515
/** @docs-private */
1616
export interface CanUpdateErrorState {
17-
/** Emits whenever the component state changes. */
18-
readonly stateChanges: Subject<void>;
1917
/** Updates the error state based on the provided error state matcher. */
2018
updateErrorState(): void;
2119
/** Whether the component is in an error state. */
@@ -37,7 +35,12 @@ export interface HasErrorState {
3735
_parentFormGroup: FormGroupDirective;
3836
_parentForm: NgForm;
3937
_defaultErrorStateMatcher: ErrorStateMatcher;
38+
39+
// These properties are defined as per the `MatFormFieldControl` interface. Since
40+
// this mixin is commonly used with custom form-field controls, we respect the
41+
// properties (also with the public name they need according to `MatFormFieldControl`).
4042
ngControl: NgControl;
43+
stateChanges: Subject<void>;
4144
}
4245

4346
/**
@@ -49,13 +52,6 @@ export function mixinErrorState<T extends AbstractConstructor<HasErrorState>>(ba
4952
export function mixinErrorState<T extends Constructor<HasErrorState>>(base: T):
5053
CanUpdateErrorStateCtor & T {
5154
return class extends base {
52-
// This class member exists as an interop with `MatFormFieldControl` which expects
53-
// a public `stateChanges` observable to emit whenever the form field should be updated.
54-
// The description is not specifically mentioning the error state, as classes using this
55-
// mixin can/should emit an event in other cases too.
56-
/** Emits whenever the component state changes. */
57-
readonly stateChanges = new Subject<void>();
58-
5955
/** Whether the component is in an error state. */
6056
errorState: boolean = false;
6157

src/material/datepicker/date-range-input-parts.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ export const MAT_DATE_RANGE_INPUT_PARENT =
7171
abstract class MatDateRangeInputPartBase<D>
7272
extends MatDatepickerInputBase<DateRange<D>> implements OnInit, DoCheck {
7373

74-
/** @docs-private */
74+
/**
75+
* Form control bound to this input part.
76+
* @docs-private
77+
*/
7578
ngControl: NgControl;
7679

7780
/** @docs-private */

src/material/input/input.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ let nextUniqueId = 0;
5353
// Boilerplate for applying mixins to MatInput.
5454
/** @docs-private */
5555
const _MatInputBase = mixinErrorState(class {
56+
/**
57+
* Emits whenever the component state changes and should cause the parent
58+
* form-field to update. Implemented as part of `MatFormFieldControl`.
59+
* @docs-private
60+
*/
61+
readonly stateChanges = new Subject<void>();
62+
5663
constructor(public _defaultErrorStateMatcher: ErrorStateMatcher,
5764
public _parentForm: NgForm,
5865
public _parentFormGroup: FormGroupDirective,
59-
/** @docs-private */
66+
/**
67+
* Form control bound to the component.
68+
* Implemented as part of `MatFormFieldControl`.
69+
* @docs-private
70+
*/
6071
public ngControl: NgControl) {}
6172
});
6273

src/material/select/select.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,22 @@ export class MatSelectChange {
181181
/** @docs-private */
182182
const _MatSelectMixinBase =
183183
mixinDisableRipple(mixinTabIndex(mixinDisabled(mixinErrorState(class {
184+
/**
185+
* Emits whenever the component state changes and should cause the parent
186+
* form-field to update. Implemented as part of `MatFormFieldControl`.
187+
* @docs-private
188+
*/
189+
readonly stateChanges = new Subject<void>();
190+
184191
constructor(public _elementRef: ElementRef,
185192
public _defaultErrorStateMatcher: ErrorStateMatcher,
186193
public _parentForm: NgForm,
187194
public _parentFormGroup: FormGroupDirective,
195+
/**
196+
* Form control bound to the component.
197+
* Implemented as part of `MatFormFieldControl`.
198+
* @docs-private
199+
*/
188200
public ngControl: NgControl) {}
189201
}))));
190202

tools/public_api_guard/material/core.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export type CanDisableRippleCtor = Constructor<CanDisableRipple> & AbstractConst
8484
export interface CanUpdateErrorState {
8585
errorState: boolean;
8686
errorStateMatcher: ErrorStateMatcher;
87-
readonly stateChanges: Subject<void>;
8887
updateErrorState(): void;
8988
}
9089

0 commit comments

Comments
 (0)