-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathDateTimePickerAndroid.android.js
124 lines (115 loc) · 3.13 KB
/
DateTimePickerAndroid.android.js
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
/**
* @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} from './androidUtils';
import pickers from './picker';
import {
createDateTimeSetEvtParams,
createDismissEvtParams,
createNeutralEvtParams,
} from './eventCreators';
import {processColor} from 'react-native';
function open(props: AndroidNativeProps) {
const {
mode = ANDROID_MODE.date,
display,
value: originalValue,
is24Hour,
minimumDate,
maximumDate,
minuteInterval,
timeZoneOffsetInMinutes,
timeZoneName,
onChange,
onError,
positiveButton,
negativeButton,
neutralButton,
neutralButtonLabel,
positiveButtonLabel,
negativeButtonLabel,
testID,
firstDayOfWeek,
} = props;
validateAndroidProps(props);
invariant(originalValue, 'A date or time must be specified as `value` prop.');
const valueTimestamp = originalValue.getTime();
const openPicker = getOpenPicker(mode);
const presentPicker = async () => {
try {
const dialogButtons = {
positive: {
label: positiveButtonLabel,
...positiveButton,
textColor: processColor(positiveButton?.textColor),
},
neutral: {
label: neutralButtonLabel,
...neutralButton,
textColor: processColor(neutralButton?.textColor),
},
negative: {
label: negativeButtonLabel,
...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,
});
switch (action) {
case DATE_SET_ACTION:
case TIME_SET_ACTION: {
const date = new Date(timestamp);
const [event] = createDateTimeSetEvtParams(date, utcOffset);
onChange?.(event, date);
break;
}
case NEUTRAL_BUTTON_ACTION: {
const [event] = createNeutralEvtParams(originalValue, utcOffset);
onChange?.(event, originalValue);
break;
}
case DISMISS_ACTION:
default: {
const [event] = createDismissEvtParams(originalValue, utcOffset);
onChange?.(event, originalValue);
break;
}
}
} catch (error) {
onError && onError(error);
}
};
presentPicker();
}
function dismiss(mode: AndroidNativeProps['mode']): Promise<boolean> {
// $FlowFixMe - `AbstractComponent` [1] is not an instance type.
return pickers[mode]?.dismiss();
}
export const DateTimePickerAndroid = {open, dismiss};