Skip to content
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

feat(module:calendar): sync some changes from ant-design (#2769) #2963

Merged
merged 1 commit into from
Feb 26, 2019
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
7 changes: 6 additions & 1 deletion components/calendar/demo/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-calendar-basic',
template: `
<nz-calendar [(ngModel)]="date" [(nzMode)]="mode"></nz-calendar>
<nz-calendar [(ngModel)]="date" [(nzMode)]="mode" (nzPanelChange)="panelChange($event)"></nz-calendar>
`
})
export class NzDemoCalendarBasicComponent {
date = new Date(2012, 11, 21);
mode = 'month';

panelChange(change: {date: Date, mode: string}): void {
console.log(change.date, change.mode);
}

}
13 changes: 12 additions & 1 deletion components/calendar/demo/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import { Component } from '@angular/core';
selector: 'nz-demo-calendar-select',
template: `
<nz-alert nzMessage="Your selected date: {{ selectedValue | date:'yyyy-MM-dd' }}"></nz-alert>
<nz-calendar [(ngModel)]="selectedValue"></nz-calendar>
<nz-calendar [(ngModel)]="selectedValue"
(selectChange)="selectChange($event)"
(nzValueChange)="valueChange($event)"
></nz-calendar>
`
})
export class NzDemoCalendarSelectComponent {
selectedValue = new Date('2017-01-25');

selectChange(select: Date): void {
console.log(`Select value: ${select}`);
}

valueChange(value: Date): void {
console.log(`Current value: ${value}`);
}
}
8 changes: 7 additions & 1 deletion components/calendar/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ registerLocaleData(en);
<nz-calendar
[nzDateCell]="dateCellTpl"
[(ngModel)]="selectedDate"
[(nzMode)]="mode">
[(nzMode)]="mode"
(nzPanelChange)="panelChange($event)"
(nzSelectChange)="selectChange($event)"
(nzValueChange)="valueChange($event)">
<!-- Another method for cell definition -->
<div *nzDateCell>Foo</div>
</nz-calendar>
Expand All @@ -46,3 +49,6 @@ registerLocaleData(en);
| `[nzDateFullCell]` | (Contentable) Customize the display of the date cell, the template content will override the cell | `TemplateRef<Date>` | - |
| `[nzMonthCell]` | (Contentable) Customize the display of the month cell, the template content will be appended to the cell | `TemplateRef<Date>` | - |
| `[nzMonthFullCell]` | (Contentable) Customize the display of the month cell, the template content will override the cell | `TemplateRef<Date>` | - |
| `(nzPanelChange)` | Callback for when panel changes | `EventEmitter<{ date: Date, mode: 'month'|'year' }>` | - |
| `(nzSelectChange)` | Callback for when a date is selected | `EventEmitter<Date>` | - |
| `(nzValueChange)` | Callback for when date changes | `EventEmitter<Date>` | - |
8 changes: 7 additions & 1 deletion components/calendar/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ registerLocaleData(zh);
<nz-calendar
[nzDateCell]="dateCellTpl"
[(ngModel)]="selectedDate"
[(nzMode)]="mode">
[(nzMode)]="mode"
(nzPanelChange)="panelChange($event)"
(nzSelectChange)="selectChange($event)"
(nzValueChange)="valueChange($event)">
<!-- 定义 Cell 的另一种方式 -->
<div *dateCell>Foo</div>
</nz-calendar>
Expand All @@ -47,3 +50,6 @@ registerLocaleData(zh);
| `[nzDateFullCell]` | (可作为内容)自定义渲染日期单元格,模版内容覆盖单元格 | `TemplateRef<Date>` | - |
| `[nzMonthCell]` | (可作为内容)自定义渲染月单元格,模版内容会被追加到单元格 | `TemplateRef<Date>` | - |
| `[nzMonthFullCell]` | (可作为内容)自定义渲染月单元格,模版内容覆盖单元格 | `TemplateRef<Date>` | - |
| `(nzPanelChange)` | 面板变化的回调 | `EventEmitter<{ date: Date, mode: 'month'|'year' }>` | - |
| `(nzSelectChange)` | 选择日期的回调 | `EventEmitter<Date>` | - |
| `(nzValueChange)` | 日期变化的回调 | `EventEmitter<Date>` | - |
15 changes: 12 additions & 3 deletions components/calendar/nz-calendar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { DateHelperService } from '../i18n/date-helper.service';
import { NzI18nService } from '../i18n/nz-i18n.service';
import { NzDateCellDirective as DateCell, NzDateFullCellDirective as DateFullCell, NzMonthCellDirective as MonthCell, NzMonthFullCellDirective as MonthFullCell } from './nz-calendar-cells';

