Skip to content

refactor(material-experimental/mdc-radio): remove adapter usage #24925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-radio/radio.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="mdc-form-field" #formField
[class.mdc-form-field--align-end]="labelPosition == 'before'">
<div class="mdc-radio" [ngClass]="_classes">
<div class="mdc-radio" [class.mdc-radio--disabled]="disabled">
<!-- Render this element first so the input is on top. -->
<div class="mat-mdc-radio-touch-target" (click)="_onInputInteraction($event)"></div>
<input #input class="mdc-radio__native-control" type="radio"
Expand Down
43 changes: 1 addition & 42 deletions src/material-experimental/mdc-radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import {
AfterViewInit,
Attribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Expand All @@ -18,12 +17,10 @@ import {
forwardRef,
Inject,
InjectionToken,
OnDestroy,
Optional,
QueryList,
ViewEncapsulation,
} from '@angular/core';
import {MDCRadioAdapter, MDCRadioFoundation} from '@material/radio';
import {
MAT_RADIO_DEFAULT_OPTIONS,
_MatRadioButtonBase,
Expand Down Expand Up @@ -107,21 +104,7 @@ export class MatRadioGroup extends _MatRadioGroupBase<MatRadioButton> {
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatRadioButton extends _MatRadioButtonBase implements AfterViewInit, OnDestroy {
private _radioAdapter: MDCRadioAdapter = {
addClass: (className: string) => this._setClass(className, true),
removeClass: (className: string) => this._setClass(className, false),
setNativeControlDisabled: (disabled: boolean) => {
if (this.disabled !== disabled) {
this.disabled = disabled;
this._changeDetector.markForCheck();
}
},
};

_radioFoundation = new MDCRadioFoundation(this._radioAdapter);
_classes: {[key: string]: boolean} = {};

export class MatRadioButton extends _MatRadioButtonBase {
constructor(
@Optional() @Inject(MAT_RADIO_GROUP) radioGroup: MatRadioGroup,
elementRef: ElementRef,
Expand All @@ -145,28 +128,4 @@ export class MatRadioButton extends _MatRadioButtonBase implements AfterViewInit
tabIndex,
);
}

override ngAfterViewInit() {
super.ngAfterViewInit();
this._radioFoundation.init();
}

override ngOnDestroy() {
super.ngOnDestroy();
this._radioFoundation.destroy();
}

private _setClass(cssClass: string, active: boolean) {
this._classes = {...this._classes, [cssClass]: active};
this._changeDetector.markForCheck();
}

/**
* Overrides the parent function so that the foundation can be set with the current
* disabled state.
*/
protected override _setDisabled(value: boolean) {
super._setDisabled(value);
this._radioFoundation.setDisabled(this.disabled);
}
}