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 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
7 changes: 7 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,13 @@ <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">Default (accent)</md-radio-button>
<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
66 changes: 45 additions & 21 deletions src/lib/radio/_radio-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,57 @@
$background: map-get($theme, background);
$foreground: map-get($theme, foreground);

.mat-radio-outer-circle {
border-color: mat-color($foreground, secondary-text);
}
.mat-radio-button {
//default color (accent)
Copy link
Member

Choose a reason for hiding this comment

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

Our indentation is +2, not +4

&:not(.mat-primary):not(.mat-accent):not(.mat-warn) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this :not expression should be necessary; the radio should just have accent be the default color.

@include radio-theme-color($accent, $foreground);
}

.mat-radio-checked .mat-radio-outer-circle {
border-color: mat-color($accent);
}
&.mat-primary {
@include radio-theme-color($primary, $foreground);
}

.mat-radio-disabled .mat-radio-outer-circle {
border-color: mat-color($foreground, disabled);
}
&.mat-accent {
@include radio-theme-color($accent, $foreground);
}

.mat-radio-inner-circle {
background-color: mat-color($accent);
}
&.mat-warn {
@include radio-theme-color($warn, $foreground);
}
}
}

.mat-radio-ripple .mat-ripple-element {
background-color: mat-color($accent, 0.26);
}
@mixin radio-theme-color($color, $foreground) {
Copy link
Member

Choose a reason for hiding this comment

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

This mixin is going to generate a lot of css output; only styles that are directly affected by the palette swap should be defined within.


.mat-radio-outer-circle {
border-color: mat-color($foreground, secondary-text);
}

.mat-radio-disabled {
.mat-radio-ripple .mat-ripple-element, .mat-radio-inner-circle {
background-color: mat-color($foreground, disabled);
.mat-radio-checked .mat-radio-outer-circle {
border-color: mat-color($color);
}

.mat-radio-label-content {
color: mat-color($foreground, disabled);
.mat-radio-disabled .mat-radio-outer-circle {
border-color: mat-color($foreground, disabled);
}

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

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

.mat-radio-disabled {
.mat-radio-ripple .mat-ripple-element, .mat-radio-inner-circle {
background-color: mat-color($foreground, disabled);
}

.mat-radio-label-content {
color: mat-color($foreground, disabled);
}
}
}
}


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

/** 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 @@ -396,6 +406,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 @@ -492,6 +505,22 @@ 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 != '') {
if (isAdd) {
this._renderer.addClass(this._elementRef.nativeElement, `mat-${color}`);
} else {
this._renderer.removeClass(this._elementRef.nativeElement, `mat-${color}`);
}
}
}

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