diff --git a/components/calendar/calendar.component.ts b/components/calendar/calendar.component.ts index 9a3e851d694..2e7ca035615 100644 --- a/components/calendar/calendar.component.ts +++ b/components/calendar/calendar.component.ts @@ -65,6 +65,7 @@ type NzCalendarDateTemplate = TemplateRef<{ $implicit: Date }>; [activeDate]="activeDate" [cellRender]="dateCell" [fullCellRender]="dateFullCell" + [nzMonthScope]="nzMonthScope" (valueChange)="onDateSelect($event)" > @@ -98,6 +99,7 @@ export class NzCalendarComponent implements ControlValueAccessor, OnChanges { @Input() nzMode: NzCalendarMode = 'month'; @Input() nzValue: Date; + @Input() nzMonthScope = false; @Output() readonly nzModeChange: EventEmitter = new EventEmitter(); @Output() readonly nzPanelChange: EventEmitter<{ date: Date; mode: NzCalendarMode }> = new EventEmitter(); diff --git a/components/calendar/calendar.spec.ts b/components/calendar/calendar.spec.ts index d158a40eaad..390b538d8f9 100644 --- a/components/calendar/calendar.spec.ts +++ b/components/calendar/calendar.spec.ts @@ -402,6 +402,26 @@ describe('Calendar', () => { expect(component.selectChange).toHaveBeenCalledTimes(2); }); }); + + describe('monthScope', () => { + let fixture: ComponentFixture; + + beforeEach(async(() => { + fixture = TestBed.createComponent(NzTestCalendarMonthCellComponent); + })); + + it('should only show date of active month', () => { + fixture.detectChanges(); + + const host = fixture.debugElement.queryAll(By.directive(Calendar))[2]; + const tds = host.queryAll(By.css('td')); + expect(tds.length).toEqual(30); + const firstTd = tds[0].query(By.css('.ant-picker-calendar-date-value')); + expect(firstTd.nativeElement.textContent).toEqual('1'); + const lastTd = tds[tds.length - 1].query(By.css('.ant-picker-calendar-date-value')); + expect(lastTd.nativeElement.textContent).toEqual('30'); + }); + }); }); @Component({ @@ -469,9 +489,12 @@ class NzTestCalendarDateFullCellComponent {} Bar + ` }) -class NzTestCalendarMonthCellComponent {} +class NzTestCalendarMonthCellComponent { + date = new Date(2020, 9, 17); +} @Component({ template: ` diff --git a/components/calendar/doc/index.en-US.md b/components/calendar/doc/index.en-US.md index 3a78716dbc2..df57d9fbd4e 100644 --- a/components/calendar/doc/index.en-US.md +++ b/components/calendar/doc/index.en-US.md @@ -52,4 +52,5 @@ registerLocaleData(en); | `[nzDateFullCell]` | (Contentable) Customize the display of the date cell, the template content will override the cell | `TemplateRef` | - | | `[nzMonthCell]` | (Contentable) Customize the display of the month cell, the template content will be appended to the cell | `TemplateRef` | - | | `[nzMonthFullCell]` | (Contentable) Customize the display of the month cell, the template content will override the cell | `TemplateRef` | - | +| `[nzMonthScope]` | Only display date of the active month | `boolean` | `false` | | `(nzPanelChange)` | Callback for when panel changes | `EventEmitter<{ date: Date, mode: 'month' \| 'year' }>` | - | diff --git a/components/calendar/doc/index.zh-CN.md b/components/calendar/doc/index.zh-CN.md index 13f16f14b37..9b7a1671a00 100644 --- a/components/calendar/doc/index.zh-CN.md +++ b/components/calendar/doc/index.zh-CN.md @@ -52,5 +52,6 @@ registerLocaleData(zh); | `[nzDateFullCell]` | (可作为内容)自定义渲染日期单元格,模版内容覆盖单元格 | `TemplateRef` | - | | `[nzMonthCell]` | (可作为内容)自定义渲染月单元格,模版内容会被追加到单元格 | `TemplateRef` | - | | `[nzMonthFullCell]` | (可作为内容)自定义渲染月单元格,模版内容覆盖单元格 | `TemplateRef` | - | +| `[nzMonthScope]` | 是否只显示当前月份的日期 | `boolean` | `false` | | `(nzPanelChange)` | 面板变化的回调 | `EventEmitter<{ date: Date, mode: 'month' \| 'year' }>` | - | | `(nzSelectChange)` | 选择日期的回调 | `EventEmitter` | - | diff --git a/components/date-picker/lib/date-table.component.ts b/components/date-picker/lib/date-table.component.ts index ee2268cc4a3..2e1c9be2c9a 100644 --- a/components/date-picker/lib/date-table.component.ts +++ b/components/date-picker/lib/date-table.component.ts @@ -38,6 +38,7 @@ export class DateTableComponent extends AbstractTable implements OnChanges, OnIn @Input() locale: NzCalendarI18nInterface; @Input() selectedValue: CandyDate[]; // Range ONLY @Input() hoverValue: CandyDate[]; // Range ONLY + @Input() nzMonthScope = false; @Output() readonly dayHover = new EventEmitter(); // Emitted when hover on a day by mouse enter @@ -225,6 +226,20 @@ export class DateTableComponent extends AbstractTable implements OnChanges, OnIn weekRows.push(row); } + if (this.nzMonthScope) { + const allDateCell: DateCell[] = []; + weekRows.forEach(row => { + const rowCells = row.dateCells.filter(cell => cell.value.getMonth() === this.activeDate.getMonth()); + allDateCell.push(...rowCells); + }); + weekRows.forEach(v => { + v.dateCells = allDateCell.splice(0, 7); + }); + // delete empty row + if (weekRows[weekRows.length - 1].dateCells.length === 0) { + weekRows.pop(); + } + } return weekRows; }