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

fix(module:time-picker): allow inputting string type #4949

Merged
merged 3 commits into from
Apr 1, 2020
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
10 changes: 10 additions & 0 deletions components/i18n/date-helper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ describe('DateHelperService', () => {
it('should get first day of week with 0 by en_US', () => {
expect(dateHelper.getFirstDayOfWeek()).toBe(0);
});

it('should do parseTime correctly', () => {
expect(dateHelper.parseTime('14:00')?.toTimeString().substr(0, 8)).toBe('14:00:00');
expect(dateHelper.parseTime('4:00')?.toTimeString().substr(0, 8)).toBe('04:00:00');
});
});

describe('Formatting with Data-fns', () => {
Expand All @@ -56,6 +61,11 @@ describe('DateHelperService', () => {
expect(dateHelper.format(date, 'yyyy-MM-dd')).toBe('2018-12-31');
expect(dateHelper.format(date, 'II')).toBe('01'); // ISO week
});

it('should do parseTime correctly', () => {
expect(dateHelper.parseTime('14:00', 'HH:mm')?.toTimeString().substr(0, 8)).toBe('14:00:00');
expect(dateHelper.parseTime('4:00', 'H:mm')?.toTimeString().substr(0, 8)).toBe('04:00:00');
});
});

describe('Custom firstDayOfWeek', () => {
Expand Down
20 changes: 12 additions & 8 deletions components/i18n/date-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import fnsFormat from 'date-fns/format';
import fnsGetISOWeek from 'date-fns/getISOWeek';
import fnsParse from 'date-fns/parse';

import parseISO from 'date-fns/parseISO';
import { WeekDayIndex } from 'ng-zorro-antd/core/time';
import { convertTokens } from './convert-tokens';
import { mergeDateConfig, NZ_DATE_CONFIG, NZ_DATE_FNS_COMPATIBLE, NzDateConfig } from './date-config';
Expand Down Expand Up @@ -47,13 +46,7 @@ export abstract class DateHelperService {
abstract getFirstDayOfWeek(): WeekDayIndex;
abstract format(date: Date, formatStr: string): string;
abstract parseDate(text: string, formatStr?: string): Date;

parseTime(text: string): Date | undefined {
if (!text) {
return;
}
return parseISO(`1970-01-01 ${text}`);
}
abstract parseTime(text: string, formatStr?: string): Date | undefined;
}

/**
Expand Down Expand Up @@ -94,6 +87,10 @@ export class DateHelperByDateFns extends DateHelperService {
weekStartsOn: this.getFirstDayOfWeek()
});
}

parseTime(text: string, formatStr: string): Date | undefined {
return this.parseDate(text, formatStr);
}
}

/**
Expand Down Expand Up @@ -122,4 +119,11 @@ export class DateHelperByDatePipe extends DateHelperService {
parseDate(text: string): Date {
return new Date(text);
}

parseTime(text: string): Date | undefined {
if (!text) {
return;
}
return new Date(Date.parse(`1970-01-01 ${text}`));
}
}
3 changes: 2 additions & 1 deletion components/time-picker/time-picker-panel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ export class NzTimePickerPanelComponent implements ControlValueAccessor, OnInit,
scrollToSelected(instance: HTMLElement, index: number, duration: number = 0, unit: NzTimePickerUnit): void {
const transIndex = this.translateIndex(index, unit);
const currentOption = (instance.children[transIndex] || instance.children[0]) as HTMLElement;
this.scrollTo(instance, currentOption.offsetTop, duration);
const parentPaddingTop = 4;
this.scrollTo(instance, currentOption.offsetTop - parentPaddingTop, duration);
}

translateIndex(index: number, unit: NzTimePickerUnit): number {
Expand Down
23 changes: 22 additions & 1 deletion components/time-picker/time-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,28 @@ describe('time-picker', () => {
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(result.getHours()).toBe(0);
}));
it('should support ISO string', fakeAsync(() => {
testComponent.date = '2020-03-27T13:49:54.917Z';
fixture.detectChanges();
tick(500);
fixture.detectChanges();
testComponent.open = true;
fixture.detectChanges();
tick(500);
fixture.detectChanges();
const date = new Date(testComponent.date);
expect(queryFromOverlay('.ant-picker-time-panel-column:nth-child(1) .ant-picker-time-panel-cell-selected > div')!.textContent).toBe(
date.getHours().toString()
);
expect(queryFromOverlay('.ant-picker-time-panel-column:nth-child(2) .ant-picker-time-panel-cell-selected > div')!.textContent).toBe(
date.getMinutes().toString()
);
}));
});

function queryFromOverlay(selector: string): HTMLElement {
return overlayContainer.getContainerElement().querySelector(selector) as HTMLElement;
}
});

@Component({
Expand All @@ -142,7 +163,7 @@ export class NzTestTimePickerComponent {
open = false;
openChange = jasmine.createSpy('open change');
autoFocus = false;
date = new Date();
date: Date | string = new Date();
disabled = false;
use12Hours = false;
onChange(): void {}
Expand Down
10 changes: 8 additions & 2 deletions components/time-picker/time-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { slideMotion } from 'ng-zorro-antd/core/animation';

import { NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { warn } from 'ng-zorro-antd/core/logger';
import { InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';

const NZ_CONFIG_COMPONENT_NAME = 'timePicker';
Expand Down Expand Up @@ -250,8 +251,13 @@ export class NzTimePickerComponent implements ControlValueAccessor, OnInit, Afte
this.updateAutoFocus();
}

writeValue(time: Date | null): void {
this.value = time;
writeValue(time: Date): void {
if (time instanceof Date) {
this.value = time;
} else {
warn('Non-Date type is not recommended for time-picker, use "Date" type');
this.value = time ? new Date(time) : null;
}
this.cdr.markForCheck();
}

Expand Down
2 changes: 1 addition & 1 deletion components/time-picker/time-value-accessor.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class NzTimeValueAccessorDirective implements ControlValueAccessor {

changed(): void {
if (this._onChange) {
const value = this.dateHelper.parseTime(this.elementRef.nativeElement.value);
const value = this.dateHelper.parseTime(this.elementRef.nativeElement.value, this.nzTime);
this._onChange(value!);
}
}
Expand Down