fix: set default values to time widgets (#4383)

This commit is contained in:
Stanimira Vlaeva
2017-08-03 22:03:05 +03:00
committed by GitHub
parent c26f1c916a
commit 14098d40db
7 changed files with 141 additions and 124 deletions

View File

@ -5,7 +5,7 @@ export function getNativeYear(datePicker: datePickerModule.DatePicker): number {
} }
export function getNativeMonth(datePicker: datePickerModule.DatePicker): number { export function getNativeMonth(datePicker: datePickerModule.DatePicker): number {
return datePicker.android.getMonth() + 1; return datePicker.android.getMonth();
} }
export function getNativeDay(datePicker: datePickerModule.DatePicker): number { 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 { export function setNativeMonth(datePicker: datePickerModule.DatePicker, value: number): void {
datePicker.android.updateDate(datePicker.android.getYear(), value - 1, datePicker.android.getDayOfMonth()); datePicker.android.updateDate(datePicker.android.getYear(), value, datePicker.android.getDayOfMonth());
} }
export function setNativeDay(datePicker: datePickerModule.DatePicker, value: number): void { 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 { export function setNativeDate(datePicker: datePickerModule.DatePicker, year: number, month: number, day: number): void {
datePicker.android.updateDate(year, month - 1, day); datePicker.android.updateDate(year, month, day);
} }

View File

@ -7,7 +7,7 @@ export function getNativeYear(datePicker: datePickerModule.DatePicker): number {
} }
export function getNativeMonth(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; return utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date).month - 1;
} }
export function getNativeDay(datePicker: datePickerModule.DatePicker): number { 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 { 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); var comps = utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date);
comps.month = value; comps.month = value + 1;
datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false); datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false);
(<any>datePicker)._changeHandler.valueChanged(datePicker.ios); (<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 { 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); var comps = utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, datePicker.ios.date);
comps.year = year; comps.year = year;
comps.month = month; comps.month = month + 1;
comps.day = day; comps.day = day;
datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false); datePicker.ios.setDateAnimated(utils.ios.getter(NSCalendar, NSCalendar.currentCalendar).dateFromComponents(comps), false);
(<any>datePicker)._changeHandler.valueChanged(datePicker.ios); (<any>datePicker)._changeHandler.valueChanged(datePicker.ios);

View File

