feat(datepicker): ability to show time via showTime property (#9570)

This commit is contained in:
Nathan Walker
2022-03-04 19:32:47 -08:00
committed by GitHub
parent c68d002c9a
commit ab4e47a1c1
11 changed files with 189 additions and 24 deletions

View File

@ -17,13 +17,15 @@
"ios": { "ios": {
"executor": "@nativescript/nx:build", "executor": "@nativescript/nx:build",
"options": { "options": {
"platform": "ios" "platform": "ios",
"noHmr": true
} }
}, },
"android": { "android": {
"executor": "@nativescript/nx:build", "executor": "@nativescript/nx:build",
"options": { "options": {
"platform": "android" "platform": "android",
"noHmr": true
} }
}, },
"clean": { "clean": {

View File

@ -9,6 +9,7 @@
<Button text="a11y" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" /> <Button text="a11y" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="box-shadow" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" /> <Button text="box-shadow" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="css-playground" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" /> <Button text="css-playground" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="datepicker" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="image-async" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" /> <Button text="image-async" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="image-handling" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" /> <Button text="image-handling" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="list-page" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" /> <Button text="list-page" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />

View File

@ -26,10 +26,10 @@ export class AccessibilityModel extends Observable {
checkedChange(args) { checkedChange(args) {
const checked = (args.object as Switch).checked; const checked = (args.object as Switch).checked;
console.log(checked); console.log(checked);
this.set('switchCheckedText', `${this.labelText} ${checked}`); this.notifyPropertyChange('switchCheckedText', `${this.labelText} ${checked}`);
// prettier-ignore // prettier-ignore
this.set('largeImageSrc', checked ? this.notifyPropertyChange('largeImageSrc', checked ?
'https://i.picsum.photos/id/669/5000/5000.jpg?hmac=VlpchW0ODhflKm0SKOYQrc8qysLWbqKmDS1MGT9apAc' : 'https://i.picsum.photos/id/669/5000/5000.jpg?hmac=VlpchW0ODhflKm0SKOYQrc8qysLWbqKmDS1MGT9apAc' :
'https://i.picsum.photos/id/684/5000/5000.jpg?hmac=loiXO_OQ-y86XY_hc7p3qJdY39fSd9CuDM0iA_--P4Q'); 'https://i.picsum.photos/id/684/5000/5000.jpg?hmac=loiXO_OQ-y86XY_hc7p3qJdY39fSd9CuDM0iA_--P4Q');
} }

View File

@ -0,0 +1,31 @@
import { Page, ImageSource, Observable, EventData, knownFolders, path, Switch, DatePicker } from '@nativescript/core';
let page: Page;
export function navigatingTo(args: EventData) {
page = <Page>args.object;
page.bindingContext = new SampleData();
}
export class SampleData extends Observable {
displayDate = {
day: new Date().getDate(),
month: new Date().getMonth(),
year: new Date().getFullYear(),
hour: new Date().getHours(),
minute: new Date().getMinutes(),
second: new Date().getSeconds(),
};
showTime = true;
dateChange(args) {
console.log('dateChange:', (<DatePicker>args.object).date);
}
checkedChange(args) {
const checked = (args.object as Switch).checked;
this.showTime = checked;
console.log('this.showTime:', this.showTime);
this.notifyPropertyChange('showTime', this.showTime);
}
}

View File

@ -0,0 +1,20 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
<Page.actionBar>
<ActionBar title="DatePicker" class="action-bar">
</ActionBar>
</Page.actionBar>
<!--dateChange="{{onDateChanged}}"
year="{{displayDate?.year}}"
month="{{displayDate?.month}}"
day="{{displayDate?.day}}"
hour="{{displayDate?.hour}}"
minute="{{displayDate?.minute}}"
second="{{{{displayDate?.second}}"-->
<StackLayout padding="20">
<DatePicker class="v-center text-center" width="{{ showTime ? 220 : 300}}" height="{{ showTime ? 100 : 250}}" showTime="{{ showTime }}" iosPreferredDatePickerStyle="{{showTime ? 2 : 1}}" dateChange="{{ dateChange }}" />
<GridLayout rows="auto" columns="auto,*">
<Switch checked="true" col="0" checkedChange="{{checkedChange}}" />
<Label text="Show Time" col="1" class="m-l-10" />
</GridLayout>
</StackLayout>
</Page>

View File

@ -22,12 +22,12 @@ export class SampleData extends Observable {
const saved = results[0]; const saved = results[0];
const base64Result = results[1]; const base64Result = results[1];
if (saved) { if (saved) {
this.set('savedData', tempFile); this.notifyPropertyChange('savedData', tempFile);
console.log('ImageAsset saved', saved, tempFile); console.log('ImageAsset saved', saved, tempFile);
} }
console.log('base64', base64Result); console.log('base64', base64Result);
console.log(results[2].width, results[2].height); console.log(results[2].width, results[2].height);
this.set('resizedImage', results[2]); this.notifyPropertyChange('resizedImage', results[2]);
} catch (e) { } catch (e) {
console.log('Failed to save ImageAsset'); console.log('Failed to save ImageAsset');
} }

View File

@ -21,12 +21,12 @@ export class VisibilityVsHiddenModel extends Observable {
if (this.currentVisibilityIndex === 3) { if (this.currentVisibilityIndex === 3) {
this.currentVisibilityIndex = 0; this.currentVisibilityIndex = 0;
} }
this.set('currentVisibility', this.visibilityOptions[this.currentVisibilityIndex]); this.notifyPropertyChange('currentVisibility', this.visibilityOptions[this.currentVisibilityIndex]);
this.set('currentVisibilityType', `Current Visibility: ${this.visibilityOptions[this.currentVisibilityIndex]}`); this.notifyPropertyChange('currentVisibilityType', `Current Visibility: ${this.visibilityOptions[this.currentVisibilityIndex]}`);
} }
toggleHidden() { toggleHidden() {
this.set('currentHidden', !this.currentHidden); this.notifyPropertyChange('currentHidden', !this.currentHidden);
this.set('currentHiddenType', `Current Hidden: ${this.currentHidden}`); this.notifyPropertyChange('currentHiddenType', `Current Hidden: ${this.currentHidden}`);
} }
} }

