Skip to content

Commit 5c51301

Browse files
jerome-nelsonjosephperrott
authored andcommitted
feat(radio): add provider for default color input (#15811)
1 parent 2d93c6d commit 5c51301

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

src/material/radio/radio.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ This internal radio button receives focus and is automatically labelled by the t
3434
`<mat-radio-button>` element.
3535

3636
Radio button groups should be given a meaningful label via `aria-label` or `aria-labelledby`.
37+
38+
### Default Color Configuration
39+
The default color for radio buttons can be configured globally using the `MAT_RADIO_DEFAULT_OPTIONS` provider
40+
41+
```
42+
providers: [
43+
provide: MAT_RADIO_DEFAULT_OPTIONS,
44+
useValue: { color: 'accent' },
45+
]
46+
```

src/material/radio/radio.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/f
33
import {Component, DebugElement, ViewChild} from '@angular/core';
44
import {By} from '@angular/platform-browser';
55
import {dispatchFakeEvent} from '@angular/cdk/testing';
6+
7+
import {MAT_RADIO_DEFAULT_OPTIONS} from './radio';
68
import {MatRadioButton, MatRadioChange, MatRadioGroup, MatRadioModule} from './index';
79

810
describe('MatRadio', () => {
@@ -796,6 +798,43 @@ describe('MatRadio', () => {
796798
});
797799
});
798800

801+
describe('MatRadioDefaultOverrides', () => {
802+
describe('when MAT_RADIO_DEFAULT_OPTIONS overridden', () => {
803+
beforeEach(async(() => {
804+
TestBed.configureTestingModule({
805+
imports: [MatRadioModule, FormsModule],
806+
declarations: [DefaultRadioButton, RadioButtonWithColorBinding],
807+
providers: [{
808+
provide: MAT_RADIO_DEFAULT_OPTIONS,
809+
useValue: {color: 'primary'},
810+
}],
811+
});
812+
813+
TestBed.compileComponents();
814+
}));
815+
it('should override default color in Component', () => {
816+
const fixture: ComponentFixture<DefaultRadioButton> =
817+
TestBed.createComponent(DefaultRadioButton);
818+
fixture.detectChanges();
819+
const radioDebugElement: DebugElement =
820+
fixture.debugElement.query(By.directive(MatRadioButton));
821+
expect(
822+
radioDebugElement.nativeElement.classList
823+
).toContain('mat-primary');
824+
});
825+
it('should not override explicit input bindings', () => {
826+
const fixture: ComponentFixture<RadioButtonWithColorBinding> =
827+
TestBed.createComponent(RadioButtonWithColorBinding);
828+
fixture.detectChanges();
829+
const radioDebugElement: DebugElement =
830+
fixture.debugElement.query(By.directive(MatRadioButton));
831+
expect(
832+
radioDebugElement.nativeElement.classList
833+
).not.toContain('mat-primary');
834+
expect(radioDebugElement.nativeElement.classList).toContain('mat-warn');
835+
});
836+
});
837+
});
799838

800839
@Component({
801840
template: `
@@ -937,3 +976,13 @@ class TranscludingWrapper {}
937976
template: `<mat-radio-button tabindex="0"></mat-radio-button>`
938977
})
939978
class RadioButtonWithPredefinedTabindex {}
979+
980+
@Component({
981+
template: `<mat-radio-button></mat-radio-button>`
982+
})
983+
class DefaultRadioButton {}
984+
985+
@Component({
986+
template: `<mat-radio-button color="warn"></mat-radio-button>`
987+
})
988+
class RadioButtonWithColorBinding {}

src/material/radio/radio.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
EventEmitter,
2222
forwardRef,
2323
Inject,
24+
InjectionToken,
2425
Input,
2526
OnDestroy,
2627
OnInit,
@@ -43,6 +44,22 @@ import {
4344
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
4445

4546

47+
export interface MatRadioDefaultOptions {
48+
color: ThemePalette;
49+
}
50+
51+
export const MAT_RADIO_DEFAULT_OPTIONS =
52+
new InjectionToken<MatRadioDefaultOptions>('mat-radio-default-options', {
53+
providedIn: 'root',
54+
factory: MAT_RADIO_DEFAULT_OPTIONS_FACTORY
55+
});
56+
57+
export function MAT_RADIO_DEFAULT_OPTIONS_FACTORY(): MatRadioDefaultOptions {
58+
return {
59+
color: 'accent'
60+
};
61+
}
62+
4663
// Increasing integer for generating unique ids for radio components.
4764
let nextUniqueId = 0;
4865

@@ -433,7 +450,9 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
433450
/** Theme color of the radio button. */
434451
@Input()
435452
get color(): ThemePalette {
436-
return this._color || (this.radioGroup && this.radioGroup.color) || 'accent';
453+
return this._color ||
454+
(this.radioGroup && this.radioGroup.color) ||
455+
this._providerOverride && this._providerOverride.color || 'accent';
437456
}
438457
set color(newValue: ThemePalette) { this._color = newValue; }
439458
private _color: ThemePalette;
@@ -474,7 +493,9 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
474493
private _changeDetector: ChangeDetectorRef,
475494
private _focusMonitor: FocusMonitor,
476495
private _radioDispatcher: UniqueSelectionDispatcher,
477-
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
496+
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
497+
@Optional() @Inject(MAT_RADIO_DEFAULT_OPTIONS)
498+
private _providerOverride?: MatRadioDefaultOptions) {
478499
super(elementRef);
479500

480501
// Assertions. Ideally these should be stripped out by the compiler.

tools/public_api_guard/material/radio.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export declare const MAT_RADIO_DEFAULT_OPTIONS: InjectionToken<MatRadioDefaultOptions>;
2+
3+
export declare function MAT_RADIO_DEFAULT_OPTIONS_FACTORY(): MatRadioDefaultOptions;
4+
15
export declare const MAT_RADIO_GROUP_CONTROL_VALUE_ACCESSOR: any;
26

37
export declare class MatRadioButton extends _MatRadioButtonMixinBase implements OnInit, AfterViewInit, OnDestroy, CanDisableRipple, HasTabIndex {
@@ -17,7 +21,7 @@ export declare class MatRadioButton extends _MatRadioButtonMixinBase implements
1721
radioGroup: MatRadioGroup;
1822
required: boolean;
1923
value: any;
20-
constructor(radioGroup: MatRadioGroup, elementRef: ElementRef, _changeDetector: ChangeDetectorRef, _focusMonitor: FocusMonitor, _radioDispatcher: UniqueSelectionDispatcher, _animationMode?: string | undefined);
24+
constructor(radioGroup: MatRadioGroup, elementRef: ElementRef, _changeDetector: ChangeDetectorRef, _focusMonitor: FocusMonitor, _radioDispatcher: UniqueSelectionDispatcher, _animationMode?: string | undefined, _providerOverride?: MatRadioDefaultOptions | undefined);
2125
_isRippleDisabled(): boolean;
2226
_markForCheck(): void;
2327
_onInputChange(event: Event): void;
@@ -36,6 +40,10 @@ export declare class MatRadioChange {
3640
value: any);
3741
}
3842

43+
export interface MatRadioDefaultOptions {
44+
color: ThemePalette;
45+
}
46+
3947
export declare class MatRadioGroup implements AfterContentInit, ControlValueAccessor {
4048
_controlValueAccessorChangeFn: (value: any) => void;
4149
_radios: QueryList<MatRadioButton>;

0 commit comments

Comments
 (0)