Skip to content
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

Add support for color attribute to radio #4039

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions src/demo-app/radio/radio-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ <h1>Basic Example</h1>
<md-radio-button name="group1">Option 2</md-radio-button>
<md-radio-button name="group1" disabled="true">Option 3 (disabled)</md-radio-button>
</section>
<h1>Color Example</h1>
<section class="demo-section">
<md-radio-button name="group2" color="primary">Primary</md-radio-button>
<md-radio-button name="group2" color="accent">Accent</md-radio-button>
<md-radio-button name="group2" color="warn">Warn</md-radio-button>
</section>
<h1>Dynamic Example</h1>
<section class="demo-section">
<div>
Expand Down
52 changes: 44 additions & 8 deletions src/lib/radio/_radio-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,64 @@
.mat-radio-outer-circle {
border-color: mat-color($foreground, secondary-text);

.mat-radio-checked & {
border-color: mat-color($accent);
}

.mat-radio-disabled & {
border-color: mat-color($foreground, disabled);
}
}

.mat-radio-inner-circle {
background-color: mat-color($accent);

.mat-radio-disabled & {
background-color: mat-color($foreground, disabled);
}
}

.mat-radio-ripple .mat-ripple-element {
background-color: mat-color($accent, 0.26);

.mat-radio-disabled & {
background-color: mat-color($foreground, disabled);
}
}

.mat-radio-button.mat-radio-checked {
&.mat-primary {
.mat-radio-outer-circle{
border-color: mat-color($primary);
}

.mat-radio-inner-circle {
background-color: mat-color($primary);
}

.mat-radio-ripple .mat-ripple-element {
background-color: mat-color($primary, 0.26);
}
}

&.mat-accent {
.mat-radio-outer-circle{
border-color: mat-color($accent);
}

.mat-radio-inner-circle {
background-color: mat-color($accent);
}

.mat-radio-ripple .mat-ripple-element {
background-color: mat-color($accent, 0.26);
}
}

&.mat-warn {
.mat-radio-outer-circle{
border-color: mat-color($warn);
}

.mat-radio-inner-circle {
background-color: mat-color($warn);
}

.mat-radio-ripple .mat-ripple-element {
background-color: mat-color($warn, 0.26);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling that we could move that into a mixin? So we don't have to repeat code three times.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure ;)
I will create a mixin, you're right.

}
}
25 changes: 25 additions & 0 deletions src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy {
this._disabled = (value != null && value !== false) ? true : null;
}

/** The color of the button. Can be `primary`, `accent`, or `warn`. */
@Input()
get color(): string {
return this._color;
}

set color(value: string) {
this._updateColor(value);
}

/**
* Event emitted when the checked state of this radio button changes.
* Change events are only emitted when the value changes due to user interaction with
Expand Down Expand Up @@ -402,6 +412,9 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy {
/** Whether the ripple effect on click should be disabled. */
private _disableRipple: boolean;

/** Current color. Can be `primary`, `accent`, or `warn`. */
private _color: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type should use the string literal values

private _color: 'primary' | 'accent' | 'warn';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to use straight color values like for example #00FF00 or rgb(255,100,0) from HTML ?


/** The child ripple instance. */
@ViewChild(MdRipple) _ripple: MdRipple;

Expand Down Expand Up @@ -498,6 +511,18 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy {
}
}

_updateColor(newColor: string) {
this._setElementColor(this._color, false);
this._setElementColor(newColor, true);
this._color = newColor;
}

_setElementColor(color: string, isAdd: boolean) {
if (color != null && color != '') {
this._renderer.setElementClass(this._elementRef.nativeElement, `mat-${color}`, isAdd);
}
}

/** Function is called whenever the focus changes for the input element. */
private _onInputFocusChange(focusOrigin: FocusOrigin) {
if (!this._focusRipple && focusOrigin === 'keyboard') {
Expand Down