Skip to content

Commit 0819c90

Browse files
author
Nicu
authored
feat(date-picker-form): Implemented not allowing future dates by default (#171)
1 parent 79c4451 commit 0819c90

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@code4ro/taskforce-fe-components",
3-
"version": "1.0.17",
3+
"version": "1.0.18",
44
"private": false,
55
"dependencies": {
66
"bulma": "^0.8.0",

src/components/form/data/datePickers.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@ export default {
1010
},
1111
{
1212
questionId: 2,
13+
questionText: "De la ce data re-incepi lucrul?",
14+
type: "DATE_PICKER",
15+
allowFuture: true
16+
},
17+
{
18+
questionId: 3,
1319
questionText: "La ce data si ora ai iesit afara?",
1420
type: "DATE_TIME_PICKER"
1521
},
1622
{
17-
questionId: 3,
23+
questionId: 4,
24+
questionText: "La ce data si ora esti programat la medic?",
25+
type: "DATE_TIME_PICKER",
26+
allowFuture: true
27+
},
28+
{
29+
questionId: 5,
1830
type: "FINAL",
1931
questionText: "Ce trebuie sa faci?",
2032
options: [

src/components/form/datePicker.js

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ListHeader } from "../list-header/list-header";
44
import ReactDatePicker from "react-datepicker";
55

66
import ro from "date-fns/locale/ro";
7+
import { setHours, setMinutes, isSameDay } from "date-fns";
78

89
import "react-datepicker/dist/react-datepicker.css";
910
import "./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

106159
DateTimePicker.propTypes = {
107160
startDate: PropTypes.object,
161+
maxDate: PropTypes.instanceOf(Date),
108162
onChange: PropTypes.func.isRequired
109163
};
110164

111165
DateOnlyPicker.propTypes = {
112166
startDate: PropTypes.object,
167+
maxDate: PropTypes.instanceOf(Date),
113168
onChange: PropTypes.func.isRequired
114169
};

0 commit comments

Comments
 (0)