-
Notifications
You must be signed in to change notification settings - Fork 388
/
DateTimePickerModal.android.js
80 lines (73 loc) · 2.27 KB
/
DateTimePickerModal.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
import React, { useEffect, useState, memo } from "react";
import PropTypes from "prop-types";
import DateTimePicker from "@react-native-community/datetimepicker";
const DateTimePickerModal = memo(({
date,
mode,
isVisible,
onCancel,
onConfirm,
onHide,
...otherProps
}) =>{
const [currentDate, setCurrentDate] = useState(date);
const [currentMode, setCurrentMode] = useState(null);
useEffect(()=>{
if (isVisible && currentMode === null) {
setCurrentMode(mode === "time" ? "time" : "date");
} else if (!isVisible) {
setCurrentMode(null);
}
}, [isVisible, currentMode]);
if (!isVisible || !currentMode) return null;
return (
<DateTimePicker
{...otherProps}
mode={currentMode}
value={date}
onChange={(event, date) => {
if (event.type === "dismissed") {
onCancel();
onHide(false);
return;
}
let nextDate = date;
if (mode === "datetime") {
if (currentMode === "date") {
setCurrentMode("time")
setCurrentDate(new Date(date));
return;
} else if (currentMode === "time") {
const year = currentDate.getFullYear();
const month = currentDate.getMonth();
const day = currentDate.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
nextDate = new Date(year, month, day, hours, minutes);
}
}
onConfirm(nextDate);
onHide(true, nextDate);
}}
/>
);
},
(prevProps, nextProps) =>
prevProps.isVisible === nextProps.isVisible
&& prevProps.date === nextProps.date
);
DateTimePickerModal.propTypes = {
date: PropTypes.instanceOf(Date),
isVisible: PropTypes.bool,
onCancel: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired,
onHide: PropTypes.func,
maximumDate: PropTypes.instanceOf(Date),
minimumDate: PropTypes.instanceOf(Date),
};
DateTimePickerModal.defaultProps = {
date: new Date(),
isVisible: false,
onHide: () => {},
};
export {DateTimePickerModal};