@@ -9,57 +9,76 @@ const {
9
9
CALENDAR_SELECTION_TYPE_RANGE ,
10
10
} = calendarConstants ;
11
11
12
- export default class Calendar extends Component {
13
- constructor ( props ) {
14
- super ( ) ;
15
12
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
+ }
19
36
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
+ }
27
40
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
30
56
}
57
+ }
31
58
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
+ }
49
63
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
+ }
54
71
55
- this . state = {
56
- maxLimit,
57
- minLimit,
58
- renderDate,
59
- selectedDates,
60
- } ;
72
+ export default class Calendar extends Component {
73
+ constructor ( props ) {
74
+ super ( ) ;
61
75
62
76
this . moveToMonth = this . moveToMonth . bind ( this ) ;
77
+ this . state = processProps ( props ) ;
78
+ }
79
+
80
+ componentWillReceiveProps ( newProps ) {
81
+ this . setState ( ( ) => processProps ( newProps ) ) ;
63
82
}
64
83
65
84
/**
0 commit comments