Skip to content
This repository was archived by the owner on Aug 30, 2019. It is now read-only.

Commit 7b4fbfa

Browse files
author
Ricardo Machado
committed
Adds a function processProps() to be used by both the constructor and the componentWillReceiveProps() from the Calendar component
1 parent 19338d7 commit 7b4fbfa

File tree

1 file changed

+61
-42
lines changed

1 file changed

+61
-42
lines changed

components/calendar/calendar.js

+61-42
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,76 @@ const {
99
CALENDAR_SELECTION_TYPE_RANGE,
1010
} = calendarConstants;
1111

12-
export default class Calendar extends Component {
13-
constructor(props) {
14-
super();
1512

16-
const { initialDates, maxDate, minDate, selectionType } = props;
17-
const maxLimit = maxDate ? normalizeDate(new Date(maxDate), 23, 59, 59, 999) : null;
18-
const renderDate = normalizeDate(((initialDates && initialDates.length) ? new Date(initialDates[0]) : new Date()));
13+
/**
14+
* Processes the given props and the existing state and returns
15+
* a new state.
16+
*
17+
* @function processProps
18+
* @param {Object} props Props to base the new state on.
19+
* @param {Object} state (Existing) state to be based on for the existing values.
20+
* @return {Object} New state to be set/used.
21+
* @static
22+
*/
23+
function processProps(props) {
24+
const { initialDates, maxDate, minDate, selectionType } = props;
25+
const maxLimit = maxDate ? normalizeDate(new Date(maxDate), 23, 59, 59, 999) : null;
26+
const renderDate = normalizeDate(((initialDates && initialDates.length) ? new Date(initialDates[0]) : new Date()));
27+
28+
let minLimit = minDate ? normalizeDate(new Date(minDate)) : null;
29+
let selectedDates = [null, null];
30+
31+
if (initialDates) {
32+
selectedDates = selectedDates.map((item, idx) => {
33+
if (!initialDates[idx]) {
34+
return null;
35+
}
1936

20-
let minLimit = minDate ? normalizeDate(new Date(minDate)) : null;
21-
let selectedDates = [null, null];
22-
if (initialDates) {
23-
selectedDates = selectedDates.map((item, idx) => {
24-
if (!initialDates[idx]) {
25-
return null;
26-
}
37+
return normalizeDate(new Date(initialDates[idx]));
38+
});
39+
}
2740

28-
return normalizeDate(new Date(initialDates[idx]));
29-
});
41+
/**
42+
* If a minDate or a maxDate is set, let's check if any selectedDates are outside of the boundaries.
43+
* If so, resets the selectedDates.
44+
*/
45+
if (minLimit || maxLimit) {
46+
const isAnyDateOutOfLimit = selectedDates.some(item => (
47+
item && (
48+
(minLimit && (minLimit.getTime() > item.getTime())) ||
49+
(maxLimit && (maxLimit.getTime() < item.getTime()))
50+
)
51+
));
52+
53+
if (isAnyDateOutOfLimit) {
54+
selectedDates = [null, null];
55+
console.warn(`A calendar instance contains a selectedDate outside of the minDate and maxDate boundaries`); // eslint-disable-line
3056
}
57+
}
3158

32-
/**
33-
* If a minDate or a maxDate is set, let's check if any selectedDates are outside of the boundaries.
34-
* If so, resets the selectedDates.
35-
*/
36-
if (minLimit || maxLimit) {
37-
const isAnyDateOutOfLimit = selectedDates.some(item => (
38-
item && (
39-
(minLimit && (minLimit.getTime() > item.getTime())) ||
40-
(maxLimit && (maxLimit.getTime() < item.getTime()))
41-
)
42-
));
43-
44-
if (isAnyDateOutOfLimit) {
45-
selectedDates = [null, null];
46-
console.warn(`A calendar instance contains a selectedDate outside of the minDate and maxDate boundaries`); // eslint-disable-line
47-
}
48-
}
59+
/** If initialDates is defined and we have a start date, we want to set it as the minLimit */
60+
if (selectedDates[0] && (selectionType === CALENDAR_SELECTION_TYPE_RANGE)) {
61+
minLimit = selectedDates[0];
62+
}
4963

50-
/** If initialDates is defined and we have a start date, we want to set it as the minLimit */
51-
if (selectedDates[0] && (selectionType === CALENDAR_SELECTION_TYPE_RANGE)) {
52-
minLimit = selectedDates[0];
53-
}
64+
return {
65+
maxLimit,
66+
minLimit,
67+
renderDate,
68+
selectedDates,
69+
};
70+
}
5471

55-
this.state = {
56-
maxLimit,
57-
minLimit,
58-
renderDate,
59-
selectedDates,
60-
};
72+
export default class Calendar extends Component {
73+
constructor(props) {
74+
super();
6175

6276
this.moveToMonth = this.moveToMonth.bind(this);
77+
this.state = processProps(props);
78+
}
79+
80+
componentWillReceiveProps(newProps) {
81+
this.setState(() => processProps(newProps));
6382
}
6483

6584
/**

0 commit comments

Comments
 (0)