-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathDateTimePickerModal.android.js
More file actions
83 lines (75 loc) · 2.25 KB
/
DateTimePickerModal.android.js
File metadata and controls
83 lines (75 loc) · 2.25 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
import React, { useEffect, useRef, useState, memo } from "react";
import PropTypes from "prop-types";
import DateTimePicker from "@react-native-community/datetimepicker";
// Memo workaround for https://github.com/react-native-community/datetimepicker/issues/54
const areEqual = (prevProps, nextProps) => {
return (
prevProps.isVisible === nextProps.isVisible &&
prevProps.date?.getTime() === nextProps.date?.getTime()
);
};
const DateTimePickerModal = memo(
({
date = new Date(),
mode,
isVisible = false,
onCancel,
onConfirm,
onHide = () => {},
...otherProps
}) => {
const currentDateRef = useRef(date);
const [currentMode, setCurrentMode] = useState(null);
useEffect(() => {
if (isVisible && currentMode === null) {
setCurrentMode(mode === "time" ? "time" : "date");
} else if (!isVisible) {
setCurrentMode(null);
}
}, [isVisible, currentMode, mode]);
if (!isVisible || !currentMode) return null;
const handleChange = (event, date) => {
if (event.type === "dismissed") {
onCancel();
onHide(false);
return;
}
let nextDate = date;
if (mode === "datetime") {
if (currentMode === "date") {
setCurrentMode("time");
currentDateRef.current = new Date(date);
return;
} else if (currentMode === "time") {
const year = currentDateRef.current.getFullYear();
const month = currentDateRef.current.getMonth();
const day = currentDateRef.current.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
nextDate = new Date(year, month, day, hours, minutes);
}
}
onConfirm(nextDate);
onHide(true, nextDate);
};
return (
<DateTimePicker
{...otherProps}
mode={currentMode}
value={date}
onChange={handleChange}
/>
);
},
areEqual
);
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),
};
export { DateTimePickerModal };