@@ -2,63 +2,58 @@ import React, { useEffect, useState, memo } from "react";
22import PropTypes from "prop-types" ;
33import DateTimePicker from "@react-native-community/datetimepicker" ;
44
5- const DateTimePickerModal = memo ( ( {
6- date,
7- mode,
8- isVisible,
9- onCancel,
10- onConfirm,
11- onHide,
12- ...otherProps
13- } ) => {
14- const [ currentDate , setCurrentDate ] = useState ( date ) ;
15- const [ currentMode , setCurrentMode ] = useState ( null ) ;
5+ const DateTimePickerModal = memo (
6+ ( { date, mode, isVisible, onCancel, onConfirm, onHide, ...otherProps } ) => {
7+ const [ currentDate , setCurrentDate ] = useState ( date ) ;
8+ const [ currentMode , setCurrentMode ] = useState ( null ) ;
169
17- useEffect ( ( ) => {
18- if ( isVisible && currentMode === null ) {
19- setCurrentMode ( mode === "time" ? "time" : "date" ) ;
20- } else if ( ! isVisible ) {
21- setCurrentMode ( null ) ;
22- }
23- } , [ isVisible , currentMode ] ) ;
10+ useEffect ( ( ) => {
11+ if ( isVisible && currentMode === null ) {
12+ setCurrentMode ( mode === "time" ? "time" : "date" ) ;
13+ } else if ( ! isVisible ) {
14+ setCurrentMode ( null ) ;
15+ }
16+ } , [ isVisible , currentMode , mode ] ) ;
17+
18+ if ( ! isVisible || ! currentMode ) return null ;
2419
25- if ( ! isVisible || ! currentMode ) return null ;
20+ const handleChange = ( event , date ) => {
21+ if ( event . type === "dismissed" ) {
22+ onCancel ( ) ;
23+ onHide ( false ) ;
24+ return ;
25+ }
26+ let nextDate = date ;
27+ if ( mode === "datetime" ) {
28+ if ( currentMode === "date" ) {
29+ setCurrentMode ( "time" ) ;
30+ setCurrentDate ( new Date ( date ) ) ;
31+ return ;
32+ } else if ( currentMode === "time" ) {
33+ const year = currentDate . getFullYear ( ) ;
34+ const month = currentDate . getMonth ( ) ;
35+ const day = currentDate . getDate ( ) ;
36+ const hours = date . getHours ( ) ;
37+ const minutes = date . getMinutes ( ) ;
38+ nextDate = new Date ( year , month , day , hours , minutes ) ;
39+ }
40+ }
41+ onConfirm ( nextDate ) ;
42+ onHide ( true , nextDate ) ;
43+ } ;
2644
27- return (
28- < DateTimePicker
29- { ...otherProps }
30- mode = { currentMode }
31- value = { date }
32- onChange = { ( event , date ) => {
33- if ( event . type === "dismissed" ) {
34- onCancel ( ) ;
35- onHide ( false ) ;
36- return ;
37- }
38- let nextDate = date ;
39- if ( mode === "datetime" ) {
40- if ( currentMode === "date" ) {
41- setCurrentMode ( "time" )
42- setCurrentDate ( new Date ( date ) ) ;
43- return ;
44- } else if ( currentMode === "time" ) {
45- const year = currentDate . getFullYear ( ) ;
46- const month = currentDate . getMonth ( ) ;
47- const day = currentDate . getDate ( ) ;
48- const hours = date . getHours ( ) ;
49- const minutes = date . getMinutes ( ) ;
50- nextDate = new Date ( year , month , day , hours , minutes ) ;
51- }
52- }
53- onConfirm ( nextDate ) ;
54- onHide ( true , nextDate ) ;
55- } }
56- />
57- ) ;
58- } ,
45+ return (
46+ < DateTimePicker
47+ { ...otherProps }
48+ mode = { currentMode }
49+ value = { date }
50+ onChange = { handleChange }
51+ />
52+ ) ;
53+ } ,
5954 ( prevProps , nextProps ) =>
60- prevProps . isVisible === nextProps . isVisible
61- && prevProps . date === nextProps . date
55+ prevProps . isVisible === nextProps . isVisible &&
56+ prevProps . date === nextProps . date
6257) ;
6358
6459DateTimePickerModal . propTypes = {
@@ -77,4 +72,4 @@ DateTimePickerModal.defaultProps = {
7772 onHide : ( ) => { } ,
7873} ;
7974
80- export { DateTimePickerModal } ;
75+ export { DateTimePickerModal } ;
0 commit comments