@@ -4,6 +4,7 @@ import { ListHeader } from "../list-header/list-header";
44import ReactDatePicker from "react-datepicker" ;
55
66import ro from "date-fns/locale/ro" ;
7+ import { setHours , setMinutes , isSameDay } from "date-fns" ;
78
89import "react-datepicker/dist/react-datepicker.css" ;
910import "./datePicker.css" ;
@@ -18,6 +19,8 @@ export const DatePicker = ({
1819 currentResponse ? currentResponse : null
1920 ) ;
2021
22+ const maxDate = ! question . allowFuture ? new Date ( ) : null ;
23+
2124 const onChange = date => {
2225 setStartDate ( date ) ;
2326 const answer = {
@@ -29,9 +32,21 @@ export const DatePicker = ({
2932
3033 const getDatePicker = ( ) => {
3134 if ( withTime ) {
32- return < DateTimePicker startDate = { startDate } onChange = { onChange } /> ;
35+ return (
36+ < DateTimePicker
37+ startDate = { startDate }
38+ maxDate = { maxDate }
39+ onChange = { onChange }
40+ />
41+ ) ;
3342 } else {
34- return < DateOnlyPicker startDate = { startDate } onChange = { onChange } /> ;
43+ return (
44+ < DateOnlyPicker
45+ startDate = { startDate }
46+ maxDate = { maxDate }
47+ onChange = { onChange }
48+ />
49+ ) ;
3550 }
3651 } ;
3752
@@ -43,7 +58,7 @@ export const DatePicker = ({
4358 ) ;
4459} ;
4560
46- const DateOnlyPicker = ( { startDate, onChange } ) => {
61+ const DateOnlyPicker = ( { startDate, maxDate , onChange } ) => {
4762 return (
4863 < ReactDatePicker
4964 customInput = { < CustomInput /> }
@@ -52,23 +67,60 @@ const DateOnlyPicker = ({ startDate, onChange }) => {
5267 onChange = { onChange }
5368 locale = { ro }
5469 dateFormat = { "d MMMM yyyy" }
70+ maxDate = { maxDate }
5571 />
5672 ) ;
5773} ;
5874
59- const DateTimePicker = ( { startDate, onChange } ) => {
75+ const DateTimePicker = ( { startDate, maxDate, onChange } ) => {
76+ const computeMinTime = date => {
77+ if ( ! maxDate ) {
78+ return ;
79+ }
80+
81+ if ( isSameDay ( date , maxDate ) ) {
82+ return setHours ( setMinutes ( new Date ( ) , 0 ) , 0 ) ;
83+ }
84+ } ;
85+
86+ const computeMaxTime = date => {
87+ if ( ! maxDate ) {
88+ return ;
89+ }
90+
91+ if ( isSameDay ( date , maxDate ) ) {
92+ return maxDate ;
93+ }
94+ } ;
95+
96+ const [ minTime , setMinTime ] = useState (
97+ computeMinTime ( startDate || new Date ( ) )
98+ ) ;
99+ const [ maxTime , setMaxTime ] = useState (
100+ computeMaxTime ( startDate || new Date ( ) )
101+ ) ;
102+
103+ const handleDatetimeChange = date => {
104+ setMinTime ( computeMinTime ( date ) ) ;
105+ setMaxTime ( computeMaxTime ( date ) ) ;
106+ onChange ( date ) ;
107+ } ;
108+
60109 return (
61110 < ReactDatePicker
62111 customInput = { < CustomInput /> }
63112 placeholderText = { "Introdu data si ora" }
64113 selected = { startDate }
65- onChange = { onChange }
114+ onChange = { handleDatetimeChange }
66115 locale = { ro }
67116 dateFormat = { "d MMMM yyyy HH:mm" }
68117 showTimeSelect
69118 timeFormat = "HH:mm"
70119 timeIntervals = { 15 }
71120 timeCaption = "Ora"
121+ maxDate = { maxDate }
122+ minTime = { minTime }
123+ maxTime = { maxTime }
72124 />
73125 ) ;
74126} ;
@@ -97,18 +149,21 @@ DatePicker.propTypes = {
97149 withTime : PropTypes . bool ,
98150 question : PropTypes . shape ( {
99151 questionId : PropTypes . number . isRequired ,
100- questionText : PropTypes . string . isRequired
152+ questionText : PropTypes . string . isRequired ,
153+ allowFuture : PropTypes . bool
101154 } ) ,
102155 onAnswer : PropTypes . func ,
103156 currentResponse : PropTypes . object
104157} ;
105158
106159DateTimePicker . propTypes = {
107160 startDate : PropTypes . object ,
161+ maxDate : PropTypes . instanceOf ( Date ) ,
108162 onChange : PropTypes . func . isRequired
109163} ;
110164
111165DateOnlyPicker . propTypes = {
112166 startDate : PropTypes . object ,
167+ maxDate : PropTypes . instanceOf ( Date ) ,
113168 onChange : PropTypes . func . isRequired
114169} ;
0 commit comments