-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
RNDateTimePickerManager.m
157 lines (130 loc) · 5.04 KB
/
RNDateTimePickerManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNDateTimePickerManager.h"
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import "RNDateTimePicker.h"
#import <React/UIView+React.h>
@implementation RCTConvert(UIDatePicker)
RCT_ENUM_CONVERTER(UIDatePickerMode, (@{
@"time": @(UIDatePickerModeTime),
@"date": @(UIDatePickerModeDate),
@"datetime": @(UIDatePickerModeDateAndTime),
}), UIDatePickerModeTime, integerValue)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
typedef UIDatePickerStyle RNCUIDatePickerStyle;
// using UIDatePickerStyle directly conflicts with RN implementation
RCT_ENUM_CONVERTER(RNCUIDatePickerStyle, (@{
@"default": @(UIDatePickerStyleAutomatic),
@"compact": @(UIDatePickerStyleCompact),
@"spinner": @(UIDatePickerStyleWheels),
@"inline": @(UIDatePickerStyleInline),
}), UIDatePickerStyleAutomatic, integerValue)
RCT_ENUM_CONVERTER(UIUserInterfaceStyle, (@{
@"dark": @(UIUserInterfaceStyleDark),
@"light": @(UIUserInterfaceStyleLight),
}), UIUserInterfaceStyleUnspecified, integerValue)
#endif
#pragma clang diagnostic pop
@end
@implementation RNDateTimePickerManager
RCT_EXPORT_MODULE()
- (UIView *)view
{
return [RNDateTimePicker new];
}
+ (NSString*) datepickerStyleToString: (UIDatePickerStyle) style API_AVAILABLE(ios(13.4)){
// RCTConvert does not handle this.?
switch (style) {
case UIDatePickerStyleCompact:
return @"compact";
case UIDatePickerStyleWheels:
return @"spinner";
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
case UIDatePickerStyleInline:
return @"inline";
#endif
default:
[NSException raise:@"Unsupported style value" format:@"UIDatePickerStyle of %ld is unsupported", (long)style];
return @"";
}
}
RCT_EXPORT_METHOD(getDefaultDisplayValue:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (@available(iOS 13.4, *)) {
UIDatePicker* view = [RNDateTimePicker new];
view.preferredDatePickerStyle = UIDatePickerStyleAutomatic;
UIDatePickerMode renderedMode = [RCTConvert UIDatePickerMode:options[@"mode"]];
view.datePickerMode = renderedMode;
// NOTE afaict we do not need to measure the actual dimensions here, but if we do, just look at the original PR
UIDatePickerStyle determinedDisplayValue = view.datePickerStyle;
resolve(@{
@"determinedDisplayValue": [RNDateTimePickerManager datepickerStyleToString:determinedDisplayValue],
});
} else {
// never happens; the condition is just to avoid compiler warnings
reject(@"UNEXPECTED_CALL", @"unexpected getDefaultDisplayValue() call", nil);
}
});
}
RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate)
RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate)
RCT_EXPORT_VIEW_PROPERTY(minuteInterval, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_REMAP_VIEW_PROPERTY(mode, datePickerMode, UIDatePickerMode)
RCT_REMAP_VIEW_PROPERTY(timeZoneOffsetInMinutes, timeZone, NSTimeZone)
RCT_CUSTOM_VIEW_PROPERTY(themeVariant, UIUserInterfaceStyle, RNDateTimePicker) {
if (@available(iOS 13.0, *)) {
if (json) {
UIUserInterfaceStyle propValue = [RCTConvert UIUserInterfaceStyle:json];
view.overrideUserInterfaceStyle = propValue;
} else {
view.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
}
}
}
RCT_CUSTOM_VIEW_PROPERTY(textColor, UIColor, RNDateTimePicker)
{
if (@available(iOS 14.0, *)) {
if (view.datePickerStyle != UIDatePickerStyleWheels) {
// prevents #247
return;
}
}
if (json) {
[view setValue:[RCTConvert UIColor:json] forKey:@"textColor"];
[view setValue:@(NO) forKey:@"highlightsToday"];
} else {
UIColor* defaultColor;
if (@available(iOS 13.0, *)) {
defaultColor = [UIColor labelColor];
} else {
defaultColor = [UIColor blackColor];
}
[view setValue:defaultColor forKey:@"textColor"];
[view setValue:@(YES) forKey:@"highlightsToday"];
}
}
// TODO vonovak setting preferredDatePickerStyle invalidates minuteinterval
RCT_CUSTOM_VIEW_PROPERTY(displayIOS, RNCUIDatePickerStyle, RNDateTimePicker)
{
// officially since 13.4 (https://developer.apple.com/documentation/uikit/uidatepickerstyle?language=objc) but practically since 14
if (@available(iOS 14.0, *)) {
if (json) {
UIDatePickerStyle propValue = [RCTConvert RNCUIDatePickerStyle:json];
view.preferredDatePickerStyle = propValue;
} else {
view.preferredDatePickerStyle = UIDatePickerStyleAutomatic;
}
}
}
@end