Skip to content

Commit 08dcfab

Browse files
authored
refactor(date-picker): value converter for month is 1-based (NativeScript#4656)
1 parent 2492344 commit 08dcfab

File tree

6 files changed

+25
-57
lines changed

6 files changed

+25
-57
lines changed

tests/app/ui/date-picker/date-picker-tests-native.android.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function getNativeYear(datePicker: datePickerModule.DatePicker): number {
55
}
66

77
export function getNativeMonth(datePicker: datePickerModule.DatePicker): number {
8-
return datePicker.android.getMonth();
8+
return datePicker.android.getMonth() + 1;
99
}
1010

1111
export function getNativeDay(datePicker: datePickerModule.DatePicker): number {
@@ -25,13 +25,13 @@ export function setNativeYear(datePicker: datePickerModule.DatePicker, value: nu
2525
}
2626

2727
export function setNativeMonth(datePicker: datePickerModule.DatePicker, value: number): void {
28-
datePicker.android.updateDate(datePicker.android.getYear(), value, datePicker.android.getDayOfMonth());
28+
datePicker.android.updateDate(datePicker.android.getYear(), value - 1, datePicker.android.getDayOfMonth());
2929
}
3030

3131
export function setNativeDay(datePicker: datePickerModule.DatePicker, value: number): void {
3232
datePicker.android.updateDate(datePicker.android.getYear(), datePicker.android.getMonth(), value);
3333
}
3434

3535
export function setNativeDate(datePicker: datePickerModule.DatePicker, year: number, month: number, day: number): void {
36-
datePicker.android.updateDate(year, month, day);
36+
datePicker.android.updateDate(year, month - 1, day);
3737
}

tests/app/ui/date-picker/date-picker-tests-native.ios.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function getNativeYear(datePicker: datePickerModule.DatePicker): number {
77
}
88

99
export function getNativeMonth(datePicker: datePickerModule.DatePicker): number {
10-
return utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date).month - 1;
10+
return utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date).month;
1111
}
1212

1313
export function getNativeDay(datePicker: datePickerModule.DatePicker): number {
@@ -31,7 +31,7 @@ export function setNativeYear(datePicker: datePickerModule.DatePicker, value: nu
3131

3232
export function setNativeMonth(datePicker: datePickerModule.DatePicker, value: number): void {
3333
var comps = utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date);
34-
comps.month = value + 1;
34+
comps.month = value;
3535
datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false);
3636
(<any>datePicker)._changeHandler.valueChanged(datePicker.ios);
3737
}
@@ -46,7 +46,7 @@ export function setNativeDay(datePicker: datePickerModule.DatePicker, value: num
4646
export function setNativeDate(datePicker: datePickerModule.DatePicker, year: number, month: number, day: number): void {
4747
var comps = utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date);
4848
comps.year = year;
49-
comps.month = month + 1;
49+
comps.month = month;
5050
comps.day = day;
5151
datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false);
5252
(<any>datePicker)._changeHandler.valueChanged(datePicker.ios);

tests/app/ui/date-picker/date-picker-tests.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
8989

9090
public test_WhenCreated_MonthIsCurrentMonth() {
9191
const actualValue = this.testView.month;
92-
const expectedValue = currentDate.getMonth();
92+
const expectedValue = currentDate.getMonth() + 1;
9393
TKUnit.assertEqual(actualValue, expectedValue);
9494
}
9595

@@ -99,11 +99,6 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
9999
TKUnit.assertEqual(actualValue, expectedValue);
100100
}
101101

102-
public test_WhenCreated_DateIsCurrentDate() {
103-
const expectedValue = currentDate;
104-
assertDate(this.testView, expectedValue.getFullYear(), expectedValue.getMonth(), expectedValue.getDate());
105-
}
106-
107102
public testYearFromLocalToNative() {
108103
const expectedValue = 1980;
109104
this.testView.year = expectedValue;
@@ -129,11 +124,10 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
129124
const today = new Date(2016, 3, 15);
130125
this.testView.year = today.getFullYear();
131126
this.testView.month = today.getMonth();
132-
133127
const expectedValue = today.getDate();
134128
this.testView.day = expectedValue;
135129

136-
const expectedDate = new Date(today.getFullYear(), today.getMonth(), expectedValue);
130+
const expectedDate = new Date(today.getFullYear(), today.getMonth() - 1, expectedValue);
137131
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
138132
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
139133
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
146140

147141
const expectedValue = today.getMonth();
148142
this.testView.month = expectedValue;
149-
const expectedDate = new Date(today.getFullYear(), expectedValue, today.getDate());
143+
const expectedDate = new Date(today.getFullYear(), expectedValue - 1, today.getDate());
150144

151145
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
152146
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
160154

161155
const expectedValue = 1980;
162156
this.testView.year = expectedValue;
163-
const expectedDate = new Date(1980, current.getMonth(), current.getDate());
157+
const expectedDate = new Date(1980, current.getMonth() - 1, current.getDate());
164158

165159
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
166160
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
218212

219213
datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), today.getMonth(), expectedValue);
220214

221-
const expectedDate = new Date(today.getFullYear(), today.getMonth(), expectedValue);
215+
const expectedDate = new Date(today.getFullYear(), today.getMonth() - 1, expectedValue);
222216
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
223217
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
224218
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
230224

231225
datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), expectedValue, today.getDate());
232226

233-
const expectedDate = new Date(today.getFullYear(), expectedValue, today.getDate());
227+
const expectedDate = new Date(today.getFullYear(), expectedValue - 1, today.getDate());
234228
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
235229
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
236230
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
242236
const expectedValue = 1981;
243237
datePickerTestsNative.setNativeDate(this.testView, expectedValue, today.getMonth(), today.getDate());
244238