@ -13,6 +13,7 @@ import * as helper from "../helper";
export function test_recycling() { export function test_recycling() {
helper.nativeView_recycling_test(() => new datePickerModule.DatePicker()); helper.nativeView_recycling_test(() => new datePickerModule.DatePicker());
} }
const currentDate = new Date();
function assertDate(datePicker: datePickerModule.DatePicker, expectedYear: number, expectedMonth: number, expectedDay: number) { function assertDate(datePicker: datePickerModule.DatePicker, expectedYear: number, expectedMonth: number, expectedDay: number) {
TKUnit.assertEqual(datePicker.year, expectedYear, "datePicker.year"); TKUnit.assertEqual(datePicker.year, expectedYear, "datePicker.year");
@ -43,7 +44,7 @@ function assertDate(datePicker: datePickerModule.DatePicker, expectedYear: numbe
export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicker> { export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicker> {
public create() { public create() {
let datePicker = new datePickerModule.DatePicker(); const datePicker = new datePickerModule.DatePicker();
datePicker.id = "DatePicker"; datePicker.id = "DatePicker";
return datePicker; return datePicker;
} }
@ -63,7 +64,7 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
} }
public test_DummyForCodeSnippet() { public test_DummyForCodeSnippet() {
let datePicker = new datePickerModule.DatePicker(); const datePicker = new datePickerModule.DatePicker();
// >> date-picker-configure // >> date-picker-configure
datePicker.year = 1980; datePicker.year = 1980;
datePicker.month = 2; datePicker.month = 2;
@ -80,72 +81,72 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
} }
} }
public test_WhenCreated_YearIsUndefined() { public test_WhenCreated_YearIsCurrentYear() {
let actualValue = this.testView.year; const actualValue = this.testView.year;
let expectedValue = undefined; const expectedValue = currentDate.getFullYear();
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public test_WhenCreated_MonthIsUndefined() { public test_WhenCreated_MonthIsCurrentMonth() {
let actualValue = this.testView.month; const actualValue = this.testView.month;
let expectedValue = undefined; const expectedValue = currentDate.getMonth();
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public test_WhenCreated_DayIsUndefined() { public test_WhenCreated_DayIsCurrentDay() {
let actualValue = this.testView.day; const actualValue = this.testView.day;
let expectedValue = undefined; const expectedValue = currentDate.getDate();
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public test_WhenCreated_DateIsUndefined() { public test_WhenCreated_DateIsCurrentDate() {
let actualValue = this.testView.date; const expectedValue = currentDate;
let expectedValue = undefined; assertDate(this.testView, expectedValue.getFullYear(), expectedValue.getMonth(), expectedValue.getDate());
TKUnit.assertEqual(actualValue, expectedValue);
} }
public testYearFromLocalToNative() { public testYearFromLocalToNative() {
let expectedValue = 1980; const expectedValue = 1980;
this.testView.year = expectedValue; this.testView.year = expectedValue;
let actualValue = datePickerTestsNative.getNativeYear(this.testView); const actualValue = datePickerTestsNative.getNativeYear(this.testView);
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public testMonthFromLocalToNative() { public testMonthFromLocalToNative() {
let expectedValue = 5; const expectedValue = 5;
this.testView.month = expectedValue; this.testView.month = expectedValue;
let actualValue = datePickerTestsNative.getNativeMonth(this.testView); const actualValue = datePickerTestsNative.getNativeMonth(this.testView);
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public testDayFromLocalToNative() { public testDayFromLocalToNative() {
let expectedValue = 19; const expectedValue = 19;
this.testView.day = expectedValue; this.testView.day = expectedValue;
let actualValue = datePickerTestsNative.getNativeDay(this.testView); const actualValue = datePickerTestsNative.getNativeDay(this.testView);
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public test_DateIsSetCorrectlyWhenDayIsSet() { public test_DateIsSetCorrectlyWhenDayIsSet() {
let today = new Date(2016, 3, 15); const today = new Date(2016, 3, 15);
this.testView.year = today.getFullYear(); this.testView.year = today.getFullYear();
this.testView.month = today.getMonth(); this.testView.month = today.getMonth();
let expectedValue = today.getDate();
const expectedValue = today.getDate();
this.testView.day = expectedValue; this.testView.day = expectedValue;
let expectedDate = new Date(today.getFullYear(), today.getMonth() - 1, expectedValue); const expectedDate = new Date(today.getFullYear(), today.getMonth(), expectedValue);
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed."); TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
} }
public test_DateIsSetCorrectlyWhenMonthIsSet() { public test_DateIsSetCorrectlyWhenMonthIsSet() {
let today = new Date(2016, 3, 15); const today = new Date(2016, 3, 15);
this.testView.year = today.getFullYear(); this.testView.year = today.getFullYear();
this.testView.day = today.getDate(); this.testView.day = today.getDate();
let expectedValue = today.getMonth(); const expectedValue = today.getMonth();
this.testView.month = expectedValue; this.testView.month = expectedValue;
let expectedDate = new Date(today.getFullYear(), expectedValue - 1, today.getDate()); const expectedDate = new Date(today.getFullYear(), expectedValue, today.getDate());
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
@ -153,13 +154,13 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
} }
public test_DateIsSetCorrectlyWhenYearIsSet() { public test_DateIsSetCorrectlyWhenYearIsSet() {
let current = new Date(2016, 3, 15); const current = new Date(2016, 3, 15);
this.testView.month = current.getMonth(); this.testView.month = current.getMonth();
this.testView.day = current.getDate(); this.testView.day = current.getDate();
let expectedValue = 1980; const expectedValue = 1980;
this.testView.year = expectedValue; this.testView.year = expectedValue;
let expectedDate = new Date(1980, current.getMonth() - 1, current.getDate()); const expectedDate = new Date(1980, current.getMonth(), current.getDate());
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
@ -167,102 +168,102 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
} }
public testMaxDateFromLocalToNative() { public testMaxDateFromLocalToNative() {
let expectedValue = new Date(1980, 1, 9); const expectedValue = new Date(1980, 1, 9);
this.testView.maxDate = expectedValue; this.testView.maxDate = expectedValue;
let actualValue = datePickerTestsNative.getNativeMaxDate(this.testView); const actualValue = datePickerTestsNative.getNativeMaxDate(this.testView);
TKUnit.assertEqual(actualValue.getTime(), expectedValue.getTime()); TKUnit.assertEqual(actualValue.getTime(), expectedValue.getTime());
} }
public testMinDateFromLocalToNative() { public testMinDateFromLocalToNative() {
let expectedValue = new Date(1980, 1, 9); const expectedValue = new Date(1980, 1, 9);
this.testView.minDate = expectedValue; this.testView.minDate = expectedValue;
let actualValue = datePickerTestsNative.getNativeMinDate(this.testView); const actualValue = datePickerTestsNative.getNativeMinDate(this.testView);
TKUnit.assertEqual(actualValue.getTime(), expectedValue.getTime()); TKUnit.assertEqual(actualValue.getTime(), expectedValue.getTime());
} }
public testYearFromNativeToLocal() { public testYearFromNativeToLocal() {
let expectedValue = 1981; const expectedValue = 1981;
datePickerTestsNative.setNativeYear(this.testView, expectedValue); datePickerTestsNative.setNativeYear(this.testView, expectedValue);
let actualValue = this.testView.year; const actualValue = this.testView.year;
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public testMonthFromNativeToLocal() { public testMonthFromNativeToLocal() {
//Use July as it has 31 days // Use July as it has 31 days
var expectedValue = 7; const expectedValue = 7;
datePickerTestsNative.setNativeMonth(this.testView, expectedValue); datePickerTestsNative.setNativeMonth(this.testView, expectedValue);
var actualValue = this.testView.month; const actualValue = this.testView.month;
TKUnit.assertEqual(actualValue, expectedValue); TKUnit.assertEqual(actualValue, expectedValue);
} }
public testYearMonthDayFromNativeToLocal() { public testYearMonthDayFromNativeToLocal() {
var testYear = 2000; const testYear = 2000;
var testMonth = 3; const testMonth = 3;
var testDay = 24; const testDay = 24;
datePickerTestsNative.setNativeDate(this.testView, testYear, testMonth, testDay); datePickerTestsNative.setNativeDate(this.testView, testYear, testMonth, testDay);
assertDate(this.testView, testYear, testMonth, testDay); assertDate(this.testView, testYear, testMonth, testDay);
} }
public testDayFromNativeToLocal() { public testDayFromNativeToLocal() {
let expectedValue = 20; const expectedValue = 20;
datePickerTestsNative.setNativeDay(this.testView, expectedValue); datePickerTestsNative.setNativeDay(this.testView, expectedValue);
let actualValue = this.testView.day; const actualValue = this.testView.day;
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue); TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
} }
public test_DateFromNativeToLocalWithDay() { public test_DateFromNativeToLocalWithDay() {
let today = new Date(); const today = new Date();
let expectedValue = 20; const expectedValue = 20;
datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), today.getMonth(), expectedValue); datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), today.getMonth(), expectedValue);
let expectedDate = new Date(today.getFullYear(), today.getMonth() - 1, expectedValue); const expectedDate = new Date(today.getFullYear(), today.getMonth(), expectedValue);
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed."); TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
} }
public test_DateFromNativeToLocalWithMonth() { public test_DateFromNativeToLocalWithMonth() {
let today = new Date(); const today = new Date();
let expectedValue = 7; const expectedValue = 7;
datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), expectedValue, today.getDate()); datePickerTestsNative.setNativeDate(this.testView, today.getFullYear(), expectedValue, today.getDate());
let expectedDate = new Date(today.getFullYear(), expectedValue - 1, today.getDate()); const expectedDate = new Date(today.getFullYear(), expectedValue, today.getDate());
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed."); TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
} }
public test_DateFromNativeToLocalWithYear() { public test_DateFromNativeToLocalWithYear() {
let today = new Date(); const today = new Date();
var expectedValue = 1981; const expectedValue = 1981;
datePickerTestsNative.setNativeDate(this.testView, expectedValue, today.getMonth(), today.getDate()); datePickerTestsNative.setNativeDate(this.testView, expectedValue, today.getMonth(), today.getDate());
let expectedDate = new Date(expectedValue, today.getMonth() - 1, today.getDate()); const expectedDate = new Date(expectedValue, today.getMonth(), today.getDate());
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed."); TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
} }
public test_DateFromNativeToLocalWithAll() { public test_DateFromNativeToLocalWithAll() {
var testYear = 2000; const testYear = 2000;
var testMonth = 3; const testMonth = 3;
var testDay = 24; const testDay = 24;
datePickerTestsNative.setNativeDate(this.testView, testYear, testMonth, testDay); datePickerTestsNative.setNativeDate(this.testView, testYear, testMonth, testDay);
let expectedDate = new Date(testYear, testMonth - 1, testDay); const expectedDate = new Date(testYear, testMonth, testDay);
TKUnit.assertEqual(this.testView.date.getDate(), expectedDate.getDate(), "Getting Day from date property failed."); 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.getMonth(), expectedDate.getMonth(), "Getting Month from date property failed.");
TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed."); TKUnit.assertEqual(this.testView.date.getFullYear(), expectedDate.getFullYear(), "Getting Year from date property failed.");
} }
public testSetYearMonthDay_BeforeLoaded() { public testSetYearMonthDay_BeforeLoaded() {
var testYear = 2000; const testYear = 2000;
var testMonth = 3; const testMonth = 3;
var testDay = 24; const testDay = 24;
this.setUpDatePicker(testYear, testMonth, testDay); this.setUpDatePicker(testYear, testMonth, testDay);
@ -276,9 +277,9 @@ export class DatePickerTest extends testModule.UITest<datePickerModule.DatePicke
} }
public testSetYearMonthDay_AfterLoaded() { public testSetYearMonthDay_AfterLoaded() {
var testYear = 2000; const testYear = 2000;
var testMonth = 3; const testMonth = 3;
var testDay = 24; const testDay = 24;
this.testView.year = testYear; this.testView.year = testYear;
this.testView.month = testMonth; this.testView.month = testMonth;

View File

@ -3,6 +3,9 @@ import { View, Property } from "../core/view";
export * from "../core/view"; export * from "../core/view";
const defaultDate = new Date();
const dateComparer = (x: Date, y: Date): boolean => (x <= y && x >= y);
export class DatePickerBase extends View implements DatePickerDefinition { export class DatePickerBase extends View implements DatePickerDefinition {
public year: number; public year: number;
public month: number; public month: number;
@ -14,25 +17,46 @@ export class DatePickerBase extends View implements DatePickerDefinition {
DatePickerBase.prototype.recycleNativeView = "auto"; DatePickerBase.prototype.recycleNativeView = "auto";
export const yearProperty = new Property<DatePickerBase, number>({ name: "year", valueConverter: (v) => parseInt(v) }); export const yearProperty = new Property<DatePickerBase, number>({
name: "year",
defaultValue: defaultDate.getFullYear(),
valueConverter: v => parseInt(v),
});
yearProperty.register(DatePickerBase); yearProperty.register(DatePickerBase);
export const monthProperty = new Property<DatePickerBase, number>({ name: "month", valueConverter: (v) => parseInt(v) }); export const monthProperty = new Property<DatePickerBase, number>({
name: "month",
defaultValue: defaultDate.getMonth(),
valueConverter: v => parseInt(v),
});
monthProperty.register(DatePickerBase); monthProperty.register(DatePickerBase);
export const dayProperty = new Property<DatePickerBase, number>({ name: "day", valueConverter: (v) => parseInt(v) }); export const dayProperty = new Property<DatePickerBase, number>({
name: "day",
defaultValue: defaultDate.getDate(),
valueConverter: v => parseInt(v),
});
dayProperty.register(DatePickerBase); dayProperty.register(DatePickerBase);
function dateComparer(x: Date, y: Date): boolean {
return (x <= y && x >= y) ? true : false;
}
// TODO: Make CoercibleProperties // TODO: Make CoercibleProperties
export const maxDateProperty = new Property<DatePickerBase, Date>({ name: "maxDate", equalityComparer: dateComparer, valueConverter: (v) => new Date(v) }); export const maxDateProperty = new Property<DatePickerBase, Date>({
name: "maxDate",
equalityComparer: dateComparer,
valueConverter: v => new Date(v),
});
maxDateProperty.register(DatePickerBase); maxDateProperty.register(DatePickerBase);
export const minDateProperty = new Property<DatePickerBase, Date>({ name: "minDate", equalityComparer: dateComparer, valueConverter: (v) => new Date(v) }); export const minDateProperty = new Property<DatePickerBase, Date>({
name: "minDate",
equalityComparer: dateComparer,
valueConverter: v => new Date(v),
});
minDateProperty.register(DatePickerBase); minDateProperty.register(DatePickerBase);
export const dateProperty = new Property<DatePickerBase, Date>({ name: "date", equalityComparer: dateComparer, valueConverter: (v) => new Date(v) }); export const dateProperty = new Property<DatePickerBase, Date>({
dateProperty.register(DatePickerBase); name: "date",
defaultValue: defaultDate,
equalityComparer: dateComparer,
valueConverter: v => new Date(v),
});
dateProperty.register(DatePickerBase);

View File

@ -31,8 +31,8 @@ function initializeDateChangedListener(): void {
dateChanged = true; dateChanged = true;
} }
if ((month + 1) !== owner.month) { if (month !== owner.month) {
monthProperty.nativeValueChange(owner, month + 1); monthProperty.nativeValueChange(owner, month);
dateChanged = true; dateChanged = true;
} }
@ -58,7 +58,8 @@ export class DatePicker extends DatePickerBase {
const picker = new android.widget.DatePicker(this._context); const picker = new android.widget.DatePicker(this._context);
picker.setCalendarViewShown(false); picker.setCalendarViewShown(false);
const listener = new DateChangedListener(this); const listener = new DateChangedListener(this);
picker.init(0, 0, 0, listener);
picker.init(this.year, this.month, this.day, listener);
(<any>picker).listener = listener; (<any>picker).listener = listener;
return picker; return picker;
} }
@ -76,7 +77,7 @@ export class DatePicker extends DatePickerBase {
private updateNativeDate(): void { private updateNativeDate(): void {
const nativeView = this.nativeViewProtected; const nativeView = this.nativeViewProtected;
const year = typeof this.year === "number" ? this.year : nativeView.getYear(); const year = typeof this.year === "number" ? this.year : nativeView.getYear();
const month = typeof this.month === "number" ? (this.month - 1) : nativeView.getMonth(); const month = typeof this.month === "number" ? this.month : nativeView.getMonth();
const day = typeof this.day === "number" ? this.day : nativeView.getDayOfMonth(); const day = typeof this.day === "number" ? this.day : nativeView.getDayOfMonth();
this.date = new Date(year, month, day); this.date = new Date(year, month, day);
} }
@ -94,7 +95,7 @@ export class DatePicker extends DatePickerBase {
return this.nativeViewProtected.getMonth(); return this.nativeViewProtected.getMonth();
} }
[monthProperty.setNative](value: number) { [monthProperty.setNative](value: number) {
if (this.nativeViewProtected.getMonth() !== (value - 1)) { if (this.nativeViewProtected.getMonth() !== value) {
this.updateNativeDate(); this.updateNativeDate();
} }
} }
@ -136,4 +137,4 @@ export class DatePicker extends DatePickerBase {
const newValue = value instanceof Date ? value.getTime() : value; const newValue = value instanceof Date ? value.getTime() : value;
this.nativeViewProtected.setMinDate(newValue); this.nativeViewProtected.setMinDate(newValue);
} }
} }

View File

@ -29,38 +29,29 @@ export class DatePicker extends DatePickerBase {
return this.nativeViewProtected.date.getFullYear(); return this.nativeViewProtected.date.getFullYear();
} }
[yearProperty.setNative](value: number) { [yearProperty.setNative](value: number) {
let picker = this.nativeViewProtected; this.date = new Date(value, this.month, this.day);
let comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
comps.year = value;
this.date = new Date(comps.year, comps.month - 1, comps.day);
} }
[monthProperty.getDefault](): number { [monthProperty.getDefault](): number {
return this.nativeViewProtected.date.getMonth(); return this.nativeViewProtected.date.getMonth();
} }
[monthProperty.setNative](value: number) { [monthProperty.setNative](value: number) {
let picker = this.nativeViewProtected; this.date = new Date(this.year, value, this.day);
let comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
comps.month = value;
this.date = new Date(comps.year, comps.month - 1, comps.day);
} }
[dayProperty.getDefault](): number { [dayProperty.getDefault](): number {
return this.nativeViewProtected.date.getDay(); return this.nativeViewProtected.date.getDay();
} }
[dayProperty.setNative](value: number) { [dayProperty.setNative](value: number) {
let picker = this.nativeViewProtected; this.date = new Date(this.year, this.month, value);
let comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
comps.day = value;
this.date = new Date(comps.year, comps.month - 1, comps.day);
} }
[dateProperty.getDefault](): Date { [dateProperty.getDefault](): Date {
return this.nativeViewProtected.date; return this.nativeViewProtected.date;
} }
[dateProperty.setNative](value: Date) { [dateProperty.setNative](value: Date) {
let picker = this.nativeViewProtected; const picker = this.nativeViewProtected;
let comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date); const comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date);
comps.year = value.getFullYear(); comps.year = value.getFullYear();
comps.month = value.getMonth() + 1; comps.month = value.getMonth() + 1;
comps.day = value.getDate(); comps.day = value.getDate();
@ -71,8 +62,8 @@ export class DatePicker extends DatePickerBase {
return this.nativeViewProtected.maximumDate; return this.nativeViewProtected.maximumDate;
} }
[maxDateProperty.setNative](value: Date) { [maxDateProperty.setNative](value: Date) {
let picker = this.nativeViewProtected; const picker = this.nativeViewProtected;
let nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000); const nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
picker.maximumDate = <any>nsDate; picker.maximumDate = <any>nsDate;
} }
@ -80,8 +71,8 @@ export class DatePicker extends DatePickerBase {
return this.nativeViewProtected.minimumDate; return this.nativeViewProtected.minimumDate;
} }
[minDateProperty.setNative](value: Date) { [minDateProperty.setNative](value: Date) {
let picker = this.nativeViewProtected; const picker = this.nativeViewProtected;
let nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000); const nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
picker.minimumDate = <any>nsDate; picker.minimumDate = <any>nsDate;
} }
@ -89,7 +80,7 @@ export class DatePicker extends DatePickerBase {
return this.nativeViewProtected.valueForKey("textColor"); return this.nativeViewProtected.valueForKey("textColor");
} }
[colorProperty.setNative](value: Color | UIColor) { [colorProperty.setNative](value: Color | UIColor) {
let picker = this.nativeViewProtected; const picker = this.nativeViewProtected;
picker.setValueForKey(value instanceof Color ? value.ios : value, "textColor"); picker.setValueForKey(value instanceof Color ? value.ios : value, "textColor");
} }
} }
@ -98,15 +89,15 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
private _owner: WeakRef<DatePicker>; private _owner: WeakRef<DatePicker>;
public static initWithOwner(owner: WeakRef<DatePicker>): UIDatePickerChangeHandlerImpl { public static initWithOwner(owner: WeakRef<DatePicker>): UIDatePickerChangeHandlerImpl {
let impl = <UIDatePickerChangeHandlerImpl>UIDatePickerChangeHandlerImpl.new(); const impl = <UIDatePickerChangeHandlerImpl>UIDatePickerChangeHandlerImpl.new();
impl._owner = owner; impl._owner = owner;
return impl; return impl;
} }
public valueChanged(sender: UIDatePicker) { public valueChanged(sender: UIDatePicker) {
let comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, sender.date); const comps = ios.getter(NSCalendar, NSCalendar.currentCalendar).componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, sender.date);
let owner = this._owner.get(); const owner = this._owner.get();
if (!owner) { if (!owner) {
return; return;
} }
@ -117,8 +108,9 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
dateChanged = true; dateChanged = true;
} }
if (comps.month !== owner.month) { const jsMonth = comps.month - 1;
monthProperty.nativeValueChange(owner, comps.month); if (jsMonth !== owner.month) {
monthProperty.nativeValueChange(owner, jsMonth);
dateChanged = true; dateChanged = true;
} }
@ -128,7 +120,7 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
} }
if (dateChanged) { if (dateChanged) {
dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day)); dateProperty.nativeValueChange(owner, new Date(comps.year, jsMonth, comps.day));
} }
} }

