Skip to content

Commit 333b11e

Browse files
matheboxkara
authored andcommitted
feat(checkbox): add color attribute. (#1463)
1 parent 35f0044 commit 333b11e

File tree

7 files changed

+110
-9
lines changed

7 files changed

+110
-9
lines changed

src/demo-app/checkbox/checkbox-demo.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ <h1>md-checkbox: Basic Example</h1>
22
<form>
33
<md-checkbox [(ngModel)]="isChecked"
44
name="cb"
5+
[color]="checkboxColor()"
56
(change)="isIndeterminate = false"
67
[indeterminate]="isIndeterminate"
78
[disabled]="isDisabled"
@@ -18,6 +19,8 @@ <h1>md-checkbox: Basic Example</h1>
1819
<label for="indeterminate-toggle">Toggle Indeterminate</label>
1920
<input id="disabled-toggle" type="checkbox" [(ngModel)]="isDisabled">
2021
<label for="disabled-toggle">Toggle Disabled</label>
22+
<input id="color-toggle" type="checkbox" [(ngModel)]="useAlternativeColor">
23+
<label for="color-toggle">Toggle Color</label>
2124
</div>
2225
<div>
2326
<p>Alignment:</p>
@@ -41,4 +44,4 @@ <h1>md-checkbox: Basic Example</h1>
4144
</div>
4245

4346
<h1>Nested Checklist</h1>
44-
<md-checkbox-demo-nested-checklist></md-checkbox-demo-nested-checklist>
47+
<md-checkbox-demo-nested-checklist></md-checkbox-demo-nested-checklist>

src/demo-app/checkbox/checkbox-demo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ export class CheckboxDemo {
6767
isChecked: boolean = false;
6868
isDisabled: boolean = false;
6969
alignment: string = 'start';
70+
useAlternativeColor: boolean = false;
7071

7172
printResult() {
7273
if (this.isIndeterminate) {
7374
return 'Maybe!';
7475
}
7576
return this.isChecked ? 'Yes!' : 'No!';
7677
}
78+
79+
checkboxColor() {
80+
return this.useAlternativeColor ? 'primary' : 'accent';
81+
}
7782
}

src/lib/checkbox/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,17 @@ checkbox if you do not wish to have any label text.
7777
```html
7878
<md-checkbox [checked]="false" aria-label="My standalone checkbox"></md-checkbox>
7979
```
80+
81+
### Theming
82+
83+
The color of a `md-checkbox` can be changed by using the `color` attribute.
84+
The value `accent` is default and will correspond to your theme accent color.
85+
Alternatively, you can specify `primary` or `warn`.
86+
87+
Example:
88+
89+
```html
90+
<md-checkbox [checked]="true" color="primary">
91+
I come after my label.
92+
</md-checkbox>
93+
```

src/lib/checkbox/_checkbox-theme.scss

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
$is-dark-theme: map-get($theme, is-dark);
66
$primary: map-get($theme, primary);
77
$accent: map-get($theme, accent);
8+
$warn: map-get($theme, warn);
89
$background: map-get($theme, background);
910

1011

@@ -14,9 +15,6 @@
1415
// The color of the checkbox's checkmark / mixedmark.
1516
$checkbox-mark-color: md-color($background, background);
1617

17-
// The color that is used as the checkbox background when it is checked.
18-
$checkbox-background-color: md-color($accent, 500);
19-
2018
// NOTE(traviskaufman): While the spec calls for translucent blacks/whites for disabled colors,
2119
// this does not work well with elements layered on top of one another. To get around this we
2220
// blend the colors together based on the base color and the theme background.
@@ -43,8 +41,16 @@
4341
}
4442

4543
.md-checkbox-indeterminate, .md-checkbox-checked {
46-
.md-checkbox-background {
47-
background-color: $checkbox-background-color;
44+
&.md-primary .md-checkbox-background {
45+
background-color: md-color($primary, 500);
46+
}
47+
48+
&.md-accent .md-checkbox-background {
49+
background-color: md-color($accent, 500);
50+
}
51+
52+
&.md-warn .md-checkbox-background {
53+
background-color: md-color($warn, 500);
4854
}
4955
}
5056

@@ -63,7 +69,17 @@
6369
}
6470

