Skip to content

Commit 6b70ca6

Browse files
crisbetoandrewseguin
authored andcommitted
fix(select): theme not being transferred to the panel (#7342)
* Fixes the theme from the form field not being transferred to the panel, causing the options to have the wrong color. This seems to have happened during the switch to the form field, but interestingly AoT didn't catch us not having a `color` anymore (possible because it's in a binding expression). * Moves a test to another test suite since it doesn't belong with the theming tests.
1 parent 5056110 commit 6b70ca6

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/lib/select/select.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
(detach)="close()">
3535

3636
<div
37-
class="mat-select-panel {{ 'mat-' + color }}"
37+
class="mat-select-panel {{ _getPanelTheme() }}"
3838
[ngClass]="panelClass"
3939
[@transformPanel]="multiple ? 'showing-multiple' : 'showing'"
4040
(@transformPanel.done)="_onPanelDone()"

src/lib/select/select.spec.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,19 @@ describe('MatSelect', () => {
22752275

22762276
expect(() => fixture.componentInstance.select.triggerValue).not.toThrow();
22772277
});
2278+
2279+
it('should allow the user to customize the label', () => {
2280+
const fixture = TestBed.createComponent(SelectWithCustomTrigger);
2281+
fixture.detectChanges();
2282+
2283+
fixture.componentInstance.control.setValue('pizza-1');
2284+
fixture.detectChanges();
2285+
2286+
const label = fixture.debugElement.query(By.css('.mat-select-value')).nativeElement;
2287+
2288+
expect(label.textContent).toContain('azziP',
2289+
'Expected the displayed text to be "Pizza" in reverse.');
2290+
});
22782291
});
22792292

22802293
describe('change event', () => {
@@ -2622,30 +2635,21 @@ describe('MatSelect', () => {
26222635

26232636
describe('theming', () => {
26242637
let fixture: ComponentFixture<BasicSelectWithTheming>;
2625-
let testInstance: BasicSelectWithTheming;
2626-
let selectElement: HTMLElement;
26272638

26282639
beforeEach(async(() => {
26292640
fixture = TestBed.createComponent(BasicSelectWithTheming);
2630-
testInstance = fixture.componentInstance;
26312641
fixture.detectChanges();
2632-
2633-
selectElement = fixture.debugElement.query(By.css('.mat-select')).nativeElement;
26342642
}));
26352643

2636-
it('should allow the user to customize the label', () => {
2637-
fixture.destroy();
2638-
2639-
const labelFixture = TestBed.createComponent(SelectWithCustomTrigger);
2640-
labelFixture.detectChanges();
2641-
2642-
labelFixture.componentInstance.control.setValue('pizza-1');
2643-
labelFixture.detectChanges();
2644+
it('should transfer the theme to the select panel', () => {
2645+
fixture.componentInstance.theme = 'warn';
2646+
fixture.detectChanges();
26442647

2645-
const label = labelFixture.debugElement.query(By.css('.mat-select-value')).nativeElement;
2648+
fixture.componentInstance.select.open();
2649+
fixture.detectChanges();
26462650

2647-
expect(label.textContent).toContain('azziP',
2648-
'Expected the displayed text to be "Pizza" in reverse.');
2651+
const panel = overlayContainerElement.querySelector('.mat-select-panel')! as HTMLElement;
2652+
expect(panel.classList).toContain('mat-warn');
26492653
});
26502654
});
26512655

src/lib/select/select.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
mixinDisabled,
5656
mixinTabIndex,
5757
} from '@angular/material/core';
58-
import {MatFormFieldControl} from '@angular/material/form-field';
58+
import {MatFormField, MatFormFieldControl} from '@angular/material/form-field';
5959
import {Observable} from 'rxjs/Observable';
6060
import {merge} from 'rxjs/observable/merge';
6161
import {Subject} from 'rxjs/Subject';
@@ -410,6 +410,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
410410
@Optional() private _dir: Directionality,
411411
@Optional() private _parentForm: NgForm,
412412
@Optional() private _parentFormGroup: FormGroupDirective,
413+
@Optional() private _parentFormField: MatFormField,
413414
@Self() @Optional() public ngControl: NgControl,
414415
@Attribute('tabindex') tabIndex: string,
415416
@Inject(MAT_SELECT_SCROLL_STRATEGY) private _scrollStrategyFactory) {
@@ -641,6 +642,11 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
641642
this._setScrollTop();
642643
}
643644

645+
/** Returns the theme to be used on the panel. */
646+
_getPanelTheme(): string {
647+
return this._parentFormField ? `mat-${this._parentFormField.color}` : '';
648+
}
649+
644650
/** Whether the select has a value. */
645651
get empty(): boolean {
646652
return !this._selectionModel || this._selectionModel.isEmpty();

0 commit comments

Comments
 (0)