mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
refactor(date-picker): value converter for month is 1-based (#4656)
This commit is contained in:
@ -5,7 +5,7 @@ export function getNativeYear(datePicker: datePickerModule.DatePicker): number {
|
||||
}
|
||||
|
||||
export function getNativeMonth(datePicker: datePickerModule.DatePicker): number {
|
||||
return datePicker.android.getMonth();
|
||||
return datePicker.android.getMonth() + 1;
|
||||
}
|
||||
|
||||
export function getNativeDay(datePicker: datePickerModule.DatePicker): number {
|
||||
@ -25,7 +25,7 @@ export function setNativeYear(datePicker: datePickerModule.DatePicker, value: nu
|
||||
}
|
||||
|
||||
export function setNativeMonth(datePicker: datePickerModule.DatePicker, value: number): void {
|
||||
datePicker.android.updateDate(datePicker.android.getYear(), value, datePicker.android.getDayOfMonth());
|
||||
datePicker.android.updateDate(datePicker.android.getYear(), value - 1, datePicker.android.getDayOfMonth());
|
||||
}
|
||||
|
||||
export function setNativeDay(datePicker: datePickerModule.DatePicker, value: number): void {
|
||||
@ -33,5 +33,5 @@ export function setNativeDay(datePicker: datePickerModule.DatePicker, value: num
|
||||
}
|
||||
|
||||
export function setNativeDate(datePicker: datePickerModule.DatePicker, year: number, month: number, day: number): void {
|
||||
datePicker.android.updateDate(year, month, day);
|
||||
datePicker.android.updateDate(year, month - 1, day);
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ export function getNativeYear(datePicker: datePickerModule.DatePicker): number {
|
||||
}
|
||||
|
||||
export function getNativeMonth(datePicker: datePickerModule.DatePicker): number {
|
||||
return utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date).month - 1;
|
||||
return utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date).month;
|
||||
}
|
||||
|
||||
export function getNativeDay(datePicker: datePickerModule.DatePicker): number {
|
||||
@ -31,7 +31,7 @@ export function setNativeYear(datePicker: datePickerModule.DatePicker, value: nu
|
||||
|
||||
export function setNativeMonth(datePicker: datePickerModule.DatePicker, value: number): void {
|
||||
var comps = utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date);
|
||||
comps.month = value + 1;
|
||||
comps.month = value;
|
||||
datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false);
|
||||
(<any>datePicker)._changeHandler.valueChanged(datePicker.ios);
|
||||
}
|
||||
@ -46,7 +46,7 @@ export function setNativeDay(datePicker: datePickerModule.DatePicker, value: num
|
||||
export function setNativeDate(datePicker: datePickerModule.DatePicker, year: number, month: number, day: number): void {
|
||||
var comps = utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date);
|
||||
comps.year = year;
|
||||
comps.month = month + 1;
|
||||
comps.month = month;
|
||||
comps.day = day;
|
||||
datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false);
|
||||
(<any>datePicker)._changeHandler.valueChanged(datePicker.ios);
|
||||
|
@ -89,7 +89,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
|
||||
public test_WhenCreated_MonthIsCurrentMonth() {
|
||||
const actualValue = this.testView.month;
|
||||
const expectedValue = currentDate.getMonth();
|
||||
const expectedValue = currentDate.getMonth() + 1;
|
||||
TKUnit.assertEqual(actualValue, expectedValue);
|
||||
}
|
||||
|
||||
@ -99,11 +99,6 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
TKUnit.assertEqual(actualValue, expectedValue);
|
||||
}
|
||||
|
||||
public test_WhenCreated_DateIsCurrentDate() {
|
||||
const expectedValue = currentDate;
|
||||
assertDate(this.testView, expectedValue.getFullYear(), expectedValue.getMonth(), expectedValue.getDate());
|
||||
}
|
||||
|
||||
public testYearFromLocalToNative() {
|
||||
const expectedValue = 1980;
|
||||
this.testView.year = expectedValue;
|
||||
@ -129,11 +124,10 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
const today = new Date(2016, 3, 15);
|
||||
this.testView.year = today.getFullYear();
|
||||
this.testView.month = today.getMonth();
|
||||
|
||||
const expectedValue = today.getDate();
|
||||
this.testView.day = expectedValue;
|
||||
|
||||
const expectedDate = new Date(today.getFullYear(), today.getMonth(), expectedValue);
|
||||
const expectedDate = new Date(today.getFullYear(), today.getMonth() - 1, expectedValue);
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
|
||||
@ -146,7 +140,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
|
||||
const expectedValue = today.getMonth();
|
||||
this.testView.month = expectedValue;
|
||||
const expectedDate = new Date(today.getFullYear(), expectedValue, today.getDate());
|
||||
const expectedDate = new Date(today.getFullYear(), expectedValue - 1, today.getDate());
|
||||
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
@ -160,7 +154,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
|
||||
const expectedValue = 1980;
|
||||
this.testView.year = expectedValue;
|
||||
const expectedDate = new Date(1980, current.getMonth(), current.getDate());
|
||||
const expectedDate = new Date(1980, current.getMonth() - 1, current.getDate());
|
||||
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
@ -218,7 +212,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
|
||||
datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), today.getMonth(), expectedValue);
|
||||
|
||||
const expectedDate = new Date(today.getFullYear(), today.getMonth(), expectedValue);
|
||||
const expectedDate = new Date(today.getFullYear(), today.getMonth() - 1, expectedValue);
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
|
||||
@ -230,7 +224,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
|
||||
datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), expectedValue, today.getDate());
|
||||
|
||||
const expectedDate = new Date(today.getFullYear(), expectedValue, today.getDate());
|
||||
const expectedDate = new Date(today.getFullYear(), expectedValue - 1, today.getDate());
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
|
||||
@ -242,7 +236,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
const expectedValue = 1981;
|
||||
datePickerTestsNative.setNativeDate(this.testView, expectedValue, today.getMonth(), today.getDate());
|
||||
|
||||
const expectedDate = new Date(expectedValue, today.getMonth(), today.getDate());
|
||||
const expectedDate = new Date(expectedValue, today.getMonth() - 1, today.getDate());
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
|
||||
@ -254,7 +248,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
|
||||
const testDay = 24;
|
||||
|
||||
datePickerTestsNative.setNativeDate(this.testView, testYear, testMonth, testDay);
|
||||
const expectedDate = new Date(testYear, testMonth, testDay);
|
||||
const expectedDate = new Date(testYear, testMonth - 1, testDay);
|
||||
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
|
||||
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
|
||||
|
@ -26,7 +26,7 @@ yearProperty.register(DatePickerBase);
|
||||
|
||||
export const monthProperty = new Property<DatePickerBase, number>({
|
||||
name: "month",
|
||||
defaultValue: defaultDate.getMonth(),
|
||||
defaultValue: defaultDate.getMonth() + 1,
|
||||
valueConverter: v => parseInt(v),
|
||||
});
|
||||
monthProperty.register(DatePickerBase);
|
||||
|
@ -31,8 +31,8 @@ function initializeDateChangedListener(): void {
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
if (month !== owner.month) {
|
||||
monthProperty.nativeValueChange(owner, month);
|
||||
if (month !== (owner.month - 1)) {
|
||||
monthProperty.nativeValueChange(owner, month + 1);
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
@ -77,42 +77,29 @@ export class DatePicker extends DatePickerBase {
|
||||
private updateNativeDate(): void {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
const year = typeof this.year === "number" ? this.year : nativeView.getYear();
|
||||
const month = typeof this.month === "number" ? this.month : nativeView.getMonth();
|
||||
const month = typeof this.month === "number" ? this.month - 1 : nativeView.getMonth();
|
||||
const day = typeof this.day === "number" ? this.day : nativeView.getDayOfMonth();
|
||||
this.date = new Date(year, month, day);
|
||||
}
|
||||
|
||||
[yearProperty.getDefault](): number {
|
||||
return this.nativeViewProtected.getYear();
|
||||
}
|
||||
[yearProperty.setNative](value: number) {
|
||||
if (this.nativeViewProtected.getYear() !== value) {
|
||||
this.updateNativeDate();
|
||||
}
|
||||
}
|
||||
|
||||
[monthProperty.getDefault](): number {
|
||||
return this.nativeViewProtected.getMonth();
|
||||
}
|
||||
[monthProperty.setNative](value: number) {
|
||||
if (this.nativeViewProtected.getMonth() !== value) {
|
||||
if (this.nativeViewProtected.getMonth() !== (value - 1)) {
|
||||
this.updateNativeDate();
|
||||
}
|
||||
}
|
||||
|
||||
[dayProperty.getDefault](): number {
|
||||
return this.nativeViewProtected.getDayOfMonth();
|
||||
}
|
||||
[dayProperty.setNative](value: number) {
|
||||
if (this.nativeViewProtected.getDayOfMonth() !== value) {
|
||||
this.updateNativeDate();
|
||||
}
|
||||
}
|
||||
|
||||
[dateProperty.getDefault](): Date {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
return new Date(nativeView.getYear(), nativeView.getMonth(), nativeView.getDayOfMonth());
|
||||
}
|
||||
[dateProperty.setNative](value: Date) {
|
||||
const nativeView = this.nativeViewProtected;
|
||||
if (nativeView.getDayOfMonth() !== value.getDay()
|
||||
|
@ -25,30 +25,18 @@ export class DatePicker extends DatePickerBase {
|
||||
return this.nativeViewProtected;
|
||||
}
|
||||
|
||||
[yearProperty.getDefault](): number {
|
||||
return this.nativeViewProtected.date.getFullYear();
|
||||
}
|
||||
[yearProperty.setNative](value: number) {
|
||||
this.date = new Date(value, this.month, this.day);
|
||||
this.date = new Date(value, this.month - 1, this.day);
|
||||
}
|
||||
|
||||
[monthProperty.getDefault](): number {
|
||||
return this.nativeViewProtected.date.getMonth();
|
||||
}
|
||||
[monthProperty.setNative](value: number) {
|
||||
this.date = new Date(this.year, value, this.day);
|
||||
this.date = new Date(this.year, value - 1, this.day);
|
||||
}
|
||||
|
||||
[dayProperty.getDefault](): number {
|
||||
return this.nativeViewProtected.date.getDay();
|
||||
}
|
||||
[dayProperty.setNative](value: number) {
|
||||
this.date = new Date(this.year, this.month, value);
|
||||
this.date = new Date(this.year, this.month - 1, value);
|
||||
}
|
||||
|
||||
[dateProperty.getDefault](): Date {
|
||||
return this.nativeViewProtected.date;
|
||||
}
|
||||
[dateProperty.setNative](value: Date) {
|
||||
const picker = this.nativeViewProtected;
|
||||
const comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
|
||||
@ -108,9 +96,8 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
const jsMonth = comps.month - 1;
|
||||
if (jsMonth !== owner.month) {
|
||||
monthProperty.nativeValueChange(owner, jsMonth);
|
||||
if (comps.month !== owner.month) {
|
||||
monthProperty.nativeValueChange(owner, comps.month);
|
||||
dateChanged = true;
|
||||
}
|
||||
|
||||
@ -120,7 +107,7 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
|
||||
}
|
||||
|
||||
if (dateChanged) {
|
||||
dateProperty.nativeValueChange(owner, new Date(comps.year, jsMonth, comps.day));
|
||||
dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user