Skip to content

Commit 9f102bd

Browse files
committed
fix(material/core): make MatOption generic
Makes `MatOption` generic so that consumers can type its `value` to something different from `any`. Fixes #19456.
1 parent 119684e commit 9f102bd

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/material-experimental/mdc-core/option/option.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {MatOptgroup} from './optgroup';
4848
encapsulation: ViewEncapsulation.None,
4949
changeDetection: ChangeDetectionStrategy.OnPush,
5050
})
51-
export class MatOption extends _MatOptionBase {
51+
export class MatOption<T = any> extends _MatOptionBase<T> {
5252
constructor(
5353
element: ElementRef<HTMLElement>,
5454
changeDetectorRef: ChangeDetectorRef,

src/material/core/option/option.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ import {MatOptionParentComponent, MAT_OPTION_PARENT_COMPONENT} from './option-pa
3636
let _uniqueIdCounter = 0;
3737

3838
/** Event object emitted by MatOption when selected or deselected. */
39-
export class MatOptionSelectionChange {
39+
export class MatOptionSelectionChange<T = any> {
4040
constructor(
4141
/** Reference to the option that emitted the event. */
42-
public source: _MatOptionBase,
42+
public source: _MatOptionBase<T>,
4343
/** Whether the change in the option's value was a result of a user action. */
4444
public isUserInput = false) { }
4545
}
4646

4747
@Directive()
48-
export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
48+
export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
4949
private _selected = false;
5050
private _active = false;
5151
private _disabled = false;
@@ -58,7 +58,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
5858
get selected(): boolean { return this._selected; }
5959

6060
/** The form value of the option. */
61-
@Input() value: any;
61+
@Input() value: T;
6262

6363
/** The unique ID of the option. */
6464
@Input() id: string = `mat-option-${_uniqueIdCounter++}`;
@@ -73,7 +73,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
7373

7474
/** Event emitted when the option is selected or deselected. */
7575
// tslint:disable-next-line:no-output-on-prefix
76-
@Output() readonly onSelectionChange = new EventEmitter<MatOptionSelectionChange>();
76+
@Output() readonly onSelectionChange = new EventEmitter<MatOptionSelectionChange<T>>();
7777

7878
/** Emits when the state of the option changes and any parents have to be notified. */
7979
readonly _stateChanges = new Subject<void>();
@@ -225,7 +225,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
225225

226226
/** Emits the selection change event. */
227227
private _emitSelectionChangeEvent(isUserInput = false): void {
228-
this.onSelectionChange.emit(new MatOptionSelectionChange(this, isUserInput));
228+
this.onSelectionChange.emit(new MatOptionSelectionChange<T>(this, isUserInput));
229229
}
230230

231231
static ngAcceptInputType_disabled: BooleanInput;
@@ -256,7 +256,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
256256
encapsulation: ViewEncapsulation.None,
257257
changeDetection: ChangeDetectionStrategy.OnPush,
258258
})
259-
export class MatOption extends _MatOptionBase {
259+
export class MatOption<T = any> extends _MatOptionBase<T> {
260260
constructor(
261261
element: ElementRef<HTMLElement>,
262262
changeDetectorRef: ChangeDetectorRef,

tools/public_api_guard/material/core.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export declare class _MatOptgroupBase extends _MatOptgroupMixinBase implements C
1212
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptgroupBase, [{ optional: true; }]>;
1313
}
1414

15-
export declare class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
15+
export declare class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
1616
readonly _stateChanges: Subject<void>;
1717
get active(): boolean;
1818
get disableRipple(): boolean | undefined;
@@ -21,9 +21,9 @@ export declare class _MatOptionBase implements FocusableOption, AfterViewChecked
2121
readonly group: _MatOptgroupBase;
2222
id: string;
2323
get multiple(): boolean | undefined;
24-
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange>;
24+
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange<T>>;
2525
get selected(): boolean;
26-
value: any;
26+
value: T;
2727
get viewValue(): string;
2828
constructor(_element: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _parent: MatOptionParentComponent, group: _MatOptgroupBase);
2929
_getAriaSelected(): boolean | null;
@@ -40,8 +40,8 @@ export declare class _MatOptionBase implements FocusableOption, AfterViewChecked
4040
setActiveStyles(): void;
4141
setInactiveStyles(): void;
4242
static ngAcceptInputType_disabled: BooleanInput;
43-
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatOptionBase, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
44-
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptionBase, never>;
43+
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatOptionBase<any>, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
44+
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptionBase<any>, never>;
4545
}
4646

4747
export declare class AnimationCurves {
@@ -209,10 +209,10 @@ export declare class MatOptgroup extends _MatOptgroupBase {
209209
static ɵfac: i0.ɵɵFactoryDeclaration<MatOptgroup, never>;
210210
}
211211

212-
export declare class MatOption extends _MatOptionBase {
212+
export declare class MatOption<T = any> extends _MatOptionBase<T> {
213213
constructor(element: ElementRef<HTMLElement>, changeDetectorRef: ChangeDetectorRef, parent: MatOptionParentComponent, group: MatOptgroup);
214-
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
215-
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption, [null, null, { optional: true; }, { optional: true; }]>;
214+
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
215+
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption<any>, [null, null, { optional: true; }, { optional: true; }]>;
216216
}
217217

218218
export declare class MatOptionModule {
@@ -227,11 +227,11 @@ export interface MatOptionParentComponent {
227227
multiple?: boolean;
228228
}
229229

230-
export declare class MatOptionSelectionChange {
230+
export declare class MatOptionSelectionChange<T = any> {
231231
isUserInput: boolean;
232-
source: _MatOptionBase;
232+
source: _MatOptionBase<T>;
233233
constructor(
234-
source: _MatOptionBase,
234+
source: _MatOptionBase<T>,
235235
isUserInput?: boolean);
236236
}
237237

0 commit comments

Comments
 (0)