UNPKG

7.52 kBJavaScriptView Raw
1import { DatePickerBase, yearProperty, monthProperty, dayProperty, dateProperty, maxDateProperty, minDateProperty, hourProperty, minuteProperty, secondProperty, showTimeProperty, iosPreferredDatePickerStyleProperty } from './date-picker-common';
2import { colorProperty } from '../styling/style-properties';
3import { Color } from '../../color';
4import { Device } from '../../platform';
5export * from './date-picker-common';
6const SUPPORT_DATE_PICKER_STYLE = parseFloat(Device.osVersion) >= 13.4;
7const SUPPORT_TEXT_COLOR = parseFloat(Device.osVersion) < 14.0;
8export class DatePicker extends DatePickerBase {
9 createNativeView() {
10 const picker = UIDatePicker.new();
11 picker.datePickerMode = this.showTime ? 2 /* UIDatePickerMode.DateAndTime */ : 1 /* UIDatePickerMode.Date */;
12 if (SUPPORT_DATE_PICKER_STYLE) {
13 picker.preferredDatePickerStyle = this.iosPreferredDatePickerStyle;
14 }
15 return picker;
16 }
17 initNativeView() {
18 super.initNativeView();
19 const nativeView = this.nativeViewProtected;
20 this._changeHandler = UIDatePickerChangeHandlerImpl.initWithOwner(new WeakRef(this));
21 nativeView.addTargetActionForControlEvents(this._changeHandler, 'valueChanged', 4096 /* UIControlEvents.ValueChanged */);
22 }
23 disposeNativeView() {
24 this._changeHandler = null;
25 super.disposeNativeView();
26 }
27 // @ts-ignore
28 get ios() {
29 return this.nativeViewProtected;
30 }
31 [showTimeProperty.setNative](value) {
32 this.showTime = value;
33 if (this.nativeViewProtected) {
34 this.nativeViewProtected.datePickerMode = this.showTime ? 2 /* UIDatePickerMode.DateAndTime */ : 1 /* UIDatePickerMode.Date */;
35 }
36 }
37 [iosPreferredDatePickerStyleProperty.setNative](value) {
38 this.iosPreferredDatePickerStyle = value;
39 if (this.nativeViewProtected) {
40 if (SUPPORT_DATE_PICKER_STYLE) {
41 this.nativeViewProtected.preferredDatePickerStyle = this.iosPreferredDatePickerStyle;
42 }
43 }
44 }
45 [yearProperty.setNative](value) {
46 this.date = new Date(value, this.month - 1, this.day, this.hour || 0, this.minute || 0, this.second || 0);
47 }
48 [monthProperty.setNative](value) {
49 this.date = new Date(this.year, value - 1, this.day, this.hour || 0, this.minute || 0, this.second || 0);
50 }
51 [dayProperty.setNative](value) {
52 this.date = new Date(this.year, this.month - 1, value, this.hour || 0, this.minute || 0, this.second || 0);
53 }
54 [hourProperty.setNative](value) {
55 this.date = new Date(this.year, this.month - 1, this.day, value, this.minute || 0, this.second || 0);
56 }
57 [minuteProperty.setNative](value) {
58 this.date = new Date(this.year, this.month - 1, this.day, this.hour || 0, value, this.second || 0);
59 }
60 [secondProperty.setNative](value) {
61 this.date = new Date(this.year, this.month - 1, this.day, this.hour || 0, this.minute || 0, value);
62 }
63 [dateProperty.setNative](value) {
64 if (value) {
65 const comps = NSCalendar.currentCalendar.componentsFromDate(4 /* NSCalendarUnit.CalendarUnitYear */ | 8 /* NSCalendarUnit.CalendarUnitMonth */ | 16 /* NSCalendarUnit.CalendarUnitDay */ | 32 /* NSCalendarUnit.HourCalendarUnit */ | 64 /* NSCalendarUnit.MinuteCalendarUnit */ | 128 /* NSCalendarUnit.SecondCalendarUnit */, this.nativeViewProtected.date);
66 comps.year = value.getFullYear();
67 comps.month = value.getMonth() + 1;
68 comps.day = value.getDate();
69 comps.hour = value.getHours();
70 comps.minute = value.getMinutes();
71 comps.second = value.getSeconds();
72 this.year = comps.year;
73 this.month = comps.month;
74 this.day = comps.day;
75 this.hour = comps.hour;
76 this.minute = comps.minute;
77 this.second = comps.second;
78 this.nativeViewProtected.setDateAnimated(NSCalendar.currentCalendar.dateFromComponents(comps), false);
79 }
80 }
81 [maxDateProperty.getDefault]() {
82 return this.nativeViewProtected.maximumDate;
83 }
84 [maxDateProperty.setNative](value) {
85 if (value) {
86 const nsDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
87 this.nativeViewProtected.maximumDate = nsDate;
88 }
89 else {
90 this.nativeViewProtected.maximumDate = null;
91 }
92 }
93 [minDateProperty.getDefault]() {
94 return this.nativeViewProtected.minimumDate;
95 }
96 [minDateProperty.setNative](value) {
97 if (value) {
98 this.nativeViewProtected.minimumDate = NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
99 }
100 else {
101 this.nativeViewProtected.minimumDate = null;
102 }
103 }
104 [colorProperty.getDefault]() {
105 return SUPPORT_TEXT_COLOR ? this.nativeViewProtected.valueForKey('textColor') : UIColor.new();
106 }
107 [colorProperty.setNative](value) {
108 if (SUPPORT_TEXT_COLOR) {
109 const picker = this.nativeViewProtected;
110 picker.setValueForKey(value instanceof Color ? value.ios : value, 'textColor');
111 }
112 }
113}
114var UIDatePickerChangeHandlerImpl = /** @class */ (function (_super) {
115 __extends(UIDatePickerChangeHandlerImpl, _super);
116 function UIDatePickerChangeHandlerImpl() {
117 return _super !== null && _super.apply(this, arguments) || this;
118 }
119 UIDatePickerChangeHandlerImpl.initWithOwner = function (owner) {
120 var impl = UIDatePickerChangeHandlerImpl.new();
121 impl._owner = owner;
122 return impl;
123 };
124 UIDatePickerChangeHandlerImpl.prototype.valueChanged = function (sender) {
125 var _a;
126 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
127 if (!owner) {
128 return;
129 }
130 var comps = NSCalendar.currentCalendar.componentsFromDate(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit | NSCalendarUnit.SecondCalendarUnit, sender.date);
131 var dateChanged = false;
132 if (comps.year !== owner.year) {
133 yearProperty.nativeValueChange(owner, comps.year);
134 dateChanged = true;
135 }
136 if (comps.month !== owner.month) {
137 monthProperty.nativeValueChange(owner, comps.month);
138 dateChanged = true;
139 }
140 if (comps.day !== owner.day) {
141 dayProperty.nativeValueChange(owner, comps.day);
142 dateChanged = true;
143 }
144 if (comps.hour !== owner.hour) {
145 hourProperty.nativeValueChange(owner, comps.hour);
146 dateChanged = true;
147 }
148 if (comps.minute !== owner.minute) {
149 minuteProperty.nativeValueChange(owner, comps.minute);
150 dateChanged = true;
151 }
152 if (comps.second !== owner.second) {
153 secondProperty.nativeValueChange(owner, comps.second);
154 dateChanged = true;
155 }
156 if (dateChanged) {
157 dateProperty.nativeValueChange(owner, new Date(comps.year, comps.month - 1, comps.day, comps.hour, comps.minute, comps.second));
158 }
159 };
160 UIDatePickerChangeHandlerImpl.ObjCExposedMethods = {
161 valueChanged: { returns: interop.types.void, params: [UIDatePicker] },
162 };
163 return UIDatePickerChangeHandlerImpl;
164}(NSObject));
165//# sourceMappingURL=index.ios.js.map
\No newline at end of file