export type ModeType = 'month' | 'year';

@Component({
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
Expand All @@ -30,8 +32,11 @@ import { NzDateCellDirective as DateCell, NzDateFullCellDirective as DateFullCel
]
})
export class NzCalendarComponent implements ControlValueAccessor, OnInit {
@Input() nzMode: 'month'|'year' = 'month';
@Output() readonly nzModeChange: EventEmitter<'month'|'year'> = new EventEmitter();
@Input() nzMode: ModeType = 'month';
@Output() readonly nzModeChange: EventEmitter<ModeType> = new EventEmitter();
@Output() readonly nzPanelChange: EventEmitter<{date: Date, mode: ModeType}> = new EventEmitter();

@Output() readonly nzSelectChange: EventEmitter<Date> = new EventEmitter();

@Input() set nzValue(value: Date) { this.updateDate(value, false); }
@Output() readonly nzValueChange: EventEmitter<Date> = new EventEmitter();
Expand Down Expand Up @@ -108,22 +113,26 @@ export class NzCalendarComponent implements ControlValueAccessor, OnInit {
this.calculateActiveMonth();
}

onModeChange(mode: 'month'|'year'): void {
onModeChange(mode: ModeType): void {
this.nzModeChange.emit(mode);
this.nzPanelChange.emit({'date': this.activeDate, 'mode': mode});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

组合 nzModeChange 和 nzValueChange 就可以得到 nzPanelChange 的内容,为什么还要再增加 nzPanelChange 这个 output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了和ant-design同步,这个issue不是这个意思?

}

onDateSelect(date: Date): void {
this.updateDate(date);
this.nzSelectChange.emit(date);
}

onYearSelect(year: number): void {
const date = setYear(this.activeDate, year);
this.updateDate(date);
this.nzSelectChange.emit(date);
}

onMonthSelect(month: number): void {
const date = setMonth(this.activeDate, month);
this.updateDate(date);
this.nzSelectChange.emit(date);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nzSelectChange 和 nzValueChange 的区别是什么

}

writeValue(value: Date|null): void {
Expand Down
60 changes: 59 additions & 1 deletion components/calendar/nz-calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe('Calendar', () => {
NzTestCalendarDateCellComponent,
NzTestCalendarDateFullCellComponent,
NzTestCalendarMonthCellComponent,
NzTestCalendarMonthFullCellComponent
NzTestCalendarMonthFullCellComponent,
NzTestCalendarChangesComponent
],
providers: [ { provide: NZ_DATE_CONFIG, useValue: { firstDayOfWeek: 0 } } ]
}).compileComponents();
Expand Down Expand Up @@ -379,6 +380,46 @@ describe('Calendar', () => {
expect(content.nativeElement.textContent.trim()).toBe('Bar');
});
});

describe('changes', () => {
let fixture: ComponentFixture<NzTestCalendarChangesComponent>;
let component: NzTestCalendarChangesComponent;

beforeEach(async(() => {
fixture = TestBed.createComponent(NzTestCalendarChangesComponent);
component = fixture.componentInstance;
}));

it('should panelChange work', fakeAsync(() => {
fixture.detectChanges();

expect(component.panelChange).toHaveBeenCalledTimes(0);

const calendar = fixture.debugElement.queryAll(By.directive(Calendar))[0].injector.get(Calendar);
calendar.onModeChange('year');
fixture.detectChanges();

expect(component.panelChange).toHaveBeenCalledTimes(1);
}));

it('should selectChange work', () => {
fixture.detectChanges();

expect(component.panelChange).toHaveBeenCalledTimes(0);

const calendar = fixture.debugElement.queryAll(By.directive(Calendar))[0].injector.get(Calendar);
calendar.onYearSelect(2019);
fixture.detectChanges();

expect(component.selectChange).toHaveBeenCalledTimes(1);

calendar.onMonthSelect(2);
fixture.detectChanges();

expect(component.selectChange).toHaveBeenCalledTimes(2);
});
});

});

@Component({
Expand Down Expand Up @@ -461,3 +502,20 @@ class NzTestCalendarMonthCellComponent { }
`
})
class NzTestCalendarMonthFullCellComponent { }

@Component({
template: `
<nz-calendar
[(nzMode)]="mode"
[(ngModel)]="date0"
(nzPanelChange)="panelChange($event)"
(nzSelectChange)="selectChange($event)">
</nz-calendar>
`
})
class NzTestCalendarChangesComponent {
mode: 'month'|'year' = 'month';
date0 = new Date(2014, 3, 14);
panelChange = jasmine.createSpy('panelChange callback');
selectChange = jasmine.createSpy('selectChange callback');
}