Skip to content

Commit b5b762a

Browse files
mmalerbatinayuangao
authored andcommitted
fix(datepicker): make datepicker work in compatibility mode (#4686)
* fix(datepicker): make datepicker work in compatibility mode * add test * address comments
1 parent 210e57c commit b5b762a

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

src/lib/datepicker/calendar.html

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
<div class="mat-calendar-header">
22
<div class="mat-calendar-controls">
3-
<button md-button class="mat-calendar-period-button" (click)="_currentPeriodClicked()"
4-
[attr.aria-label]="_periodButtonLabel">
3+
<!--
4+
TODO(mmalerba): Clean up duplicated compatibility mode code when we have a better way to do
5+
this.
6+
-->
7+
8+
<!-- Check for compatibility mode and use correct prefix for md-button. -->
9+
<button *ngIf="!_isCompatibilityMode" md-button class="mat-calendar-period-button"
10+
(click)="_currentPeriodClicked()" [attr.aria-label]="_periodButtonLabel">
511
{{_periodButtonText}}
612
<div class="mat-calendar-arrow" [class.mat-calendar-invert]="!_monthView"></div>
713
</button>
14+
<button *ngIf="_isCompatibilityMode" mat-button class="mat-calendar-period-button"
15+
(click)="_currentPeriodClicked()" [attr.aria-label]="_periodButtonLabel">
16+
{{_periodButtonText}}
17+
<div class="mat-calendar-arrow" [class.mat-calendar-invert]="!_monthView"></div>
18+
</button>
19+
820
<div class="mat-calendar-spacer"></div>
9-
<button md-icon-button class="mat-calendar-previous-button" [disabled]="!_previousEnabled()"
10-
(click)="_previousClicked()" [attr.aria-label]="_prevButtonLabel">
21+
22+
<!-- Check for compatibility mode and use correct prefix for md-icon-button. -->
23+
<button *ngIf="!_isCompatibilityMode" md-icon-button class="mat-calendar-previous-button"
24+
[disabled]="!_previousEnabled()" (click)="_previousClicked()"
25+
[attr.aria-label]="_prevButtonLabel">
26+
</button>
27+
<button *ngIf="_isCompatibilityMode" mat-icon-button class="mat-calendar-previous-button"
28+
[disabled]="!_previousEnabled()" (click)="_previousClicked()"
29+
[attr.aria-label]="_prevButtonLabel">
30+
</button>
31+
32+
<!-- Check for compatibility mode and use correct prefix for md-icon-button. -->
33+
<button *ngIf="!_isCompatibilityMode" md-icon-button class="mat-calendar-next-button"
34+
[disabled]="!_nextEnabled()" (click)="_nextClicked()"
35+
[attr.aria-label]="_nextButtonLabel">
1136
</button>
12-
<button md-icon-button class="mat-calendar-next-button" [disabled]="!_nextEnabled()"
13-
(click)="_nextClicked()" [attr.aria-label]="_nextButtonLabel">
37+
<button *ngIf="_isCompatibilityMode" mat-icon-button class="mat-calendar-next-button"
38+
[disabled]="!_nextEnabled()" (click)="_nextClicked()"
39+
[attr.aria-label]="_nextButtonLabel">
1440
</button>
1541
</div>
1642
</div>

src/lib/datepicker/calendar.spec.ts

+40-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
} from '../core/keyboard/keycodes';
2424
import {MdDatepickerIntl} from './datepicker-intl';
2525
import {MdNativeDateModule} from '../core/datetime/index';
26+
import {NoConflictStyleCompatibilityMode} from '../core';
27+
import {MdButtonModule} from '../button/index';
2628

2729

