Skip to content

Commit ddb7d84

Browse files
committed
feat(date-picker,date-range): add calendar as implicit actions context
1 parent 5868239 commit ddb7d84

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ All notable changes for each version of this project will be documented in this
6767
- `increment` and `decrement` methods now accept an optional `delta` parameter which targets the currently spun date portion. It takes precedence over the values set in `spinDelta`.
6868
- `IgxDatePicker`
6969
- **Feature** - `value` accepts ISO 8601 string format.
70+
- **Feature** - The actions template now exposes the Calendar component as implicit context:
71+
```html
72+
<igx-date-picker>
73+
<ng-template igxPickerActions let-calendar>
74+
<button igxButton="flat" (click)="calendar.viewDate = today">Today</button>
75+
</ng-template>
76+
</igx-date-picker>
77+
```
7078
- **Breaking Change** - `value` type could be `Date` or `string`.
7179
- **Breaking Change** - `onSelection` event is renamed to `valueChange`.
7280
- **Breaking Change** - new way to define custom elements in the `igx-date-picker` while the following properties are deleted or deprecated: `formatter`, `context`, `labelInternal`, `template`.
@@ -115,6 +123,14 @@ All notable changes for each version of this project will be documented in this
115123
- **Breaking Change** - `onValidationFailed` event is renamed to `validationFailed`.
116124
- `IgxDateRangePicker`
117125
- **Feature** - `value` start and end accept ISO 8601 string format.
126+
- **Feature** - The actions template now exposes the Calendar component as implicit context:
127+
```html
128+
<igx-date-range-picker>
129+
<ng-template igxPickerActions let-calendar>
130+
<button igxButton="flat" (click)="calendar.viewDate = today">Today</button>
131+
</ng-template>
132+
</igx-date-range-picker>
133+
```
118134
- **Breaking Change** - `value` start and end types could be `Date` or `string`.
119135
- **Breaking Change** - `rangeSelected` event is renamed to `valueChange`.
120136
- **Breaking Change** - `onOpening`, `onOpened`, `onClosing` and `onClosed` events are renamed respectively to `opening`, `opened`, `closing` and `closed`.

projects/igniteui-angular/src/lib/date-common/calendar-container/calendar-container.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@
1212
</ng-template>
1313

