Skip to content

Commit

Permalink
feat(module:date-picker): add week month year range (#5832)
Browse files Browse the repository at this point in the history
Close #5742
  • Loading branch information
wenqi73 authored Sep 27, 2020
1 parent 8d8bf36 commit 0725d88
Show file tree
Hide file tree
Showing 43 changed files with 592 additions and 606 deletions.
54 changes: 8 additions & 46 deletions components/core/time/candy-date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,6 @@ describe('candy-date coverage supplements', () => {
expect(date.isSameSecond(new CandyDate('2019-5-5 12:12:12'))).toBeFalsy();
});

it('support isAfter', () => {
expect(date.isAfterYear(null)).toBeFalsy();

expect(date.isAfterYear(new CandyDate('2000'))).toBeTruthy();

expect(date.isAfterMonth(new CandyDate('2000-5-5 12:12:12'))).toBeTruthy();
expect(date.isAfterMonth(new CandyDate('2018-4-5 12:12:12'))).toBeTruthy();

expect(date.isAfterDay(new CandyDate('2018-5-4 11:12:12'))).toBeTruthy();

expect(date.isAfterHour(new CandyDate('2000-5-5 12:12:12'))).toBeTruthy();
expect(date.isAfterHour(new CandyDate('2018-4-5 12:12:12'))).toBeTruthy();
expect(date.isAfterHour(new CandyDate('2018-5-4 12:12:12'))).toBeTruthy();
expect(date.isAfterHour(new CandyDate('2018-5-5 11:12:12'))).toBeTruthy();

expect(date.isAfterMinute(new CandyDate('2000-5-5 12:12:12'))).toBeTruthy();
expect(date.isAfterMinute(new CandyDate('2018-4-5 12:12:12'))).toBeTruthy();
expect(date.isAfterMinute(new CandyDate('2018-5-4 12:12:12'))).toBeTruthy();
expect(date.isAfterMinute(new CandyDate('2018-5-5 11:12:12'))).toBeTruthy();
expect(date.isAfterMinute(new CandyDate('2018-5-5 12:11:12'))).toBeTruthy();

expect(date.isAfterSecond(new CandyDate('2000-5-5 12:12:12'))).toBeTruthy();
expect(date.isAfterSecond(new CandyDate('2018-4-5 12:12:12'))).toBeTruthy();
expect(date.isAfterSecond(new CandyDate('2018-5-4 12:12:12'))).toBeTruthy();
expect(date.isAfterSecond(new CandyDate('2018-5-5 11:12:12'))).toBeTruthy();
expect(date.isAfterSecond(new CandyDate('2018-5-5 12:11:12'))).toBeTruthy();
expect(date.isAfterSecond(new CandyDate('2018-5-5 12:12:11'))).toBeTruthy();
});

it('support isBefore', () => {
expect(date.isBeforeYear(null)).toBeFalsy();

Expand All @@ -60,23 +31,6 @@ describe('candy-date coverage supplements', () => {
expect(date.isBeforeMonth(new CandyDate('2018-6-5 12:12:12'))).toBeTruthy();

expect(date.isBeforeDay(new CandyDate('2018-6-5 12:12:12'))).toBeTruthy();

expect(date.isBeforeHour(new CandyDate('2100-5-5 12:12:12'))).toBeTruthy();
expect(date.isBeforeHour(new CandyDate('2018-6-5 12:12:12'))).toBeTruthy();
expect(date.isBeforeHour(new CandyDate('2018-5-6 12:12:12'))).toBeTruthy();
expect(date.isBeforeHour(new CandyDate('2018-5-5 13:12:12'))).toBeTruthy();

expect(date.isBeforeMinute(new CandyDate('2100-5-5 12:12:12'))).toBeTruthy();
expect(date.isBeforeMinute(new CandyDate('2018-6-5 12:12:12'))).toBeTruthy();
expect(date.isBeforeMinute(new CandyDate('2018-5-6 12:12:12'))).toBeTruthy();
expect(date.isBeforeMinute(new CandyDate('2018-5-5 13:12:12'))).toBeTruthy();
expect(date.isBeforeMinute(new CandyDate('2018-5-5 12:13:12'))).toBeTruthy();

expect(date.isBeforeSecond(new CandyDate('2100-5-5 12:12:12'))).toBeTruthy();
expect(date.isBeforeSecond(new CandyDate('2018-6-5 12:12:12'))).toBeTruthy();
expect(date.isBeforeSecond(new CandyDate('2018-5-6 12:12:12'))).toBeTruthy();
expect(date.isBeforeSecond(new CandyDate('2018-5-5 13:12:12'))).toBeTruthy();
expect(date.isBeforeSecond(new CandyDate('2018-5-5 12:12:13'))).toBeTruthy();
});

it('should throw error while putting invalid date input', () => {
Expand Down Expand Up @@ -107,5 +61,13 @@ describe('candy-date coverage supplements', () => {
result = normalizeRangeValue([new CandyDate(), new CandyDate()], true);
expect(result[0]!.getMonth()).toEqual(now.getMonth());
expect(result[1]!.getMonth()).toEqual(now.getMonth());

result = normalizeRangeValue([new CandyDate(), new CandyDate()], false, 'year');
expect(result[0]!.getYear()).toEqual(now.getFullYear());
expect(result[1]!.getYear()).toEqual(now.getFullYear() + 1);

result = normalizeRangeValue([new CandyDate(), new CandyDate()], true, 'year');
expect(result[0]!.getYear()).toEqual(now.getFullYear());
expect(result[1]!.getYear()).toEqual(now.getFullYear());
});
}); // /candy-date coverage supplements
94 changes: 34 additions & 60 deletions components/core/time/candy-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ import startOfWeek from 'date-fns/startOfWeek';
import { warn } from 'ng-zorro-antd/core/logger';
import { IndexableObject, NzSafeAny } from 'ng-zorro-antd/core/types';

type CandyDateCompareGrain = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';

export type CandyDateMode = 'decade' | 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
export type NormalizedMode = 'decade' | 'year' | 'month';
export type WeekDayIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6;
export type CandyDateType = CandyDate | Date | null;
export type SingleValue = CandyDate | null;
export type CompatibleValue = SingleValue | SingleValue[];

export function wrongSortOrder(rangeValue: SingleValue[]): boolean {
const [start, end] = rangeValue;
return !!start && !!end && start.isAfterSecond(end);
return !!start && !!end && end.isBeforeDay(start);
}

export function normalizeRangeValue(value: SingleValue[], allowSameMonth: boolean): CandyDate[] {
export function normalizeRangeValue(value: SingleValue[], allowSameInTwoPanel: boolean, type: NormalizedMode = 'month'): CandyDate[] {
const [start, end] = value;
let newStart: CandyDate = start || new CandyDate();
let newEnd: CandyDate = end || new CandyDate();
if (start && !end) {
newStart = start;
newEnd = start.addMonths(1);
newEnd = start.add(1, type);
} else if (!start && end) {
newStart = end.addMonths(-1);
newStart = end.add(-1, type);
newEnd = end;
}
if (newEnd.isSameMonth(newStart) && !allowSameMonth) {
newEnd = newStart.addMonths(1);
if (newEnd.isSame(newStart, type) && !allowSameInTwoPanel) {
newEnd = newStart.add(1, type);
}
return [newStart, newEnd];
}
Expand Down Expand Up @@ -91,15 +91,6 @@ export class CandyDate implements IndexableObject {
}
}

// getLocale(): string {
// return this.locale;
// }

// setLocale(locale: string): CandyDate {
// this.locale = locale;
// return this;
// }

calendarStart(options?: { weekStartsOn: WeekDayIndex | undefined }): CandyDate {
return new CandyDate(startOfWeek(startOfMonth(this.nativeDate), options));
}
Expand Down Expand Up @@ -188,9 +179,29 @@ export class CandyDate implements IndexableObject {
return this.setDate(this.getDate() + amount);
}

isSame(date: CandyDateType, grain: CandyDateCompareGrain = 'day'): boolean {
add(amount: number, mode: NormalizedMode): CandyDate {
switch (mode) {
case 'decade':
return this.addYears(amount * 10);
break;
case 'year':
return this.addYears(amount);
break;
case 'month':
return this.addMonths(amount);
break;
default:
return this.addMonths(amount);
break;
}
}

isSame(date: CandyDateType, grain: CandyDateMode = 'day'): boolean {
let fn;
switch (grain) {
case 'decade':
fn = (pre: Date, next: Date) => Math.abs(pre.getFullYear() - next.getFullYear()) < 11;
break;
case 'year':
fn = isSameYear;
break;
Expand Down Expand Up @@ -240,7 +251,7 @@ export class CandyDate implements IndexableObject {
return this.isSame(date, 'second');
}

compare(date: CandyDateType, grain: CandyDateCompareGrain = 'day', isBefore: boolean = true): boolean {
isBefore(date: CandyDateType, grain: CandyDateMode = 'day'): boolean {
if (date === null) {
return false;
}
Expand Down Expand Up @@ -268,56 +279,19 @@ export class CandyDate implements IndexableObject {
fn = differenceInCalendarDays;
break;
}
return isBefore ? fn(this.nativeDate, this.toNativeDate(date)) < 0 : fn(this.nativeDate, this.toNativeDate(date)) > 0;
return fn(this.nativeDate, this.toNativeDate(date)) < 0;
}

isBeforeYear(date: CandyDateType): boolean {
return this.compare(date, 'year');
return this.isBefore(date, 'year');
}

isBeforeMonth(date: CandyDateType): boolean {
return this.compare(date, 'month');
return this.isBefore(date, 'month');
}

isBeforeDay(date: CandyDateType): boolean {
return this.compare(date, 'day');
}

isBeforeHour(date: CandyDateType): boolean {
return this.compare(date, 'hour');
}

isBeforeMinute(date: CandyDateType): boolean {
return this.compare(date, 'minute');
}

isBeforeSecond(date: CandyDateType): boolean {
return this.compare(date, 'second');
}

// TODO: isBefore
isAfterYear(date: CandyDateType): boolean {
return this.compare(date, 'year', false);
}

isAfterMonth(date: CandyDateType): boolean {
return this.compare(date, 'month', false);
}

isAfterDay(date: CandyDateType): boolean {
return this.compare(date, 'day', false);
}

isAfterHour(date: CandyDateType): boolean {
return this.compare(date, 'hour', false);
}

isAfterMinute(date: CandyDateType): boolean {
return this.compare(date, 'minute', false);
}

isAfterSecond(date: CandyDateType): boolean {
return this.compare(date, 'second', false);
return this.isBefore(date, 'day');
}

// Equal to today accurate to "day"
Expand Down
5 changes: 2 additions & 3 deletions components/date-picker/date-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ describe('NzDatePickerComponent', () => {
expect(getPickerInput(fixture.debugElement).placeholder).toBe('Select date');

openPickerByClickTrigger();
expect(getPickerInput(fixture.debugElement).placeholder).toBe('Select date');
expect(queryFromOverlay(`.${PREFIX_CLASS}-content th`).textContent).toContain('Su');
}));

Expand Down Expand Up @@ -501,7 +500,7 @@ describe('NzDatePickerComponent', () => {
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('2019') > -1).toBeTruthy();
// Click to choose a year to change panel
dispatchMouseEvent(queryFromOverlay('td.ant-picker-cell-selected'), 'click');
dispatchMouseEvent(queryFromOverlay('td.ant-picker-cell'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
Expand Down Expand Up @@ -1045,7 +1044,7 @@ class NzTestDatePickerComponent {
nzDisabledTime: any; // tslint:disable-line:no-any
nzRenderExtraFooter!: string | (() => TemplateRef<void> | string);
nzShowToday = false;
nzMode!: string;
nzMode: string = 'date';
nzSuffixIcon!: string;

// nzRanges;
Expand Down
Loading

0 comments on commit 0725d88

Please sign in to comment.