-
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathDateTimePickerAndroid.android.js
More file actions
152 lines (143 loc) · 3.75 KB
/
DateTimePickerAndroid.android.js
File metadata and controls
152 lines (143 loc) · 3.75 KB
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
/**
* @format
* @flow strict-local
*/
import {
DATE_SET_ACTION,
TIME_SET_ACTION,
DISMISS_ACTION,
NEUTRAL_BUTTON_ACTION,
ANDROID_DISPLAY,
ANDROID_MODE,
} from './constants';
import invariant from 'invariant';
import type {AndroidNativeProps} from './types';
import {
getOpenPicker,
validateAndroidProps,
materialPickers,
} from './androidUtils';
import defaultPickers from './picker';
import {
createDateTimeSetEvtParams,
createDismissEvtParams,
createNeutralEvtParams,
} from './eventCreators';
import {processColor} from 'react-native';
import {warnIfOnChangeIsUsed} from './utils';
function open(props: AndroidNativeProps) {
const {
mode = ANDROID_MODE.date,
display,
value: originalValue,
is24Hour,
minimumDate,
maximumDate,
minuteInterval,
timeZoneOffsetInMinutes,
timeZoneName,
onChange,
onValueChange,
onDismiss,
onNeutralButtonPress,
onError,
positiveButton,
negativeButton,
neutralButton,
testID,
firstDayOfWeek,
title,
initialInputMode,
design,
fullscreen,
startOnYearSelection,
} = props;
validateAndroidProps(props);
warnIfOnChangeIsUsed(onChange);
invariant(originalValue, 'A date or time must be specified as `value` prop.');
const valueTimestamp = originalValue.getTime();
const openPicker = getOpenPicker(mode, design);
const presentPicker = async () => {
try {
const dialogButtons = {
positive: {
...positiveButton,
textColor: processColor(positiveButton?.textColor),
},
neutral: {
...neutralButton,
textColor: processColor(neutralButton?.textColor),
},
negative: {
...negativeButton,
textColor: processColor(negativeButton?.textColor),
},
};
const displayOverride =
display === ANDROID_DISPLAY.spinner
? ANDROID_DISPLAY.spinner
: ANDROID_DISPLAY.default;
const {action, timestamp, utcOffset} = await openPicker({
value: valueTimestamp,
display: displayOverride,
is24Hour,
minimumDate,
maximumDate,
minuteInterval,
timeZoneOffsetInMinutes,
timeZoneName,
dialogButtons,
testID,
firstDayOfWeek,
title,
initialInputMode,
fullscreen,
startOnYearSelection,
});
switch (action) {
case DATE_SET_ACTION:
case TIME_SET_ACTION: {
const date = new Date(timestamp);
if (onValueChange) {
onValueChange({nativeEvent: {timestamp, utcOffset}}, date);
} else {
const [event] = createDateTimeSetEvtParams(date, utcOffset);
onChange?.(event, date);
}
break;
}
case NEUTRAL_BUTTON_ACTION: {
if (onNeutralButtonPress) {
onNeutralButtonPress();
} else {
const [event] = createNeutralEvtParams(originalValue, utcOffset);
onChange?.(event, originalValue);
}
break;
}
case DISMISS_ACTION:
default: {
if (onDismiss) {
onDismiss();
} else {
const [event] = createDismissEvtParams(originalValue, utcOffset);
onChange?.(event, originalValue);
}
break;
}
}
} catch (error) {
onError && onError(error);
}
};
presentPicker();
}
function dismiss(
mode: AndroidNativeProps['mode'],
design?: AndroidNativeProps['design'] = 'default',
): Promise<boolean> {
const pickers = design === 'material' ? materialPickers : defaultPickers;
// $FlowFixMe - `AbstractComponent` [1] is not an instance type.
return pickers[mode].dismiss();
}
export const DateTimePickerAndroid = {open, dismiss};