1414
<igx-calendar></igx-calendar>
15-
<ng-container *ngTemplateOutlet="this.pickerActions
16-
? this.pickerActions.template
17-
: defaultPickerActions">
15+
<ng-container *ngTemplateOutlet="this.pickerActions?.template || defaultPickerActions; context: { $implicit: this.calendar }">
1816
</ng-container>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { Component, ViewChild } from '@angular/core';
2+
import { ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
4+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
5+
import { IgxCalendarComponent } from '../../calendar/public_api';
6+
import { IgxButtonDirective, IgxButtonModule } from '../../directives/button/button.directive';
7+
import { configureTestSuite } from '../../test-utils/configure-suite';
8+
import { IgxPickerActionsDirective, IgxPickersCommonModule } from '../picker-icons.common';
9+
import { IgxCalendarContainerComponent, IgxCalendarContainerModule } from './calendar-container.component';
10+
11+
12+
describe('Calendar Container', () => {
13+
let fixture: ComponentFixture<IgxDatePickerTestComponent>;
14+
let container: IgxCalendarContainerComponent;
15+
configureTestSuite();
16+
beforeAll(waitForAsync(() => {
17+
TestBed.configureTestingModule({
18+
declarations: [
19+
IgxDatePickerTestComponent
20+
],
21+
imports: [IgxCalendarContainerModule, IgxPickersCommonModule, IgxButtonModule, NoopAnimationsModule]
22+
})
23+
.compileComponents();
24+
}));
25+
26+
beforeEach(fakeAsync(() => {
27+
fixture = TestBed.createComponent(IgxDatePickerTestComponent);
28+
fixture.detectChanges();
29+
container = fixture.componentInstance.container;
30+
}));
31+
32+
it('should render calendar', () => {
33+
fixture = TestBed.createComponent(IgxDatePickerTestComponent);
34+
fixture.detectChanges();
35+
const calendar = fixture.debugElement.query(By.directive(IgxCalendarComponent));
36+
expect(calendar).toBeDefined();
37+
});
38+
39+
it('should render default actions', () => {
40+
spyOn(container.calendarClose, 'emit');
41+
spyOn(container.todaySelection, 'emit');
42+
container.closeButtonLabel = 'cancel';
43+
fixture.detectChanges();
44+
let buttons = fixture.debugElement.queryAll(By.directive(IgxButtonDirective));
45+
expect(buttons).toHaveSize(1);
46+
expect(buttons[0].nativeElement.innerText).toEqual('cancel');
47+
buttons[0].triggerEventHandler('click', {});
48+
expect(container.calendarClose.emit).toHaveBeenCalledTimes(1);
49+
50+
container.todayButtonLabel = 'ok';
51+
fixture.detectChanges();
52+
buttons = fixture.debugElement.queryAll(By.directive(IgxButtonDirective));
53+
expect(buttons).toHaveSize(2);
54+
expect(buttons[1].nativeElement.innerText).toEqual('ok');
55+
buttons[1].triggerEventHandler('click', {});
56+
expect(container.todaySelection.emit).toHaveBeenCalledTimes(1);
57+
});
58+
59+
it('should render default toggle and clear icons', () => {
60+
spyOn(fixture.componentInstance, 'doWork');
61+
container.pickerActions = fixture.componentInstance.actions;
62+
fixture.detectChanges();
63+
64+
const calendar = fixture.debugElement.query(By.directive(IgxCalendarComponent)).componentInstance;
65+
const buttons = fixture.debugElement.queryAll(By.directive(IgxButtonDirective));
66+
expect(buttons).toHaveSize(1);
67+
expect(buttons[0].nativeElement.innerText).toEqual('action');
68+
buttons[0].triggerEventHandler('click', {});
69+
expect(fixture.componentInstance.doWork).toHaveBeenCalledWith(calendar);
70+
});
71+
});
72+
73+
@Component({
74+
template: `
75+
<igx-calendar-container>
76+
</igx-calendar-container>
77+
<ng-template igxPickerActions let-calendar>
78+
<button igxButton (click)="doWork(calendar)">action</button>
79+
</ng-template>
80+
`
81+
})
82+
export class IgxDatePickerTestComponent {
83+
@ViewChild(IgxCalendarContainerComponent) public container: IgxCalendarContainerComponent;
84+
@ViewChild(IgxPickerActionsDirective) public actions: IgxPickerActionsDirective;
85+
public doWork = (_calendar: any) => {};
86+
}

src/app/date-picker/date-picker.sample.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ <h4 class="sample-title">Additional Icons And Custom Buttons</h4>
5252
<igx-suffix (click)="dp5.open()">
5353
<igx-icon>alarm</igx-icon>
5454
</igx-suffix>
55-
<ng-template igxPickerActions>
55+
<ng-template igxPickerActions let-calendar>
5656
<div class="action-buttons">
57-
<button igxButton="flat" (click)="dp5.selectToday()">Today</button>
57+
<button igxButton="flat" (click)="calendar.viewDate = today">Today</button>
58+
<button igxButton="flat" (click)="calendar.viewDate = nextYear">Next Year</button>
59+
<button igxButton="flat" (click)="dp5.selectToday()">Select Today</button>
5860
</div>
5961
</ng-template>
6062
</igx-date-picker>

src/app/date-picker/date-picker.sample.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
.action-buttons {
1515
display: flex;
16-
flex-flow: column;
16+
flex-flow: row;
1717
margin: 10px;
1818
}
1919

src/app/date-picker/date-picker.sample.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export class DatePickerSampleComponent {
1919
public date5 = new Date(new Date(this.date4.getFullYear(), this.date4.getMonth(), this.date4.getDate() + 1));
2020
public date6 = new Date(new Date(this.date5.getFullYear(), this.date5.getMonth(), this.date5.getDate() + 1));
2121
public date7 = new Date(new Date(this.date6.getFullYear(), this.date6.getMonth(), this.date6.getDate() + 1));
22+
public today = new Date(this.date1);
23+
public nextYear = new Date(this.date1.getFullYear() + 1, this.date1.getMonth(), this.date1.getDate());
2224

2325
public disabledDates: DateRangeDescriptor[] = [{ type: DateRangeType.Specific, dateRange: [this.date1, this.date2, this.date3] }];
2426
public specialDates: DateRangeDescriptor[] = [{ type: DateRangeType.Specific, dateRange: [this.date5, this.date6, this.date7] }];

0 commit comments

Comments
 (0)