6571
// TODO(jelbourn): remove style for temporary ripple once the real ripple is applied.
66-
.md-checkbox-focused .md-ink-ripple {
67-
background-color: md-color($accent, 0.26);
72+
.md-checkbox-focused {
73+
&.md-primary .md-ink-ripple {
74+
background-color: md-color($primary, 0.26);
75+
}
76+
77+
&.md-accent .md-ink-ripple {
78+
background-color: md-color($accent, 0.26);
79+
}
80+
81+
&.md-warn .md-ink-ripple {
82+
background-color: md-color($warn, 0.26);
83+
}
6884
}
6985
}

src/lib/checkbox/checkbox.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@import '../core/theming/theming';
2+
@import '../core/style/elevation';
23
@import '../core/style/variables';
34
@import '../core/ripple/ripple';
45

@@ -189,6 +190,10 @@ $_md-checkbox-indeterminate-checked-easing-function: cubic-bezier(0.14, 0, 0, 1)
189190

190191
md-checkbox {
191192
cursor: pointer;
193+
194+
// Animation
195+
transition: background $swift-ease-out-duration $swift-ease-out-timing-function,
196+
md-elevation-transition-property-value();
192197
}
193198

194199
.md-checkbox-layout {

src/lib/checkbox/checkbox.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,36 @@ describe('MdCheckbox', () => {
267267
expect(inputElement.required).toBe(false);
268268
});
269269

270+
describe('color behaviour', () => {
271+
it('should apply class based on color attribute', () => {
272+
testComponent.checkboxColor = 'primary';
273+
fixture.detectChanges();
274+
expect(checkboxDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
275+
276+
testComponent.checkboxColor = 'accent';
277+
fixture.detectChanges();
278+
expect(checkboxDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
279+
});
280+
281+
it('should should not clear previous defined classes', () => {
282+
checkboxDebugElement.nativeElement.classList.add('custom-class');
283+
284+
testComponent.checkboxColor = 'primary';
285+
fixture.detectChanges();
286+
287+
expect(checkboxDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
288+
expect(checkboxDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
289+
290+
testComponent.checkboxColor = 'accent';
291+
fixture.detectChanges();
292+
293+
expect(checkboxDebugElement.nativeElement.classList.contains('md-primary')).toBe(false);
294+
expect(checkboxDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
295+
expect(checkboxDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
296+
297+
});
298+
});
299+
270300
describe('state transition css classes', () => {
271301
it('should transition unchecked -> checked -> unchecked', () => {
272302
testComponent.isChecked = true;
@@ -519,6 +549,7 @@ describe('MdCheckbox', () => {
519549
[checked]="isChecked"
520550
[indeterminate]="isIndeterminate"
521551
[disabled]="isDisabled"
552+
[color]="checkboxColor"
522553
(change)="changeCount = changeCount + 1"
523554
(click)="onCheckboxClick($event)"
524555
(change)="onCheckboxChange($event)">
@@ -536,6 +567,7 @@ class SingleCheckbox {
536567
parentElementKeyedUp: boolean = false;
537568
lastKeydownEvent: Event = null;
538569
changeCount: number = 0;
570+
checkboxColor: string = 'primary';
539571

540572
onCheckboxClick(event: Event) {}
541573
onCheckboxChange(event: MdCheckboxChange) {}

src/lib/checkbox/checkbox.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ export class MdCheckbox implements ControlValueAccessor {
128128

129129
private _indeterminate: boolean = false;
130130

131+
private _color: string;
132+
131133
private _controlValueAccessorChangeFn: (value: any) => void = (value) => {};
132134

133135
hasFocus: boolean = false;
134136

135-
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
137+
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {
138+
this.color = 'accent';
139+
}
136140

137141
/**
138142
* Whether the checkbox is checked. Note that setting `checked` will immediately set
@@ -174,6 +178,28 @@ export class MdCheckbox implements ControlValueAccessor {
174178
}
175179
}
176180

181+
/** Sets the color of the checkbox */
182+
@Input()
183+
get color(): string {
184+
return this._color;
185+
}
186+
187+
set color(value: string) {
188+
this._updateColor(value);
189+
}
190+
191+
_updateColor(newColor: string) {
192+
this._setElementColor(this._color, false);
193+
this._setElementColor(newColor, true);
194+
this._color = newColor;
195+
}
196+
197+
_setElementColor(color: string, isAdd: boolean) {
198+
if (color != null && color != '') {
199+
this._renderer.setElementClass(this._elementRef.nativeElement, `md-${color}`, isAdd);
200+
}
201+
}
202+
177203
/**
178204
* Implemented as part of ControlValueAccessor.
179205
* TODO: internal

0 commit comments

Comments
 (0)