Skip to content

Commit

Permalink
fix(module:date-picker): modify date-fns week-year format (#5753)
Browse files Browse the repository at this point in the history
close #5327
  • Loading branch information
wenqi73 committed Sep 10, 2020
1 parent 35a5023 commit 4911e36
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions components/date-picker/date-picker.component.spec.ts
Expand Up @@ -384,6 +384,7 @@ describe('NzDatePickerComponent', () => {
}));

it('should support nzDefaultPickerValue', fakeAsync(() => {
fixture.detectChanges();
fixtureInstance.nzDefaultPickerValue = new Date('2015-09-17');
fixture.detectChanges();
flush();
Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/date-picker.component.ts
Expand Up @@ -226,7 +226,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Cont
// Default format when it's empty
if (!this.nzFormat) {
if (this.showWeek) {
this.nzFormat = 'yyyy-ww'; // Format for week
this.nzFormat = this.i18n.getDateLocale() ? 'RRRR-II' : 'yyyy-ww'; // Format for week
} else {
this.nzFormat = this.nzShowTime ? 'yyyy-MM-dd HH:mm:ss' : 'yyyy-MM-dd';
}
Expand Down
6 changes: 4 additions & 2 deletions components/date-picker/date-picker.service.ts
Expand Up @@ -56,8 +56,10 @@ export class DatePickerService implements OnDestroy {
}

setValue(value: CompatibleValue): void {
this.value = value;
this.valueChange$.next(this.value);
if (value !== this.value) {
this.value = value;
this.valueChange$.next(this.value);
}
}

getActiveIndex(part: RangePartType = this.activeInput): number {
Expand Down
3 changes: 3 additions & 0 deletions components/date-picker/date-range-popup.component.ts
Expand Up @@ -181,6 +181,9 @@ export class DateRangePopupComponent implements OnInit, OnChanges, OnDestroy {
if (changes.panelMode) {
this.endPanelMode = this.panelMode;
}
if (changes.defaultPickerValue) {
this.initActiveDate();
}
}

ngOnDestroy(): void {
Expand Down
13 changes: 6 additions & 7 deletions components/date-picker/picker.component.ts
Expand Up @@ -189,7 +189,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
destroy$ = new Subject();
prefixCls = PREFIX_CLASS;
// Index signature in type 'string | string[]' only permits reading
inputValue: NzSafeAny;
inputValue: NzSafeAny = '';
activeBarStyle: object = { position: 'absolute' };
animationOpenState = false;
overlayOpen: boolean = false; // Available when "open"=undefined
Expand Down Expand Up @@ -238,22 +238,20 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
constructor(
private elementRef: ElementRef,
private dateHelper: DateHelperService,
private changeDetector: ChangeDetectorRef,
private cdr: ChangeDetectorRef,
private platform: Platform,
public datePickerService: DatePickerService,
@Inject(DOCUMENT) doc: NzSafeAny
) {
this.document = doc;
this.origin = new CdkOverlayOrigin(this.elementRef);
this.updateInputValue();
}

ngOnInit(): void {
this.inputSize = Math.max(10, this.format.length) + 2;

this.datePickerService.valueChange$.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.updateInputValue();
this.changeDetector.markForCheck();
});
}

Expand Down Expand Up @@ -285,7 +283,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
this.focus();
}
this.panel?.cdr.markForCheck();
this.changeDetector.markForCheck();
this.cdr.markForCheck();
});
}

Expand Down Expand Up @@ -386,7 +384,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
onPositionChange(position: ConnectedOverlayPositionChange): void {
this.currentPositionX = position.connectionPair.originX;
this.currentPositionY = position.connectionPair.originY;
this.changeDetector.detectChanges(); // Take side-effects to position styles
this.cdr.detectChanges(); // Take side-effects to position styles
}

onClickClear(event: MouseEvent): void {
Expand All @@ -404,6 +402,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
} else {
this.inputValue = this.formatValue(newValue as CandyDate);
}
this.cdr.markForCheck();
}

formatValue(value: CandyDate): string {
Expand Down Expand Up @@ -461,7 +460,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
animationDone(): void {
if (!this.realOpenState) {
this.animationOpenState = false;
this.changeDetector.markForCheck();
this.cdr.markForCheck();
}
}
}
4 changes: 3 additions & 1 deletion components/date-picker/week-picker.component.spec.ts
Expand Up @@ -83,7 +83,9 @@ describe('NzWeekPickerComponent', () => {
});

@Component({
template: ` <nz-week-picker [nzFormat]="nzFormat" [ngModel]="nzValue"></nz-week-picker> `
template: `
<nz-week-picker [nzFormat]="nzFormat" [ngModel]="nzValue"></nz-week-picker>
`
})
export class NzTestWeekPickerComponent {
nzFormat?: string;
Expand Down
4 changes: 2 additions & 2 deletions components/i18n/date-helper.service.spec.ts
Expand Up @@ -30,7 +30,7 @@ describe('DateHelperService', () => {
xit('should do formatting correctly', () => {
const date = new Date('2018-12-31 12:11:10');
expect(dateHelper.format(date, 'yyyy-MM-dd')).toBe('2018-12-31');
expect(dateHelper.format(date, 'yyyy-ww')).toBe('2018-53');
expect(dateHelper.format(date, 'ww')).toBe('01');
});

it('should get first day of week with 0 by en_US', () => {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('DateHelperService', () => {
it('should do formatting correctly', () => {
const date = new Date('2018-12-31 12:11:10');
expect(dateHelper.format(date, 'yyyy-MM-dd')).toBe('2018-12-31');
expect(dateHelper.format(date, 'II')).toBe('01'); // ISO week
expect(dateHelper.format(date, 'RRRR-II')).toBe('2019-01'); // ISO week
});

it('should do parseTime correctly', () => {
Expand Down

0 comments on commit 4911e36

Please sign in to comment.