245-
const expectedDate = new Date(expectedValue, today.getMonth(), today.getDate());
239+
const expectedDate = new Date(expectedValue, today.getMonth() - 1, today.getDate());
246240
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
247241
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
248242
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
254248
const testDay = 24;
255249

256250
datePickerTestsNative.setNativeDate(this.testView, testYear, testMonth, testDay);
257-
const expectedDate = new Date(testYear, testMonth, testDay);
251+
const expectedDate = new Date(testYear, testMonth - 1, testDay);
258252
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed.");
259253
TKUnit.assertEqual(this.testView.date.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
260254
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");

tns-core-modules/ui/date-picker/date-picker-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ yearProperty.register(DatePickerBase);
2626

2727
export const monthProperty = new Property<DatePickerBase, number>({
2828
name: "month",
29-
defaultValue: defaultDate.getMonth(),
29+
defaultValue: defaultDate.getMonth() + 1,
3030
valueConverter: v => parseInt(v),
3131
});
3232
monthProperty.register(DatePickerBase);

tns-core-modules/ui/date-picker/date-picker.android.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function initializeDateChangedListener(): void {
3131
dateChanged = true;
3232
}
3333

34-
if (month !== owner.month) {
35-
monthProperty.nativeValueChange(owner, month);
34+
if (month !== (owner.month - 1)) {
35+
monthProperty.nativeValueChange(owner, month + 1);
3636
dateChanged = true;
3737
}
3838

@@ -77,42 +77,29 @@ export class DatePicker extends DatePickerBase {
7777
private updateNativeDate(): void {
7878
const nativeView = this.nativeViewProtected;
7979
const year = typeof this.year === "number" ? this.year : nativeView.getYear();
80-
const month = typeof this.month === "number" ? this.month : nativeView.getMonth();
80+
const month = typeof this.month === "number" ? this.month - 1 : nativeView.getMonth();
8181
const day = typeof this.day === "number" ? this.day : nativeView.getDayOfMonth();
8282
this.date = new Date(year, month, day);
8383
}
8484

85-
[yearProperty.getDefault](): number {
86-
return this.nativeViewProtected.getYear();
87-
}
8885
[yearProperty.setNative](value: number) {
8986
if (this.nativeViewProtected.getYear() !== value) {
9087
this.updateNativeDate();
9188
}
9289
}
9390

94-
[monthProperty.getDefault](): number {
95-
return this.nativeViewProtected.getMonth();
96-
}
9791
[monthProperty.setNative](value: number) {
98-
if (this.nativeViewProtected.getMonth() !== value) {
92+
if (this.nativeViewProtected.getMonth() !== (value - 1)) {
9993
this.updateNativeDate();
10094
}
10195
}
10296

103-
[dayProperty.getDefault](): number {
104-
return this.nativeViewProtected.getDayOfMonth();
105-
}
10697
[dayProperty.setNative](value: number) {
10798
if (this.nativeViewProtected.getDayOfMonth() !== value) {
10899
this.updateNativeDate();
109100
}
110101
}
111102

112-
[dateProperty.getDefault](): Date {
113-
const nativeView = this.nativeViewProtected;
114-
return new Date(nativeView.getYear(), nativeView.getMonth(), nativeView.getDayOfMonth());
115-
}
116103
[dateProperty.setNative](value: Date) {
117104
const nativeView = this.nativeViewProtected;
118105
if (nativeView.getDayOfMonth() !== value.getDay()

tns-core-modules/ui/date-picker/date-picker.ios.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,18 @@ export class DatePicker extends DatePickerBase {
2525
return this.nativeViewProtected;
2626
}
2727

28-
[yearProperty.getDefault](): number {
29-
return this.nativeViewProtected.date.getFullYear();
30-
}
3128
[yearProperty.setNative](value: number) {
32-
this.date = new Date(value, this.month, this.day);
29+
this.date = new Date(value, this.month - 1, this.day);
3330
}
3431

35-
[monthProperty.getDefault](): number {
36-
return this.nativeViewProtected.date.getMonth();
37-
}
3832
[monthProperty.setNative](value: number) {
39-
this.date = new Date(this.year, value, this.day);
33+
this.date = new Date(this.year, value - 1, this.day);
4034
}
4135

42-
[dayProperty.getDefault](): number {
43-
return this.nativeViewProtected.date.getDay();
44-
}
4536
[dayProperty.setNative](value: number) {
46-
this.date = new Date(this.year, this.month, value);
37+
this.date = new Date(this.year, this.month - 1, value);
4738
}
4839

49-
[dateProperty.getDefault](): Date {
50-
return this.nativeViewProtected.date;
51-
}
5240
[dateProperty.setNative](value: Date) {
5341
const picker = this.nativeViewProtected;
5442
const comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
@@ -108,9 +96,8 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
10896
dateChanged = true;
10997
}
11098

111-
const jsMonth = comps.month - 1;
112-
if (jsMonth !== owner.month) {
113-
monthProperty.nativeValueChange(owner, jsMonth);
99+
if (comps.month !== owner.month) {
100+
monthProperty.nativeValueChange(owner, comps.month);
114101
dateChanged = true;
115102
}
116103

@@ -120,7 +107,7 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
120107
}
121108

122109
if (dateChanged) {
123-
dateProperty.nativeValueChange(owner, new Date(comps.year, jsMonth, comps.day));
110+
dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day));
124111
}
125112
}
126113

0 commit comments

Comments
 (0)