Skip to content

Commit b3932f7

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 c03d8ac commit b3932f7

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 7 deletions
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.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@ export class _MatOptgroupBase extends _MatOptgroupMixinBase implements CanDisabl
281281
}
282282

283283
// @public
284-
export class MatOption extends _MatOptionBase {
284+
export class MatOption<T = any> extends _MatOptionBase<T> {
285285
constructor(element: ElementRef<HTMLElement>, changeDetectorRef: ChangeDetectorRef, parent: MatOptionParentComponent, group: MatOptgroup);
286286
// (undocumented)
287-
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
287+
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
288288
// (undocumented)
289-
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption, [null, null, { optional: true; }, { optional: true; }]>;
289+
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption<any>, [null, null, { optional: true; }, { optional: true; }]>;
290290
}
291291

292292
// @public (undocumented)
293-
export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
293+
export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
294294
constructor(_element: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _parent: MatOptionParentComponent, group: _MatOptgroupBase);
295295
get active(): boolean;
296296
deselect(): void;
@@ -313,19 +313,19 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
313313
ngAfterViewChecked(): void;
314314
// (undocumented)
315315
ngOnDestroy(): void;
316-
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange>;
316+
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange<T>>;
317317
select(): void;
318318
get selected(): boolean;
319319
_selectViaInteraction(): void;
320320
setActiveStyles(): void;
321321
setInactiveStyles(): void;
322322
readonly _stateChanges: Subject<void>;
323-
value: any;
323+
value: T;
324324
get viewValue(): string;
325325
// (undocumented)
326-
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatOptionBase, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
326+
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatOptionBase<any>, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
327327
// (undocumented)
328-
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptionBase, never>;
328+
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptionBase<any>, never>;
329329
}
330330

331331
// @public (undocumented)
@@ -349,12 +349,12 @@ export interface MatOptionParentComponent {
349349
}
350350

351351
// @public
352-
export class MatOptionSelectionChange {
352+
export class MatOptionSelectionChange<T = any> {
353353
constructor(
354-
source: _MatOptionBase,
354+
source: _MatOptionBase<T>,
355355
isUserInput?: boolean);
356356
isUserInput: boolean;
357-
source: _MatOptionBase;
357+
source: _MatOptionBase<T>;
358358
}
359359

360360
// @public

0 commit comments

Comments
 (0)