forked from react-native-datetimepicker/datetimepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroidUtils.js
107 lines (101 loc) · 2.93 KB
/
androidUtils.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
/**
* @format
* @flow strict-local
*/
import {ANDROID_DISPLAY, ANDROID_MODE} from './constants';
import pickers from './picker';
import type {AndroidNativeProps, DateTimePickerResult} from './types';
import {sharedPropsValidation} from './utils';
import invariant from 'invariant';
import {processColor} from 'react-native';
type Timestamp = number;
type ProcessedButton = {
title: string,
textColor: $Call<typeof processColor>,
};
type OpenParams = {
value: Timestamp,
display: AndroidNativeProps['display'],
is24Hour: AndroidNativeProps['is24Hour'],
minimumDate: AndroidNativeProps['minimumDate'],
maximumDate: AndroidNativeProps['maximumDate'],
minuteInterval: AndroidNativeProps['minuteInterval'],
timeZoneOffsetInMinutes: AndroidNativeProps['timeZoneOffsetInMinutes'],
timeZoneName: AndroidNativeProps['timeZoneName'],
testID: AndroidNativeProps['testID'],
dialogButtons: {
positive: ProcessedButton,
negative: ProcessedButton,
neutral: ProcessedButton,
},
};
export type PresentPickerCallback =
(OpenParams) => Promise<DateTimePickerResult>;
function getOpenPicker(
mode: AndroidNativeProps['mode'],
): PresentPickerCallback {
switch (mode) {
case ANDROID_MODE.time:
return ({
value,
display,
is24Hour,
minuteInterval,
timeZoneOffsetInMinutes,
timeZoneName,
dialogButtons,
}: OpenParams) =>
// $FlowFixMe - `AbstractComponent` [1] is not an instance type.
pickers[mode].open({
value,
display,
minuteInterval,
is24Hour,
timeZoneOffsetInMinutes,
timeZoneName,
dialogButtons,
});
default:
return ({
value,
display,
minimumDate,
maximumDate,
timeZoneOffsetInMinutes,
timeZoneName,
dialogButtons,
testID,
}: OpenParams) =>
// $FlowFixMe - `AbstractComponent` [1] is not an instance type.
pickers[ANDROID_MODE.date].open({
value,
display,
minimumDate,
maximumDate,
timeZoneOffsetInMinutes,
timeZoneName,
dialogButtons,
testID,
});
}
}
function validateAndroidProps(props: AndroidNativeProps) {
sharedPropsValidation({value: props?.value});
const {mode, display} = props;
invariant(
!(display === ANDROID_DISPLAY.calendar && mode === ANDROID_MODE.time) &&
!(display === ANDROID_DISPLAY.clock && mode === ANDROID_MODE.date),
`display: ${display} and mode: ${mode} cannot be used together.`,
);
if (
props?.positiveButtonLabel !== undefined ||
props?.negativeButtonLabel !== undefined ||
props?.neutralButtonLabel !== undefined
) {
console.warn(
'positiveButtonLabel, negativeButtonLabel and neutralButtonLabel are deprecated.' +
'Use positive / negative / neutralButton prop instead.',
);
}
}
export {getOpenPicker, validateAndroidProps};