From 67f3c0b1410750fc5a316a69da81083af17b8d22 Mon Sep 17 00:00:00 2001 From: Nathan Tate Date: Wed, 25 Sep 2019 12:22:07 -0700 Subject: [PATCH] feat(material-experimental/radio): Set up the MDC foundation (#17180) --- src/dev-app/mdc-radio/mdc-radio-demo.html | 2 +- .../mdc-radio/BUILD.bazel | 1 + .../mdc-radio/radio.html | 4 +- src/material-experimental/mdc-radio/radio.ts | 51 +++++++++++++++++-- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/dev-app/mdc-radio/mdc-radio-demo.html b/src/dev-app/mdc-radio/mdc-radio-demo.html index 841ddb1dd198..080d5f205f30 100644 --- a/src/dev-app/mdc-radio/mdc-radio-demo.html +++ b/src/dev-app/mdc-radio/mdc-radio-demo.html @@ -2,5 +2,5 @@

Basic Example

Option 1 Option 2 - Option 3 + Option 3 (Disabled)
diff --git a/src/material-experimental/mdc-radio/BUILD.bazel b/src/material-experimental/mdc-radio/BUILD.bazel index 8f29ad25324c..3ee657e8ec96 100644 --- a/src/material-experimental/mdc-radio/BUILD.bazel +++ b/src/material-experimental/mdc-radio/BUILD.bazel @@ -14,6 +14,7 @@ ng_module( module_name = "@angular/material-experimental/mdc-radio", deps = [ "//src/material/core", + "@npm//@material/radio", ], ) diff --git a/src/material-experimental/mdc-radio/radio.html b/src/material-experimental/mdc-radio/radio.html index f6d686ac9e50..d28f21dd56e1 100644 --- a/src/material-experimental/mdc-radio/radio.html +++ b/src/material-experimental/mdc-radio/radio.html @@ -1,6 +1,6 @@
-
- +
+
diff --git a/src/material-experimental/mdc-radio/radio.ts b/src/material-experimental/mdc-radio/radio.ts index 7f11085282e7..f52c69a804f9 100644 --- a/src/material-experimental/mdc-radio/radio.ts +++ b/src/material-experimental/mdc-radio/radio.ts @@ -6,7 +6,17 @@ * found in the LICENSE file at https://angular.io/license */ -import {ChangeDetectionStrategy, Component, ViewEncapsulation, Input} from '@angular/core'; +import { + ChangeDetectionStrategy, + Component, + ViewEncapsulation, + Input, + OnDestroy, + AfterViewInit, + ChangeDetectorRef, +} from '@angular/core'; +import {coerceBooleanProperty} from '@angular/cdk/coercion'; +import {MDCRadioAdapter, MDCRadioFoundation} from '@material/radio'; // Increasing integer for generating unique ids for radio components. let nextUniqueId = 0; @@ -24,15 +34,50 @@ let nextUniqueId = 0; encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, }) -export class MatRadio { +export class MatRadio implements AfterViewInit, OnDestroy { private _uniqueId: string = `mat-radio-${++nextUniqueId}`; + private _radioAdapter: MDCRadioAdapter = { + addClass: (className: string) => this._setClass(className, true), + removeClass: (className: string) => this._setClass(className, false), + setNativeControlDisabled: (disabled: boolean) => { + this._disabled = disabled; + this._changeDetectorRef.markForCheck(); + }, + }; + + _radioFoundation = new MDCRadioFoundation(this._radioAdapter); + _classes: {[key: string]: boolean} = {}; + /** The unique ID for the radio button. */ @Input() id: string = this._uniqueId; + /** Whether the radio button is disabled. */ + @Input() + get disabled(): boolean { + return this._disabled; + } + set disabled(disabled: boolean) { + this._radioFoundation.setDisabled(coerceBooleanProperty(disabled)); + } + private _disabled = false; + + constructor(private _changeDetectorRef: ChangeDetectorRef) {} + /** ID of the native input element inside `` */ get inputId(): string { return `${this.id || this._uniqueId}-input`; } - // TODO: set up MDC foundation class. + ngAfterViewInit() { + this._radioFoundation.init(); + } + + ngOnDestroy() { + this._radioFoundation.destroy(); + } + + private _setClass(cssClass: string, active: boolean) { + this._classes = {...this._classes, [cssClass]: active}; + this._changeDetectorRef.markForCheck(); + } }