View File

@ -8,6 +8,8 @@ interface Time {
minute: number; minute: number;
} }
const dateComparer = (x: Date, y: Date): boolean => (x <= y && x >= y);
export function getValidTime(picker: TimePickerDefinition, hour: number, minute: number): Time { export function getValidTime(picker: TimePickerDefinition, hour: number, minute: number): Time {
if (picker.minuteInterval > 1) { if (picker.minuteInterval > 1) {
let minuteFloor = minute - (minute % picker.minuteInterval); let minuteFloor = minute - (minute % picker.minuteInterval);
@ -97,7 +99,7 @@ export abstract class TimePickerBase extends View implements TimePickerDefinitio
TimePickerBase.prototype.recycleNativeView = "auto"; TimePickerBase.prototype.recycleNativeView = "auto";
export var minHourProperty = new Property<TimePickerBase, number>({ export const minHourProperty = new Property<TimePickerBase, number>({
name: "minHour", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => { name: "minHour", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => {
if (!isHourValid(newValue) || !isValidTime(picker)) { if (!isHourValid(newValue) || !isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "minHour", newValue)); throw new Error(getErrorMessage(picker, "minHour", newValue));
@ -106,7 +108,7 @@ export var minHourProperty = new Property<TimePickerBase, number>({
}); });
minHourProperty.register(TimePickerBase); minHourProperty.register(TimePickerBase);
export var maxHourProperty = new Property<TimePickerBase, number>({ export const maxHourProperty = new Property<TimePickerBase, number>({
name: "maxHour", defaultValue: 23, valueChanged: (picker, oldValue, newValue) => { name: "maxHour", defaultValue: 23, valueChanged: (picker, oldValue, newValue) => {
if (!isHourValid(newValue) || !isValidTime(picker)) { if (!isHourValid(newValue) || !isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "maxHour", newValue)); throw new Error(getErrorMessage(picker, "maxHour", newValue));
@ -116,7 +118,7 @@ export var maxHourProperty = new Property<TimePickerBase, number>({
}); });
maxHourProperty.register(TimePickerBase); maxHourProperty.register(TimePickerBase);
export var minMinuteProperty = new Property<TimePickerBase, number>({ export const minMinuteProperty = new Property<TimePickerBase, number>({
name: "minMinute", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => { name: "minMinute", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => {
if (!isMinuteValid(newValue) || !isValidTime(picker)) { if (!isMinuteValid(newValue) || !isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "minMinute", newValue)); throw new Error(getErrorMessage(picker, "minMinute", newValue));
@ -125,7 +127,7 @@ export var minMinuteProperty = new Property<TimePickerBase, number>({
}); });
minMinuteProperty.register(TimePickerBase); minMinuteProperty.register(TimePickerBase);
export var maxMinuteProperty = new Property<TimePickerBase, number>({ export const maxMinuteProperty = new Property<TimePickerBase, number>({
name: "maxMinute", defaultValue: 59, valueChanged: (picker, oldValue, newValue) => { name: "maxMinute", defaultValue: 59, valueChanged: (picker, oldValue, newValue) => {
if (!isMinuteValid(newValue) || !isValidTime(picker)) { if (!isMinuteValid(newValue) || !isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "maxMinute", newValue)); throw new Error(getErrorMessage(picker, "maxMinute", newValue));
@ -134,7 +136,7 @@ export var maxMinuteProperty = new Property<TimePickerBase, number>({
}); });
maxMinuteProperty.register(TimePickerBase); maxMinuteProperty.register(TimePickerBase);
export var minuteIntervalProperty = new Property<TimePickerBase, number>({ export const minuteIntervalProperty = new Property<TimePickerBase, number>({
name: "minuteInterval", defaultValue: 1, valueChanged: (picker, oldValue, newValue) => { name: "minuteInterval", defaultValue: 1, valueChanged: (picker, oldValue, newValue) => {
if (!isMinuteIntervalValid(newValue)) { if (!isMinuteIntervalValid(newValue)) {
throw new Error(getErrorMessage(picker, "minuteInterval", newValue)); throw new Error(getErrorMessage(picker, "minuteInterval", newValue));
@ -143,11 +145,7 @@ export var minuteIntervalProperty = new Property<TimePickerBase, number>({
}); });
minuteIntervalProperty.register(TimePickerBase); minuteIntervalProperty.register(TimePickerBase);
function dateComparer(x: Date, y: Date): boolean { export const minuteProperty = new Property<TimePickerBase, number>({
return (x <= y && x >= y) ? true : false;
}
export var minuteProperty = new Property<TimePickerBase, number>({
name: "minute", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => { name: "minute", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => {
if (!isMinuteValid(newValue) || !isValidTime(picker)) { if (!isMinuteValid(newValue) || !isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "minute", newValue)); throw new Error(getErrorMessage(picker, "minute", newValue));
@ -158,7 +156,7 @@ export var minuteProperty = new Property<TimePickerBase, number>({
}); });
minuteProperty.register(TimePickerBase); minuteProperty.register(TimePickerBase);
export var hourProperty = new Property<TimePickerBase, number>({ export const hourProperty = new Property<TimePickerBase, number>({
name: "hour", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => { name: "hour", defaultValue: 0, valueChanged: (picker, oldValue, newValue) => {
if (!isHourValid(newValue) || !isValidTime(picker)) { if (!isHourValid(newValue) || !isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "Hour", newValue)); throw new Error(getErrorMessage(picker, "Hour", newValue));
@ -170,8 +168,9 @@ export var hourProperty = new Property<TimePickerBase, number>({
}); });
hourProperty.register(TimePickerBase); hourProperty.register(TimePickerBase);
export var timeProperty = new Property<TimePickerBase, Date>({ export const timeProperty = new Property<TimePickerBase, Date>({
name: "time", equalityComparer: dateComparer, valueChanged: (picker, oldValue, newValue) => { name: "time", defaultValue: new Date(), equalityComparer: dateComparer,
valueChanged: (picker, oldValue, newValue) => {
if (!isValidTime(picker)) { if (!isValidTime(picker)) {
throw new Error(getErrorMessage(picker, "time", newValue)); throw new Error(getErrorMessage(picker, "time", newValue));
} }