Skip to content

fix(datepicker): toggle should inherit color from mat-form-field through mat-datepicker #10245

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

Merged
merged 1 commit into from
Mar 6, 2018
Merged
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
6 changes: 3 additions & 3 deletions src/demo-app/datepicker/datepicker-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ <h2>Options</h2>
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-form-field color="accent">
<mat-label>Min date</mat-label>
<input matInput [matDatepicker]="minDatePicker" [(ngModel)]="minDate"
[disabled]="inputDisabled">
<mat-datepicker-toggle matSuffix [for]="minDatePicker"></mat-datepicker-toggle>
<mat-datepicker #minDatePicker [touchUi]="touch" [disabled]="datepickerDisabled"></mat-datepicker>
</mat-form-field>
<mat-form-field>
<mat-form-field color="accent">
<mat-label>Max date</mat-label>
<input matInput [matDatepicker]="maxDatePicker" [(ngModel)]="maxDate"
[disabled]="inputDisabled">
Expand All @@ -30,7 +30,7 @@ <h2>Options</h2>
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-form-field color="accent">
<mat-label>Start at date</mat-label>
<input matInput [matDatepicker]="startAtPicker" [(ngModel)]="startAt"
[disabled]="inputDisabled">
Expand Down
8 changes: 8 additions & 0 deletions src/lib/datepicker/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ As with other types of `<input>`, the datepicker works with `@angular/forms` dir

<!-- example(datepicker-value) -->

### Changing the datepicker colors

The datepicker popup will automatically inherit the color palette (`primary`, `accent`, or `warn`)
from the `mat-form-field` it is attached to. If you would like to specify a different palette for
the popup you can do so by setting the `color` property on `mat-datepicker`.

<!-- example(datepicker-color) -->

### Date validation

There are three properties that add date validation to the datepicker input. The first two are the
Expand Down
15 changes: 10 additions & 5 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
@Input() startView: 'month' | 'year' = 'month';

/** Color palette to use on the datepicker's calendar. */
@Input() color: ThemePalette;
@Input()
get color(): ThemePalette {
return this._color ||
(this._datepickerInput ? this._datepickerInput._getThemePalette() : undefined);
}
set color(value: ThemePalette) {
this._color = value;
}
_color: ThemePalette;

/**
* Whether the calendar UI is in touch mode. In touch mode the calendar opens in a dialog rather
Expand Down Expand Up @@ -464,13 +472,10 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {

/** Passes the current theme color along to the calendar overlay. */
private _setColor(): void {
const input = this._datepickerInput;
const color = this.color || (input ? input._getThemePalette() : undefined);

const color = this.color;
if (this._popupComponentRef) {
this._popupComponentRef.instance.color = color;
}

if (this._dialogRef) {
this._dialogRef.componentInstance.color = color;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** No CSS for this example */
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<mat-form-field color="accent">
<mat-label>Inherited calendar color</mat-label>
<input matInput [matDatepicker]="picker1">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>

<mat-form-field color="accent">
<mat-label>Custom calendar color</mat-label>
<input matInput [matDatepicker]="picker2">
<mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2 color="primary"></mat-datepicker>
</mat-form-field>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Component} from '@angular/core';

/** @title Datepicker palette colors */
@Component({
selector: 'datepicker-color-example',
templateUrl: 'datepicker-color-example.html',
styleUrls: ['datepicker-color-example.css'],
})
export class DatepickerColorExample {}