Skip to content

Commit

Permalink
fix(module:date-picker): start and end month displays unreasonable
Browse files Browse the repository at this point in the history
fix(module:date-picker,timepicker): trackBy function

close NG-ZORRO#6308
close NG-ZORRO#6142
close NG-ZORRO#5992
  • Loading branch information
wenqi73 committed Jan 13, 2021
1 parent 4711194 commit 86f6731
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 75 deletions.
16 changes: 14 additions & 2 deletions components/core/time/candy-date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,25 @@ describe('candy-date coverage supplements', () => {
expect(result[0]!.getMonth()).toEqual(8);
expect(result[1]!.getMonth()).toEqual(9);

result = normalizeRangeValue([randomDay, null], true);
expect(result[0]!.getMonth()).toEqual(8);
expect(result[1]!.getMonth()).toEqual(8);

result = normalizeRangeValue([null, null], false);
expect(result[0]!.getMonth()).toEqual(now.getMonth());
expect(differenceInCalendarMonths(result[1]!.nativeDate, now)).toEqual(1);

result = normalizeRangeValue([new CandyDate(), new CandyDate()], false);
result = normalizeRangeValue([null, null], true);
expect(result[0]!.getMonth()).toEqual(now.getMonth());
expect(differenceInCalendarMonths(result[1]!.nativeDate, now)).toEqual(1);
expect(result[1]!.getMonth()).toEqual(now.getMonth());

result = normalizeRangeValue([randomDay, new CandyDate()], false, 'month', 'left');
expect(result[0]!.getMonth()).toEqual(randomDay.getMonth());
expect(differenceInCalendarMonths(result[1]!.nativeDate, randomDay.nativeDate)).toEqual(1);

result = normalizeRangeValue([randomDay, new CandyDate()], false, 'month', 'right');
expect(result[1]!.getMonth()).toEqual(now.getMonth());
expect(differenceInCalendarMonths(result[0]!.nativeDate, now)).toEqual(-1);

result = normalizeRangeValue([new CandyDate(), new CandyDate()], true);
expect(result[0]!.getMonth()).toEqual(now.getMonth());
Expand Down
23 changes: 16 additions & 7 deletions components/core/time/candy-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,28 @@ export function wrongSortOrder(rangeValue: SingleValue[]): boolean {
return !!start && !!end && end.isBeforeDay(start);
}

export function normalizeRangeValue(value: SingleValue[], allowSameInTwoPanel: boolean, type: NormalizedMode = 'month'): CandyDate[] {
export function normalizeRangeValue(
value: SingleValue[],
hasTimePicker: boolean,
type: NormalizedMode = 'month',
activePart: 'left' | 'right' = 'left'
): CandyDate[] {
const [start, end] = value;
let newStart: CandyDate = start || new CandyDate();
let newEnd: CandyDate = end || new CandyDate();
let newEnd: CandyDate = end || (hasTimePicker ? newStart : newStart.add(1, type));

if (start && !end) {
newStart = start;
newEnd = start.add(1, type);
newEnd = hasTimePicker ? start : start.add(1, type);
} else if (!start && end) {
newStart = end.add(-1, type);
newStart = hasTimePicker ? end : end.add(-1, type);
newEnd = end;
}
if (newEnd.isSame(newStart, type) && !allowSameInTwoPanel) {
newEnd = newStart.add(1, type);
} else if (start && end && !hasTimePicker) {
if (activePart === 'left') {
newEnd = newStart.add(1, type);
} else {
newStart = newEnd.add(-1, type);
}
}
return [newStart, newEnd];
}
Expand Down
9 changes: 5 additions & 4 deletions components/date-picker/calendar-footer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,23 @@ export class CalendarFooterComponent implements OnChanges {
isNonEmptyString = isNonEmptyString;
isTodayDisabled: boolean = false;
todayTitle: string = '';
private now: CandyDate = new CandyDate();

constructor(private dateHelper: DateHelperService) {}

ngOnChanges(changes: SimpleChanges): void {
const now: Date = new Date();
if (changes.disabledDate) {
this.isTodayDisabled = !!(this.disabledDate && this.disabledDate(this.now.nativeDate));
this.isTodayDisabled = !!(this.disabledDate && this.disabledDate(now));
}
if (changes.locale) {
// NOTE: Compat for DatePipe formatting rules
const dateFormat: string = transCompatFormat(this.locale.dateFormat);
this.todayTitle = this.dateHelper.format(this.now.nativeDate, dateFormat);
this.todayTitle = this.dateHelper.format(now, dateFormat);
}
}

onClickToday(): void {
this.clickToday.emit(this.now.clone()); // To prevent the "now" being modified from outside, we use clone
const now: CandyDate = new CandyDate();
this.clickToday.emit(now.clone()); // To prevent the "now" being modified from outside, we use clone
}
}
10 changes: 5 additions & 5 deletions components/date-picker/date-picker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export class DatePickerService implements OnDestroy {

initValue(): void {
if (this.isRange) {
this.setActiveDate([]);
this.value = this.initialValue = [];
this.initialValue = [];
} else {
this.value = this.initialValue = null;
this.initialValue = null;
}
this.setValue(this.initialValue);
}

hasValue(value: CompatibleValue = this.value): boolean {
Expand All @@ -46,14 +46,14 @@ export class DatePickerService implements OnDestroy {
}
}

setActiveDate(value: CompatibleValue, allowSameInTwoPanel: boolean = false, mode: NormalizedMode = 'month'): void {
setActiveDate(value: CompatibleValue, hasTimePicker: boolean = false, mode: NormalizedMode = 'month'): void {
const parentPanels: { [key in NzDateMode]?: NormalizedMode } = {
date: 'month',
month: 'year',
year: 'decade'
};
if (this.isRange) {
this.activeDate = normalizeRangeValue(value as CandyDate[], allowSameInTwoPanel, parentPanels[mode]);
this.activeDate = normalizeRangeValue(value as CandyDate[], hasTimePicker, parentPanels[mode], this.activeInput);
} else {
this.activeDate = cloneDate(value);
}
Expand Down
55 changes: 25 additions & 30 deletions components/date-picker/date-range-popup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import { getTimeConfig, isAllowedDate, PREFIX_CLASS } from './util';
<div class="{{ prefixCls }}-range-arrow" [style.left.px]="datePickerService?.arrowLeft"></div>
<div class="{{ prefixCls }}-panel-container">
<div class="{{ prefixCls }}-panels">
<ng-container *ngTemplateOutlet="tplRangePart; context: { partType: 'left' }"></ng-container>
<ng-container *ngTemplateOutlet="tplRangePart; context: { partType: 'right' }"></ng-container>
<ng-container *ngTemplateOutlet="tplInnerPopup; context: { partType: 'left' }"></ng-container>
<ng-container *ngTemplateOutlet="tplInnerPopup; context: { partType: 'right' }"></ng-container>
</div>
<ng-container *ngTemplateOutlet="tplFooter"></ng-container>
</div>
Expand All @@ -71,28 +71,29 @@ import { getTimeConfig, isAllowedDate, PREFIX_CLASS } from './util';
</ng-template>
<ng-template #tplInnerPopup let-partType="partType">
<!-- TODO(@wenqi73) [selectedValue] [hoverValue] types-->
<inner-popup
*ngIf="show(partType)"
[showWeek]="showWeek"
[endPanelMode]="getPanelMode(endPanelMode, partType)"
[partType]="partType"
[locale]="locale!"
[showTimePicker]="hasTimePicker"
[timeOptions]="getTimeOptions(partType)"
[panelMode]="getPanelMode(panelMode, partType)"
(panelModeChange)="onPanelModeChange($event, partType)"
[activeDate]="getActiveDate(partType)"
[value]="getValue(partType)"
[disabledDate]="disabledDate"
[dateRender]="dateRender"
[selectedValue]="$any(datePickerService?.value)"
[hoverValue]="$any(hoverValue)"
(cellHover)="onCellHover($event)"
(selectDate)="changeValueFromSelect($event, !showTime)"
(selectTime)="onSelectTime($event, partType)"
(headerChange)="onActiveDateChange($event, partType)"
></inner-popup>
<div class="{{ prefixCls }}-panel" [class.ant-picker-panel-rtl]="dir === 'rtl'" [style.display]="show(partType) ? 'block' : 'none'">
<!-- TODO(@wenqi73) [selectedValue] [hoverValue] types-->
<inner-popup
[showWeek]="showWeek"
[endPanelMode]="getPanelMode(endPanelMode, partType)"
[partType]="partType"
[locale]="locale!"
[showTimePicker]="hasTimePicker"
[timeOptions]="getTimeOptions(partType)"
[panelMode]="getPanelMode(panelMode, partType)"
(panelModeChange)="onPanelModeChange($event, partType)"
[activeDate]="getActiveDate(partType)"
[value]="getValue(partType)"
[disabledDate]="disabledDate"
[dateRender]="dateRender"
[selectedValue]="$any(datePickerService?.value)"
[hoverValue]="$any(hoverValue)"
(cellHover)="onCellHover($event)"
(selectDate)="changeValueFromSelect($event, !showTime)"
(selectTime)="onSelectTime($event, partType)"
(headerChange)="onActiveDateChange($event, partType)"
></inner-popup>
</div>
</ng-template>
<ng-template #tplFooter>
Expand All @@ -111,12 +112,6 @@ import { getTimeConfig, isAllowedDate, PREFIX_CLASS } from './util';
></calendar-footer>
</ng-template>
<ng-template #tplRangePart let-partType="partType">
<div class="{{ prefixCls }}-panel" [class.ant-picker-panel-rtl]="dir === 'rtl'">
<ng-container *ngTemplateOutlet="tplInnerPopup; context: { partType: partType }"></ng-container>
</div>
</ng-template>
<!-- Range ONLY: Range Quick Selector -->
<ng-template #tplRangeQuickSelector>
<li
Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { NzDatePickerModule } from 'ng-zorro-antd/date-picker';

## API

**注意:**nz-date-picker 的部分 locale 来自于 Angular 自身的[国际化支持](https://angular.io/guide/i18n),需要在 `app.module.ts` 文件中 引入相应的 Angular 语言包。
**注意:** nz-date-picker 的部分 locale 来自于 Angular 自身的[国际化支持](https://angular.io/guide/i18n),需要在 `app.module.ts` 文件中 引入相应的 Angular 语言包。
例如:
```typescript
import { registerLocaleData } from '@angular/common';
Expand Down
4 changes: 2 additions & 2 deletions components/date-picker/lib/date-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class DateTableComponent extends AbstractTable implements OnChanges, OnIn
const row: DateBodyRow = {
isActive: false,
dateCells: [],
trackByIndex: `${weekStart.getYear()}`
trackByIndex: week
};

for (let day = 0; day < 7; day++) {
Expand All @@ -78,7 +78,7 @@ export class DateTableComponent extends AbstractTable implements OnChanges, OnIn
const title = this.dateHelper.format(date.nativeDate, dateFormat);
const label = this.dateHelper.format(date.nativeDate, 'dd');
const cell: DateCell = {
trackByIndex: title,
trackByIndex: day,
value: date.nativeDate,
label: label,
isSelected: false,
Expand Down
4 changes: 2 additions & 2 deletions components/date-picker/lib/decade-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class DecadeTableComponent extends AbstractTable implements OnChanges {
for (let rowIndex = 0; rowIndex < MAX_ROW; rowIndex++) {
const row: DateBodyRow = {
dateCells: [],
trackByIndex: previousYear
trackByIndex: rowIndex
};

for (let colIndex = 0; colIndex < MAX_COL; colIndex++) {
Expand All @@ -51,7 +51,7 @@ export class DecadeTableComponent extends AbstractTable implements OnChanges {
const content = `${start}-${end}`;

const cell: DecadeCell = {
trackByIndex: content,
trackByIndex: colIndex,
value: this.activeDate.setYear(start).nativeDate,
content,
title: content,
Expand Down
4 changes: 2 additions & 2 deletions components/date-picker/lib/month-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export class MonthTableComponent extends AbstractTable implements OnChanges, OnI
for (let rowIndex = 0; rowIndex < this.MAX_ROW; rowIndex++) {
const row: DateBodyRow = {
dateCells: [],
trackByIndex: this.activeDate.getYear()
trackByIndex: rowIndex
};

for (let colIndex = 0; colIndex < this.MAX_COL; colIndex++) {
const month = this.activeDate.setMonth(monthValue);
const isDisabled = this.isDisabledMonth(month);
const content = this.dateHelper.format(month.nativeDate, 'MMM');
const cell: DateCell = {
trackByIndex: content,
trackByIndex: colIndex,
value: month.nativeDate,
isDisabled,
isSelected: month.isSameMonth(this.value),
Expand Down
4 changes: 2 additions & 2 deletions components/date-picker/lib/year-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export class YearTableComponent extends AbstractTable {
for (let rowIndex = 0; rowIndex < this.MAX_ROW; rowIndex++) {
const row: DateBodyRow = {
dateCells: [],
trackByIndex: startYear
trackByIndex: rowIndex
};
for (let colIndex = 0; colIndex < this.MAX_COL; colIndex++) {
const yearNum = previousYear + yearValue;
const year = this.activeDate.setYear(yearNum);
const content = this.dateHelper.format(year.nativeDate, 'yyyy');
const isDisabled = this.isDisabledYear(year);
const cell: YearCell = {
trackByIndex: content,
trackByIndex: colIndex,
value: year.nativeDate,
isDisabled,
isSameDecade: yearNum >= startYear && yearNum <= endYear,
Expand Down
1 change: 1 addition & 0 deletions components/date-picker/picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
}
this.focus();
this.updateInputWidthAndArrowLeft();
this.panel?.updateActiveDate();
});
}

Expand Down
Loading

0 comments on commit 86f6731

Please sign in to comment.