diff --git a/src/DateTimePickerModal.android.js b/src/DateTimePickerModal.android.js index 3495d1b..2add5cb 100644 --- a/src/DateTimePickerModal.android.js +++ b/src/DateTimePickerModal.android.js @@ -6,18 +6,26 @@ import DateTimePicker from "@react-native-community/datetimepicker"; const areEqual = (prevProps, nextProps) => { return ( prevProps.isVisible === nextProps.isVisible && - prevProps.date.getTime() === nextProps.date.getTime() + (prevProps.date?.getTime() === nextProps.date?.getTime()) ); }; const DateTimePickerModal = memo( - ({ date, mode, isVisible, onCancel, onConfirm, onHide, ...otherProps }) => { + ({ + date = new Date(), + mode = 'date', + 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"); + setCurrentMode(mode === 'time' ? 'time' : 'date'); } else if (!isVisible) { setCurrentMode(null); } @@ -26,18 +34,18 @@ const DateTimePickerModal = memo( if (!isVisible || !currentMode) return null; const handleChange = (event, date) => { - if (event.type === "dismissed") { + if (event.type === 'dismissed') { onCancel(); onHide(false); return; } let nextDate = date; - if (mode === "datetime") { - if (currentMode === "date") { - setCurrentMode("time"); + if (mode === 'datetime') { + if (currentMode === 'date') { + setCurrentMode('time'); currentDateRef.current = new Date(date); return; - } else if (currentMode === "time") { + } else if (currentMode === 'time') { const year = currentDateRef.current.getFullYear(); const month = currentDateRef.current.getMonth(); const day = currentDateRef.current.getDate(); @@ -62,6 +70,8 @@ const DateTimePickerModal = memo( areEqual ); +export default DateTimePickerModal; + DateTimePickerModal.propTypes = { date: PropTypes.instanceOf(Date), isVisible: PropTypes.bool, @@ -72,10 +82,4 @@ DateTimePickerModal.propTypes = { minimumDate: PropTypes.instanceOf(Date), }; -DateTimePickerModal.defaultProps = { - date: new Date(), - isVisible: false, - onHide: () => {}, -}; - -export { DateTimePickerModal }; +export { DateTimePickerModal }; \ No newline at end of file