mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactor(date-picker): value converter for month is 1-based (#4656)
This commit is contained in:
@@ -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