2830
// When constructing a Date, the month is zero-based. This can be confusing, since people are
@@ -35,6 +37,7 @@ describe('MdCalendar', () => {
3537
beforeEach(async(() => {
3638
TestBed.configureTestingModule({
3739
imports: [
40+
MdButtonModule,
3841
MdNativeDateModule,
3942
],
4043
declarations: [
@@ -444,18 +447,13 @@ describe('MdCalendar', () => {
444447
let fixture: ComponentFixture<CalendarWithMinMax>;
445448
let testComponent: CalendarWithMinMax;
446449
let calendarElement: HTMLElement;
447-
let prevButton: HTMLButtonElement;
448-
let nextButton: HTMLButtonElement;
449450
let calendarInstance: MdCalendar<Date>;
450451

451452
beforeEach(() => {
452453
fixture = TestBed.createComponent(CalendarWithMinMax);
453454

454455
let calendarDebugElement = fixture.debugElement.query(By.directive(MdCalendar));
455456
calendarElement = calendarDebugElement.nativeElement;
456-
prevButton =
457-
calendarElement.querySelector('.mat-calendar-previous-button') as HTMLButtonElement;
458-
nextButton = calendarElement.querySelector('.mat-calendar-next-button') as HTMLButtonElement;
459457
calendarInstance = calendarDebugElement.componentInstance;
460458
testComponent = fixture.componentInstance;
461459
});
@@ -478,6 +476,9 @@ describe('MdCalendar', () => {
478476
testComponent.startAt = new Date(2016, FEB, 1);
479477
fixture.detectChanges();
480478

479+
let prevButton =
480+
calendarElement.querySelector('.mat-calendar-previous-button') as HTMLButtonElement;
481+
481482
expect(prevButton.disabled).toBe(false, 'previous button should not be disabled');
482483
expect(calendarInstance._activeDate).toEqual(new Date(2016, FEB, 1));
483484

@@ -497,6 +498,9 @@ describe('MdCalendar', () => {
497498
testComponent.startAt = new Date(2017, DEC, 1);
498499
fixture.detectChanges();
499500

501+
let nextButton =
502+
calendarElement.querySelector('.mat-calendar-next-button') as HTMLButtonElement;
503+
500504
expect(nextButton.disabled).toBe(false, 'next button should not be disabled');
501505
expect(calendarInstance._activeDate).toEqual(new Date(2017, DEC, 1));
502506

@@ -584,6 +588,37 @@ describe('MdCalendar', () => {
584588
});
585589
});
586590

591+
describe('MdCalendar in compatibility mode', () => {
592+
beforeEach(async(() => {
593+
TestBed.configureTestingModule({
594+
imports: [
595+
MdButtonModule,
596+
MdNativeDateModule,
597+
NoConflictStyleCompatibilityMode,
598+
],
599+
declarations: [
600+
MdCalendar,
601+
MdCalendarBody,
602+
MdMonthView,
603+
MdYearView,
604+
605+
// Test components.
606+
StandardCalendar,
607+
],
608+
providers: [
609+
MdDatepickerIntl,
610+
],
611+
});
612+
613+
TestBed.compileComponents();
614+
}));
615+
616+
it('should not throw on creation', () => {
617+
let fixture = TestBed.createComponent(StandardCalendar);
618+
expect(() => fixture.detectChanges()).not.toThrow();
619+
});
620+
});
621+
587622

588623
@Component({
589624
template: `<md-calendar [startAt]="startDate" [(selected)]="selected"></md-calendar>`

src/lib/datepicker/calendar.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {DateAdapter} from '../core/datetime/index';
2626
import {MdDatepickerIntl} from './datepicker-intl';
2727
import {createMissingDateImplError} from './datepicker-errors';
2828
import {MD_DATE_FORMATS, MdDateFormats} from '../core/datetime/date-formats';
29+
import {MATERIAL_COMPATIBILITY_MODE} from '../core';
2930

3031

3132
/**
@@ -111,6 +112,7 @@ export class MdCalendar<D> implements AfterContentInit {
111112
constructor(private _elementRef: ElementRef,
112113
private _intl: MdDatepickerIntl,
113114
private _ngZone: NgZone,
115+
@Optional() @Inject(MATERIAL_COMPATIBILITY_MODE) public _isCompatibilityMode: boolean,
114116
@Optional() private _dateAdapter: DateAdapter<D>,
115117
@Optional() @Inject(MD_DATE_FORMATS) private _dateFormats: MdDateFormats) {
116118
if (!this._dateAdapter) {

0 commit comments

Comments
 (0)