View File

@ -17,13 +17,15 @@
"ios": { "ios": {
"executor": "@nativescript/nx:build", "executor": "@nativescript/nx:build",
"options": { "options": {
"platform": "ios" "platform": "ios",
"noHmr": true
} }
}, },
"android": { "android": {
"executor": "@nativescript/nx:build", "executor": "@nativescript/nx:build",
"options": { "options": {
"platform": "android" "platform": "android",
"noHmr": true
} }
}, },
"clean": { "clean": {

View File

@ -1,5 +1,6 @@
import { DatePicker as DatePickerDefinition } from '.'; import { DatePicker as DatePickerDefinition } from '.';
import { View, CSSType } from '../core/view'; import { View, CSSType } from '../core/view';
import { booleanConverter } from '../core/view-base';
import { Property } from '../core/properties'; import { Property } from '../core/properties';
const defaultDate = new Date(); const defaultDate = new Date();
@ -10,10 +11,14 @@ export class DatePickerBase extends View implements DatePickerDefinition {
public year: number; public year: number;
public month: number; public month: number;
public day: number; public day: number;
public hour: number;
public minute: number;
public second: number;
public maxDate: Date; public maxDate: Date;
public minDate: Date; public minDate: Date;
public date: Date; public date: Date;
public iosPreferredDatePickerStyle: number; public iosPreferredDatePickerStyle: number;
public showTime: boolean;
} }
DatePickerBase.prototype.recycleNativeView = 'auto'; DatePickerBase.prototype.recycleNativeView = 'auto';
@ -39,6 +44,27 @@ export const dayProperty = new Property<DatePickerBase, number>({
}); });
dayProperty.register(DatePickerBase); dayProperty.register(DatePickerBase);
export const hourProperty = new Property<DatePickerBase, number>({
name: 'hour',
defaultValue: defaultDate.getHours(),
valueConverter: (v) => parseInt(v),
});
hourProperty.register(DatePickerBase);
export const minuteProperty = new Property<DatePickerBase, number>({
name: 'minute',
defaultValue: defaultDate.getMinutes(),
valueConverter: (v) => parseInt(v),
});
minuteProperty.register(DatePickerBase);
export const secondProperty = new Property<DatePickerBase, number>({
name: 'second',
defaultValue: defaultDate.getSeconds(),
valueConverter: (v) => parseInt(v),
});
secondProperty.register(DatePickerBase);
// TODO: Make CoercibleProperties // TODO: Make CoercibleProperties
export const maxDateProperty = new Property<DatePickerBase, Date>({ export const maxDateProperty = new Property<DatePickerBase, Date>({
name: 'maxDate', name: 'maxDate',
@ -62,6 +88,13 @@ export const dateProperty = new Property<DatePickerBase, Date>({
}); });
dateProperty.register(DatePickerBase); dateProperty.register(DatePickerBase);
export const showTimeProperty = new Property<DatePickerBase, boolean>({
name: 'showTime',
defaultValue: false,
valueConverter: (v) => booleanConverter(v),
});
showTimeProperty.register(DatePickerBase);
export const iosPreferredDatePickerStyleProperty = new Property<DatePickerBase, number>({ export const iosPreferredDatePickerStyleProperty = new Property<DatePickerBase, number>({
name: 'iosPreferredDatePickerStyle', name: 'iosPreferredDatePickerStyle',
defaultValue: 0, defaultValue: 0,

View File

@ -1,4 +1,6 @@
import { DatePickerBase, yearProperty, monthProperty, dayProperty, dateProperty, maxDateProperty, minDateProperty } from './date-picker-common'; import { DatePickerBase, yearProperty, monthProperty, dayProperty, dateProperty, maxDateProperty, minDateProperty } from './date-picker-common';
import { TimePicker } from '../time-picker';
import { StackLayout } from '../layouts/stack-layout';
export * from './date-picker-common'; export * from './date-picker-common';
@ -40,8 +42,15 @@ function initializeDateChangedListener(): void {
dateChanged = true; dateChanged = true;
} }
if (dateChanged) { if (dateChanged || (owner.showTime && owner.timePicker)) {
dateProperty.nativeValueChange(owner, new Date(year, month, day)); let newDate: Date;
if (owner.showTime && owner.timePicker) {
const dateTime = owner.timePicker.time;
newDate = new Date(year, month, day, dateTime.getHours(), dateTime.getMinutes(), dateTime.getSeconds(), dateTime.getMilliseconds());
} else {
newDate = new Date(year, month, day);
}
dateProperty.nativeValueChange(owner, newDate);
} }
} }
} }
@ -51,6 +60,7 @@ function initializeDateChangedListener(): void {
export class DatePicker extends DatePickerBase { export class DatePicker extends DatePickerBase {
nativeViewProtected: android.widget.DatePicker; nativeViewProtected: android.widget.DatePicker;
timePicker: TimePicker;
public createNativeView() { public createNativeView() {
const picker = new android.widget.DatePicker(this._context); const picker = new android.widget.DatePicker(this._context);
@ -66,9 +76,21 @@ export class DatePicker extends DatePickerBase {
const listener = new DateChangedListener(this); const listener = new DateChangedListener(this);
nativeView.init(this.year, this.month - 1, this.day, listener); nativeView.init(this.year, this.month - 1, this.day, listener);
(<any>nativeView).listener = listener; (<any>nativeView).listener = listener;
if (this.showTime) {
this.timePicker = new TimePicker();
this.timePicker.width = this.width;
this.timePicker.height = this.height;
this.timePicker.on('timeChange', (args) => {
this.updateNativeDate();
});
(<StackLayout>this.parent).addChild(this.timePicker);
}
} }
public disposeNativeView() { public disposeNativeView() {
if (this.timePicker) {
this.timePicker.disposeNativeView();
}
(<any>this.nativeViewProtected).listener.owner = null; (<any>this.nativeViewProtected).listener.owner = null;
super.disposeNativeView(); super.disposeNativeView();
} }
@ -78,8 +100,13 @@ export class DatePicker extends DatePickerBase {
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 - 1 : nativeView.getMonth();
const day = typeof this.day === 'number' ? this.day : nativeView.getDayOfMonth(); const day = typeof this.day === 'number' ? this.day : nativeView.getDayOfMonth();
if (this.showTime && this.timePicker) {
const time = this.timePicker.time || new Date();
this.date = new Date(year, month, day, time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
} else {
this.date = new Date(year, month, day); this.date = new Date(year, month, day);
} }
}
[yearProperty.setNative](value: number) { [yearProperty.setNative](value: number) {
if (this.nativeViewProtected.getYear() !== value) { if (this.nativeViewProtected.getYear() !== value) {

View File

@ -1,4 +1,4 @@
import { DatePickerBase, yearProperty, monthProperty, dayProperty, dateProperty, maxDateProperty, minDateProperty } from './date-picker-common'; import { DatePickerBase, yearProperty, monthProperty, dayProperty, dateProperty, maxDateProperty, minDateProperty, hourProperty, minuteProperty, secondProperty, showTimeProperty, iosPreferredDatePickerStyleProperty } from './date-picker-common';
import { colorProperty } from '../styling/style-properties'; import { colorProperty } from '../styling/style-properties';
import { Color } from '../../color'; import { Color } from '../../color';
import { Device } from '../../platform'; import { Device } from '../../platform';
@ -14,7 +14,7 @@ export class DatePicker extends DatePickerBase {
public createNativeView() { public createNativeView() {
const picker = UIDatePicker.new(); const picker = UIDatePicker.new();
picker.datePickerMode = UIDatePickerMode.Date; picker.datePickerMode = this.showTime ? UIDatePickerMode.DateAndTime : UIDatePickerMode.Date;
if (SUPPORT_DATE_PICKER_STYLE) { if (SUPPORT_DATE_PICKER_STYLE) {
picker.preferredDatePickerStyle = this.iosPreferredDatePickerStyle; picker.preferredDatePickerStyle = this.iosPreferredDatePickerStyle;
} }
@ -38,27 +38,61 @@ export class DatePicker extends DatePickerBase {
return this.nativeViewProtected; return this.nativeViewProtected;
} }
[showTimeProperty.setNative](value: boolean) {
this.showTime = value;
if (this.nativeViewProtected) {
this.nativeViewProtected.datePickerMode = this.showTime ? UIDatePickerMode.DateAndTime : UIDatePickerMode.Date;
}
}
[iosPreferredDatePickerStyleProperty.setNative](value: number) {
this.iosPreferredDatePickerStyle = value;
if (this.nativeViewProtected) {
if (SUPPORT_DATE_PICKER_STYLE) {
this.nativeViewProtected.preferredDatePickerStyle = this.iosPreferredDatePickerStyle;
}
}
}
[yearProperty.setNative](value: number) { [yearProperty.setNative](value: number) {
this.date = new Date(value, this.month - 1, this.day); this.date = new Date(value, this.month - 1, this.day, this.hour || 0, this.minute || 0, this.second || 0);
} }
[monthProperty.setNative](value: number) { [monthProperty.setNative](value: number) {
this.date = new Date(this.year, value - 1, this.day); this.date = new Date(this.year, value - 1, this.day, this.hour || 0, this.minute || 0, this.second || 0);
} }
[dayProperty.setNative](value: number) { [dayProperty.setNative](value: number) {
this.date = new Date(this.year, this.month - 1, value); this.date = new Date(this.year, this.month - 1, value, this.hour || 0, this.minute || 0, this.second || 0);
}
[hourProperty.setNative](value: number) {
this.date = new Date(this.year, this.month - 1, this.day, value, this.minute || 0, this.second || 0);
}
[minuteProperty.setNative](value: number) {
this.date = new Date(this.year, this.month - 1, this.day, this.hour || 0, value, this.second || 0);
}
[secondProperty.setNative](value: number) {
this.date = new Date(this.year, this.month - 1, this.day, this.hour || 0, this.minute || 0, value);
} }
[dateProperty.setNative](value: Date) { [dateProperty.setNative](value: Date) {
const picker = this.nativeViewProtected; const picker = this.nativeViewProtected;
const comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, picker.date); const comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit | NSCalendarUnit.SecondCalendarUnit, 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();
comps.hour = value.getHours();
comps.minute = value.getMinutes();
comps.second = value.getSeconds();
this.year = comps.year; this.year = comps.year;
this.month = comps.month; this.month = comps.month;
this.day = comps.day; this.day = comps.day;
this.hour = comps.hour;
this.minute = comps.minute;
this.second = comps.second;
picker.setDateAnimated(NSCalendar.currentCalendar.dateFromComponents(comps), false); picker.setDateAnimated(NSCalendar.currentCalendar.dateFromComponents(comps), false);
} }
@ -103,13 +137,13 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
} }
public valueChanged(sender: UIDatePicker) { public valueChanged(sender: UIDatePicker) {
const comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay, sender.date);
const owner = this._owner.get(); const owner = this._owner.get();
if (!owner) { if (!owner) {
return; return;
} }
const comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit | NSCalendarUnit.SecondCalendarUnit, sender.date);
let dateChanged = false; let dateChanged = false;
if (comps.year !== owner.year) { if (comps.year !== owner.year) {
yearProperty.nativeValueChange(owner, comps.year); yearProperty.nativeValueChange(owner, comps.year);
@ -126,8 +160,23 @@ class UIDatePickerChangeHandlerImpl extends NSObject {
dateChanged = true; dateChanged = true;
} }
if (comps.hour !== owner.hour) {
hourProperty.nativeValueChange(owner, comps.hour);
dateChanged = true;
}
if (comps.minute !== owner.minute) {
minuteProperty.nativeValueChange(owner, comps.minute);
dateChanged = true;
}
if (comps.second !== owner.second) {
secondProperty.nativeValueChange(owner, comps.second);
dateChanged = true;
}
if (dateChanged) { if (dateChanged) {
dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day)); dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day, comps.hour, comps.minute, comps.second));
} }
} }